提交 e154e01e 编写于 作者: P Peter Krempa

util: buffer: Simplify handling of indent overflows

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: NPeter Krempa <pkrempa@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 49037f94
......@@ -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;
}
}
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册