[PATCH 07/19] util: buffer: Simplify handling of indent overflows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rather than setting usage error truncate the indentation level. Having
the output string misformated is way more useful to figure out where the
error lies rather than reporting an error after a giant formatter
function.

In testBufAutoIndent we now validate that the indentation is truncated
and testBufAddBuffer2 is removed since it became bogus.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/virbuffer.c |  8 ++++----
 tests/virbuftest.c   | 35 ++++-------------------------------
 2 files changed, 8 insertions(+), 35 deletions(-)

diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
index 9a74ca78bb..69fcf946f5 100644
--- a/src/util/virbuffer.c
+++ b/src/util/virbuffer.c
@@ -56,8 +56,8 @@ virBufferSetError(virBufferPtr buf, int error)
  * negative to decrease).  Automatic indentation is performed by all
  * additive functions when the existing buffer is empty or ends with a
  * newline (however, note that no indentation is added after newlines
- * embedded in an appended string).  If @indent would cause overflow,
- * the buffer error indicator is set.
+ * embedded in an appended string).  If @indent would cause overflow, the
+ * indentation level is truncated.
  */
 void
 virBufferAdjustIndent(virBufferPtr buf, int indent)
@@ -67,12 +67,12 @@ virBufferAdjustIndent(virBufferPtr buf, int indent)

     if (indent > 0) {
         if (INT_MAX - indent < buf->indent) {
-            virBufferSetError(buf, -1);
+            buf->indent = INT_MAX;
             return;
         }
     } else {
         if (buf->indent < -indent) {
-            virBufferSetError(buf, -1);
+            buf->indent = 0;
             return;
         }
     }
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
index 8b8754adfa..246c572bd2 100644
--- a/tests/virbuftest.c
+++ b/tests/virbuftest.c
@@ -85,12 +85,12 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
         ret = -1;
     }
     virBufferAdjustIndent(buf, -3);
-    if (virBufferGetIndent(buf, false) != -1 ||
-        virBufferGetIndent(buf, true) != -1 ||
-        virBufferError(buf) != -1) {
-        VIR_TEST_DEBUG("Usage error not flagged");
+    if (virBufferGetIndent(buf, false) != 0 ||
+        virBufferGetIndent(buf, true) != 0) {
+        VIR_TEST_DEBUG("Indentation level not truncated");
         ret = -1;
     }
+    virBufferAdjustIndent(buf, 3);
     virBufferFreeAndReset(buf);
     if (virBufferGetIndent(buf, false) != 0 ||
         virBufferGetIndent(buf, true) != 0 ||
@@ -298,32 +298,6 @@ static int testBufAddBuffer(const void *data G_GNUC_UNUSED)
     return ret;
 }

-static int
-testBufAddBuffer2(const void *opaque G_GNUC_UNUSED)
-{
-    g_auto(virBuffer) buf1 = VIR_BUFFER_INITIALIZER;
-    g_auto(virBuffer) buf2 = VIR_BUFFER_INITIALIZER;
-
-    /* Intent of this test is to demonstrate a memleak that happen with
-     * virBufferAddBuffer */
-
-    virBufferAddLit(&buf1, "Hello world!\n");
-    virBufferAddLit(&buf2, "Hello world!\n");
-
-    /* Intentional usage error */
-    virBufferAdjustIndent(&buf2, -2);
-
-    virBufferAddBuffer(&buf1, &buf2);
-
-    if (virBufferCurrentContent(&buf1) ||
-        !virBufferCurrentContent(&buf2)) {
-        VIR_TEST_DEBUG("Unexpected buffer content");
-        return -1;
-    }
-
-    return 0;
-}
-
 struct testBufAddStrData {
     const char *data;
     const char *expect;
@@ -481,7 +455,6 @@ mymain(void)
     DO_TEST("Auto-indentation", testBufAutoIndent, 0);
     DO_TEST("Trim", testBufTrim, 0);
     DO_TEST("AddBuffer", testBufAddBuffer, 0);
-    DO_TEST("AddBuffer2", testBufAddBuffer2, 0);
     DO_TEST("set indent", testBufSetIndent, 0);
     DO_TEST("autoclean", testBufferAutoclean, 0);

-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux