buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "testutils.h"
|
2012-12-04 12:04:07 +00:00
|
|
|
#include "virbuffer.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
|
2013-06-07 15:10:28 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
struct testInfo {
|
|
|
|
int doEscape;
|
|
|
|
};
|
|
|
|
|
2011-10-20 21:48:47 +00:00
|
|
|
static int testBufInfiniteLoop(const void *data)
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
{
|
|
|
|
virBuffer bufinit = VIR_BUFFER_INITIALIZER;
|
|
|
|
virBufferPtr buf = &bufinit;
|
|
|
|
char *addstr = NULL, *bufret = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
const struct testInfo *info = data;
|
|
|
|
|
|
|
|
virBufferAddChar(buf, 'a');
|
|
|
|
|
|
|
|
/*
|
2011-10-20 21:48:47 +00:00
|
|
|
* Infinite loop used to trigger if:
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
* (strlen + 1 > 1000) && (strlen == buf-size - buf-use - 1)
|
2011-10-20 21:48:47 +00:00
|
|
|
* which was the case after the above addchar at the time of the bug.
|
|
|
|
* This test is a bit fragile, since it relies on virBuffer internals.
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
*/
|
2014-11-13 14:20:43 +00:00
|
|
|
if (virAsprintf(&addstr, "%*s", buf->a - buf->b - 1, "a") < 0)
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (info->doEscape)
|
|
|
|
virBufferEscapeString(buf, "%s", addstr);
|
|
|
|
else
|
2011-04-30 16:34:49 +00:00
|
|
|
virBufferAsprintf(buf, "%s", addstr);
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:44 +00:00
|
|
|
out:
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
bufret = virBufferContentAndReset(buf);
|
|
|
|
if (!bufret) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Buffer had error set");
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(addstr);
|
|
|
|
VIR_FREE(bufret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-20 21:48:47 +00:00
|
|
|
static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virBuffer bufinit = VIR_BUFFER_INITIALIZER;
|
|
|
|
virBufferPtr buf = &bufinit;
|
|
|
|
const char expected[] =
|
|
|
|
" 1\n 2\n 3\n 4\n 5\n 6\n 7\n &\n 8\n 9\n 10\n ' 11'\n";
|
|
|
|
char *result = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (virBufferGetIndent(buf, false) != 0 ||
|
|
|
|
virBufferGetIndent(buf, true) != 0) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Wrong indentation");
|
2011-10-20 21:48:47 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virBufferAdjustIndent(buf, 3);
|
2012-06-08 19:50:23 +00:00
|
|
|
if (STRNEQ(virBufferCurrentContent(buf), "")) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Wrong content");
|
2012-06-08 19:50:23 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
2011-10-20 21:48:47 +00:00
|
|
|
if (virBufferGetIndent(buf, false) != 3 ||
|
|
|
|
virBufferGetIndent(buf, true) != 3 ||
|
|
|
|
virBufferError(buf)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Wrong indentation");
|
2011-10-20 21:48:47 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virBufferAdjustIndent(buf, -2);
|
|
|
|
if (virBufferGetIndent(buf, false) != 1 ||
|
|
|
|
virBufferGetIndent(buf, true) != 1 ||
|
|
|
|
virBufferError(buf)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Wrong indentation");
|
2011-10-20 21:48:47 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virBufferAdjustIndent(buf, -3);
|
|
|
|
if (virBufferGetIndent(buf, false) != -1 ||
|
|
|
|
virBufferGetIndent(buf, true) != -1 ||
|
|
|
|
virBufferError(buf) != -1) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Usage error not flagged");
|
2011-10-20 21:48:47 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virBufferFreeAndReset(buf);
|
|
|
|
if (virBufferGetIndent(buf, false) != 0 ||
|
|
|
|
virBufferGetIndent(buf, true) != 0 ||
|
|
|
|
virBufferError(buf)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Reset didn't clear indentation");
|
2011-10-20 21:48:47 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virBufferAdjustIndent(buf, 2);
|
|
|
|
virBufferAddLit(buf, "1");
|
2013-09-25 14:31:18 +00:00
|
|
|
if (virBufferError(buf)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Buffer had error");
|
2013-09-25 14:31:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-06-08 19:50:23 +00:00
|
|
|
if (STRNEQ(virBufferCurrentContent(buf), " 1")) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Wrong content");
|
2012-06-08 19:50:23 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
2011-10-20 21:48:47 +00:00
|
|
|
if (virBufferGetIndent(buf, false) != 2 ||
|
|
|
|
virBufferGetIndent(buf, true) != 0) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Wrong indentation");
|
2011-10-20 21:48:47 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virBufferAddLit(buf, "\n");
|
|
|
|
virBufferAdd(buf, "" "2\n", -1); /* Extra "" appeases syntax-check */
|
|
|
|
virBufferAddChar(buf, '3');
|
|
|
|
virBufferAddChar(buf, '\n');
|
|
|
|
virBufferAsprintf(buf, "%d", 4);
|
|
|
|
virBufferAsprintf(buf, "%c", '\n');
|
|
|
|
virBufferStrcat(buf, "5", "\n", "6\n", NULL);
|
|
|
|
virBufferEscapeString(buf, "%s\n", "7");
|
|
|
|
virBufferEscapeString(buf, "%s\n", "&");
|
|
|
|
virBufferEscapeSexpr(buf, "%s", "8\n");
|
|
|
|
virBufferURIEncodeString(buf, "9");
|
|
|
|
virBufferAddChar(buf, '\n');
|
|
|
|
virBufferEscapeShell(buf, "10");
|
|
|
|
virBufferAddChar(buf, '\n');
|
|
|
|
virBufferEscapeShell(buf, " 11");
|
|
|
|
virBufferAddChar(buf, '\n');
|
|
|
|
|
2013-09-25 14:31:18 +00:00
|
|
|
if (virBufferError(buf)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Buffer had error");
|
2013-09-25 14:31:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-20 21:48:47 +00:00
|
|
|
result = virBufferContentAndReset(buf);
|
|
|
|
if (!result || STRNEQ(result, expected)) {
|
|
|
|
virtTestDifference(stderr, expected, result);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
VIR_FREE(result);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
static int testBufTrim(const void *data ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virBuffer bufinit = VIR_BUFFER_INITIALIZER;
|
|
|
|
virBufferPtr buf = NULL;
|
|
|
|
char *result = NULL;
|
|
|
|
const char *expected = "a,b";
|
|
|
|
int ret = -1;
|
2013-06-18 15:52:00 +00:00
|
|
|
|
|
|
|
virBufferTrim(buf, "", 0);
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
buf = &bufinit;
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "a;");
|
2013-06-18 15:52:00 +00:00
|
|
|
virBufferTrim(buf, "", 0);
|
|
|
|
virBufferTrim(buf, "", -1);
|
|
|
|
virBufferTrim(buf, NULL, 1);
|
|
|
|
virBufferTrim(buf, NULL, 5);
|
|
|
|
virBufferTrim(buf, "a", 2);
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
|
|
|
|
virBufferAddLit(buf, ",b,,");
|
2013-06-18 15:52:00 +00:00
|
|
|
virBufferTrim(buf, "b", -1);
|
|
|
|
virBufferTrim(buf, "b,,", 1);
|
|
|
|
virBufferTrim(buf, ",", -1);
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
|
2013-09-25 14:31:18 +00:00
|
|
|
if (virBufferError(buf)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Buffer had error");
|
2013-09-25 14:31:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
result = virBufferContentAndReset(buf);
|
|
|
|
if (!result || STRNEQ(result, expected)) {
|
|
|
|
virtTestDifference(stderr, expected, result);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-06-18 15:52:00 +00:00
|
|
|
virBufferTrim(buf, NULL, -1);
|
|
|
|
if (virBufferError(buf) != -1) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Usage error not flagged");
|
2013-06-18 15:52:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:44 +00:00
|
|
|
cleanup:
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
virBufferFreeAndReset(buf);
|
|
|
|
VIR_FREE(result);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-19 09:56:58 +00:00
|
|
|
static int testBufAddBuffer(const void *data ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virBuffer buf1 = VIR_BUFFER_INITIALIZER;
|
|
|
|
virBuffer buf2 = VIR_BUFFER_INITIALIZER;
|
|
|
|
virBuffer buf3 = VIR_BUFFER_INITIALIZER;
|
|
|
|
int ret = -1;
|
|
|
|
char *result = NULL;
|
|
|
|
const char *expected = \
|
|
|
|
" A long time ago, in a galaxy far,\n" \
|
|
|
|
" far away...\n" \
|
|
|
|
" It is a period of civil war.\n" \
|
|
|
|
" Rebel spaceships, striking\n" \
|
|
|
|
" from a hidden base, have won\n" \
|
|
|
|
" their first victory against\n" \
|
|
|
|
" the evil Galactic Empire.\n" \
|
|
|
|
" During the battle, rebel\n" \
|
|
|
|
" spies managed to steal secret\n" \
|
|
|
|
" plans to the Empire's\n" \
|
|
|
|
" ultimate weapon, the DEATH\n" \
|
|
|
|
" STAR, an armored space\n" \
|
|
|
|
" station with enough power to\n" \
|
|
|
|
" destroy an entire planet.\n";
|
|
|
|
|
|
|
|
if (virBufferUse(&buf1)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf1 already in use");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virBufferUse(&buf2)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf2 already in use");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virBufferUse(&buf3)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf3 already in use");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferAdjustIndent(&buf1, 2);
|
|
|
|
virBufferAddLit(&buf1, "A long time ago, in a galaxy far,\n");
|
|
|
|
virBufferAddLit(&buf1, "far away...\n");
|
|
|
|
|
|
|
|
virBufferAdjustIndent(&buf2, 4);
|
|
|
|
virBufferAddLit(&buf2, "It is a period of civil war.\n");
|
|
|
|
virBufferAddLit(&buf2, "Rebel spaceships, striking\n");
|
|
|
|
virBufferAddLit(&buf2, "from a hidden base, have won\n");
|
|
|
|
virBufferAddLit(&buf2, "their first victory against\n");
|
|
|
|
virBufferAddLit(&buf2, "the evil Galactic Empire.\n");
|
|
|
|
|
|
|
|
virBufferAdjustIndent(&buf3, 2);
|
|
|
|
virBufferAddLit(&buf3, "During the battle, rebel\n");
|
|
|
|
virBufferAddLit(&buf3, "spies managed to steal secret\n");
|
|
|
|
virBufferAddLit(&buf3, "plans to the Empire's\n");
|
|
|
|
virBufferAddLit(&buf3, "ultimate weapon, the DEATH\n");
|
|
|
|
virBufferAddLit(&buf3, "STAR, an armored space\n");
|
|
|
|
virBufferAddLit(&buf3, "station with enough power to\n");
|
|
|
|
virBufferAddLit(&buf3, "destroy an entire planet.\n");
|
|
|
|
|
|
|
|
if (!virBufferUse(&buf1)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Error adding to buf1");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virBufferUse(&buf2)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Error adding to buf2");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virBufferUse(&buf3)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("Error adding to buf3");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferAddBuffer(&buf2, &buf3);
|
|
|
|
|
|
|
|
if (!virBufferUse(&buf2)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf2 cleared mistakenly");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virBufferUse(&buf3)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf3 is not clear even though it should be");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferAddBuffer(&buf1, &buf2);
|
|
|
|
|
|
|
|
if (!virBufferUse(&buf1)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf1 cleared mistakenly");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virBufferUse(&buf2)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf2 is not clear even though it should be");
|
2015-02-19 09:56:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = virBufferContentAndReset(&buf1);
|
|
|
|
if (STRNEQ_NULLABLE(result, expected)) {
|
|
|
|
virtTestDifference(stderr, expected, result);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virBufferFreeAndReset(&buf1);
|
|
|
|
virBufferFreeAndReset(&buf2);
|
|
|
|
VIR_FREE(result);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-24 09:53:29 +00:00
|
|
|
struct testBufAddStrData {
|
|
|
|
const char *data;
|
|
|
|
const char *expect;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
testBufAddStr(const void *opaque ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
const struct testBufAddStrData *data = opaque;
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *actual;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<c>\n");
|
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
virBufferAddStr(&buf, data->data);
|
|
|
|
virBufferAdjustIndent(&buf, -2);
|
|
|
|
virBufferAddLit(&buf, "</c>");
|
|
|
|
|
|
|
|
if (!(actual = virBufferContentAndReset(&buf))) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf is empty");
|
2015-03-24 09:53:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STRNEQ_NULLABLE(actual, data->expect)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("testBufAddStr(): Strings don't match:\n");
|
2015-03-24 09:53:29 +00:00
|
|
|
virtTestDifference(stderr, data->expect, actual);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(actual);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
|
2015-03-30 10:41:40 +00:00
|
|
|
static int
|
|
|
|
testBufEscapeStr(const void *opaque ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
const struct testBufAddStrData *data = opaque;
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *actual;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<c>\n");
|
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
virBufferEscapeString(&buf, "<el>%s</el>\n", data->data);
|
|
|
|
virBufferAdjustIndent(&buf, -2);
|
|
|
|
virBufferAddLit(&buf, "</c>");
|
|
|
|
|
|
|
|
if (!(actual = virBufferContentAndReset(&buf))) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("buf is empty");
|
2015-03-30 10:41:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STRNEQ_NULLABLE(actual, data->expect)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_DEBUG("testBufEscapeStr(): Strings don't match:\n");
|
2015-03-30 10:41:40 +00:00
|
|
|
virtTestDifference(stderr, data->expect, actual);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(actual);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
static int
|
2011-04-29 16:21:20 +00:00
|
|
|
mymain(void)
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
2010-09-13 11:33:31 +00:00
|
|
|
#define DO_TEST(msg, cb, data) \
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
do { \
|
|
|
|
struct testInfo info = { data }; \
|
2013-09-20 18:13:35 +00:00
|
|
|
if (virtTestRun("Buf: " msg, cb, &info) < 0) \
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
ret = -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
DO_TEST("EscapeString infinite loop", testBufInfiniteLoop, 1);
|
|
|
|
DO_TEST("VSprintf infinite loop", testBufInfiniteLoop, 0);
|
2011-10-20 21:48:47 +00:00
|
|
|
DO_TEST("Auto-indentation", testBufAutoIndent, 0);
|
virBuffer: add way to trim back extra text
I'm tired of writing:
bool sep = false;
while (...) {
if (sep)
virBufferAddChar(buf, ',');
sep = true;
virBufferAdd(buf, str);
}
This makes it easier, allowing one to write:
while (...)
virBufferAsprintf(buf, "%s,", str);
virBufferTrim(buf, ",", -1);
to trim any remaining comma.
* src/util/buf.h (virBufferTrim): Declare.
* src/util/buf.c (virBufferTrim): New function.
* tests/virbuftest.c (testBufTrim): Test it.
2012-05-18 22:36:59 +00:00
|
|
|
DO_TEST("Trim", testBufTrim, 0);
|
2015-02-19 09:56:58 +00:00
|
|
|
DO_TEST("AddBuffer", testBufAddBuffer, 0);
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
|
2015-03-24 09:53:29 +00:00
|
|
|
#define DO_TEST_ADD_STR(DATA, EXPECT) \
|
|
|
|
do { \
|
|
|
|
struct testBufAddStrData info = { DATA, EXPECT }; \
|
|
|
|
if (virtTestRun("Buf: AddStr", testBufAddStr, &info) < 0) \
|
|
|
|
ret = -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
DO_TEST_ADD_STR("", "<c>\n</c>");
|
|
|
|
DO_TEST_ADD_STR("<a/>", "<c>\n <a/></c>");
|
|
|
|
DO_TEST_ADD_STR("<a/>\n", "<c>\n <a/>\n</c>");
|
|
|
|
DO_TEST_ADD_STR("<b>\n <a/>\n</b>\n", "<c>\n <b>\n <a/>\n </b>\n</c>");
|
|
|
|
|
2015-03-30 10:41:40 +00:00
|
|
|
#define DO_TEST_ESCAPE(data, expect) \
|
|
|
|
do { \
|
|
|
|
struct testBufAddStrData info = { data, expect }; \
|
|
|
|
if (virtTestRun("Buf: EscapeStr", testBufEscapeStr, &info) < 0) \
|
|
|
|
ret = -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
DO_TEST_ESCAPE("<td></td><td></td>",
|
|
|
|
"<c>\n <el><td></td><td></td></el>\n</c>");
|
|
|
|
DO_TEST_ESCAPE("\007\"&&\"\x15",
|
|
|
|
"<c>\n <el>"&&"</el>\n</c>");
|
|
|
|
DO_TEST_ESCAPE(",,'..',,",
|
|
|
|
"<c>\n <el>,,'..',,</el>\n</c>");
|
|
|
|
DO_TEST_ESCAPE("\x01\x01\x02\x03\x05\x08",
|
|
|
|
"<c>\n <el></el>\n</c>");
|
|
|
|
|
2014-03-17 09:38:38 +00:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
buf: Fix possible infinite loop in EscapeString, VSnprintf
The current code will go into an infinite loop if the printf generated
string is >= 1000, AND exactly 1 character smaller than the amount of free
space in the buffer. When this happens, we are dropped into the loop body,
but nothing will actually change, because count == (buf->size - buf->use - 1),
and virBufferGrow returns unchanged if count < (buf->size - buf->use)
Fix this by removing the '- 1' bit from 'size'. The *nprintf functions handle
the NULL byte for us anyways, so we shouldn't need to manually accommodate
for it.
Here's a bug where we are actually hitting this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=602772
v2: Eric's improvements: while -> if (), remove extra va_list variable,
make sure we report buffer error if snprintf fails
v3: Add tests/virbuftest which reproduces the infinite loop before this
patch, works correctly after
2010-09-01 17:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIRT_TEST_MAIN(mymain)
|