diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 693e4b2942ea6097fc77acda922481cbca799882..4969497c008b83cd4421e6842b0714618d4f8091 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -660,27 +660,28 @@ virBufferStrcat(virBufferPtr buf, ...) * further limits how much of the tail is trimmed. If @str is NULL, then * @len must be non-negative. * - * Returns -1 if @buf has previously encountered an error or if @len is - * invalid, 0 if there was nothing to trim (@buf was too short or @str - * didn't match), and 1 if the trim was successful. + * Sets error to -1 (usage) if str is NULL and len is less than zero. */ -int +void virBufferTrim(virBufferPtr buf, const char *str, int len) { size_t len2 = 0; - if (!buf || buf->error || (!str && len < 0)) - return -1; + if (!buf || buf->error) + return; + if (!str && len < 0) { + virBufferSetError(buf, -1); + return; + } if (len > 0 && len > buf->use) - return 0; + return; if (str) { len2 = strlen(str); if (len2 > buf->use || memcmp(&buf->content[buf->use - len2], str, len2) != 0) - return 0; + return; } buf->use -= len < 0 ? len2 : len; buf->content[buf->use] = '\0'; - return 1; } diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 7b69c0e5cf0d1a3f36d7395faf525ac48db72d6a..e0b77ab7dde605f3f96d525f1daf4089629840af 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -77,6 +77,6 @@ void virBufferURIEncodeString(virBufferPtr buf, const char *str); void virBufferAdjustIndent(virBufferPtr buf, int indent); int virBufferGetIndent(const virBufferPtr buf, bool dynamic); -int virBufferTrim(virBufferPtr buf, const char *trim, int len); +void virBufferTrim(virBufferPtr buf, const char *trim, int len); #endif /* __VIR_BUFFER_H__ */ diff --git a/tests/virbuftest.c b/tests/virbuftest.c index c7a504b2f1f42d5383d07af3e3dd020e07882eb1..3938f0df57bfbc3187dc69addb17804aaf3b893d 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -148,38 +148,21 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED) char *result = NULL; const char *expected = "a,b"; int ret = -1; - int i = 1; - -#define ACT(str, len, result) \ - do { \ - if (virBufferTrim(buf, str, len) != result) { \ - TEST_ERROR("trim %d failed", i); \ - goto cleanup; \ - } \ - i++; \ - } while (0); - - if (virBufferTrim(buf, "", 0) != -1) { - TEST_ERROR("Wrong failure detection 1"); - goto cleanup; - } + + virBufferTrim(buf, "", 0); buf = &bufinit; - if (virBufferTrim(buf, NULL, -1) != -1) { - TEST_ERROR("Wrong failure detection 2"); - goto cleanup; - } virBufferAddLit(buf, "a;"); - ACT("", 0, 1); - ACT("", -1, 1); - ACT(NULL, 1, 1); - ACT(NULL, 5, 0); - ACT("a", 2, 0); + virBufferTrim(buf, "", 0); + virBufferTrim(buf, "", -1); + virBufferTrim(buf, NULL, 1); + virBufferTrim(buf, NULL, 5); + virBufferTrim(buf, "a", 2); virBufferAddLit(buf, ",b,,"); - ACT("b", -1, 0); - ACT("b,,", 1, 1); - ACT(",", -1, 1); + virBufferTrim(buf, "b", -1); + virBufferTrim(buf, "b,,", 1); + virBufferTrim(buf, ",", -1); result = virBufferContentAndReset(buf); if (!result || STRNEQ(result, expected)) { @@ -187,6 +170,12 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED) goto cleanup; } + virBufferTrim(buf, NULL, -1); + if (virBufferError(buf) != -1) { + TEST_ERROR("Usage error not flagged"); + goto cleanup; + } + ret = 0; cleanup: diff --git a/tools/virsh.c b/tools/virsh.c index 26d37c69ad808e821e34484421aab99108202c45..af2bb76a8373172b7c86307091ffc13670080089 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -632,18 +632,15 @@ vshTreePrintInternal(vshControl *ctl, false, indent) < 0) goto cleanup; } - if (virBufferTrim(indent, " ", -1) < 0) - goto cleanup; + virBufferTrim(indent, " ", -1); /* If there was no child device, and we're the last in * a list of devices, then print another blank line */ if (nextlastdev == -1 && devid == lastdev) vshPrint(ctl, "%s\n", virBufferCurrentContent(indent)); - if (!root) { - if (virBufferTrim(indent, NULL, 2) < 0) - goto cleanup; - } + if (!root) + virBufferTrim(indent, NULL, 2); ret = 0; cleanup: return ret;