]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: buffer: Simplify handling of indent overflows
authorPeter Krempa <pkrempa@redhat.com>
Thu, 24 Oct 2019 07:25:20 +0000 (09:25 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 24 Oct 2019 17:35:34 +0000 (19:35 +0200)
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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virbuffer.c
tests/virbuftest.c

index 9a74ca78bb7a120a5e410f7da4140cd36ce5830b..69fcf946f559464ab484a58977855264833e77ea 100644 (file)
@@ -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;
         }
     }
index 8b8754adfa9f700feaece8df0b6ddc99240ae213..246c572bd2247624fe18ee268fe35b431420d0ff 100644 (file)
@@ -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);