[libvirt] [PATCH V2] free buf->content when vsnprintf() failed

When buf->error is 1, we do not return buf->content in the function virBufferContentAndReset(). So we should free buf->content when vsnprintf() failed. --- Changes v1->v2 - rename virBufferNoMemory() to virBufferSetError() and use it to free buf->content as Laine Stump suggested src/util/buf.c | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/buf.c b/src/util/buf.c index 7557ad1..a3cc063 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -39,7 +39,7 @@ struct _virBuffer { * freeing the content and setting the error flag. */ static void -virBufferNoMemory(const virBufferPtr buf) +virBufferSetError(const virBufferPtr buf) { VIR_FREE(buf->content); buf->size = 0; @@ -70,7 +70,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len) size = buf->use + len + 1000; if (VIR_REALLOC_N(buf->content, size) < 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return -1; } buf->size = size; @@ -241,7 +241,7 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...) size = buf->size - buf->use; if ((count = vsnprintf(&buf->content[buf->use], size, format, argptr)) < 0) { - buf->error = 1; + virBufferSetError(buf); goto err; } @@ -259,7 +259,7 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...) size = buf->size - buf->use; if ((count = vsnprintf(&buf->content[buf->use], size, format, argptr)) < 0) { - buf->error = 1; + virBufferSetError(buf); goto err; } } @@ -299,7 +299,7 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st } if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return; } @@ -386,7 +386,7 @@ virBufferEscapeSexpr(const virBufferPtr buf, } if (VIR_ALLOC_N(escaped, 2 * len + 1) < 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return; } -- 1.7.1

On Wed, Apr 27, 2011 at 01:43:24PM +0800, Wen Congyang wrote:
When buf->error is 1, we do not return buf->content in the function virBufferContentAndReset(). So we should free buf->content when vsnprintf() failed.
--- Changes v1->v2 - rename virBufferNoMemory() to virBufferSetError() and use it to free buf->content as Laine Stump suggested
src/util/buf.c | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/util/buf.c b/src/util/buf.c index 7557ad1..a3cc063 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -39,7 +39,7 @@ struct _virBuffer { * freeing the content and setting the error flag. */ static void -virBufferNoMemory(const virBufferPtr buf) +virBufferSetError(const virBufferPtr buf) { VIR_FREE(buf->content); buf->size = 0; @@ -70,7 +70,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len) size = buf->use + len + 1000;
if (VIR_REALLOC_N(buf->content, size) < 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return -1; } buf->size = size; @@ -241,7 +241,7 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...) size = buf->size - buf->use; if ((count = vsnprintf(&buf->content[buf->use], size, format, argptr)) < 0) { - buf->error = 1; + virBufferSetError(buf); goto err; }
@@ -259,7 +259,7 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...) size = buf->size - buf->use; if ((count = vsnprintf(&buf->content[buf->use], size, format, argptr)) < 0) { - buf->error = 1; + virBufferSetError(buf); goto err; } } @@ -299,7 +299,7 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st }
if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return; }
@@ -386,7 +386,7 @@ virBufferEscapeSexpr(const virBufferPtr buf, }
if (VIR_ALLOC_N(escaped, 2 * len + 1) < 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return; }
ACK, Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/

At 2011-4-27 14:22, Daniel Veillard write:
On Wed, Apr 27, 2011 at 01:43:24PM +0800, Wen Congyang wrote:
When buf->error is 1, we do not return buf->content in the function virBufferContentAndReset(). So we should free buf->content when vsnprintf() failed.
--- Changes v1->v2 - rename virBufferNoMemory() to virBufferSetError() and use it to free buf->content as Laine Stump suggested
src/util/buf.c | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/util/buf.c b/src/util/buf.c index 7557ad1..a3cc063 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -39,7 +39,7 @@ struct _virBuffer { * freeing the content and setting the error flag. */ static void -virBufferNoMemory(const virBufferPtr buf) +virBufferSetError(const virBufferPtr buf) { VIR_FREE(buf->content); buf->size = 0; @@ -70,7 +70,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len) size = buf->use + len + 1000;
if (VIR_REALLOC_N(buf->content, size)< 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return -1; } buf->size = size; @@ -241,7 +241,7 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...) size = buf->size - buf->use; if ((count = vsnprintf(&buf->content[buf->use], size, format, argptr))< 0) { - buf->error = 1; + virBufferSetError(buf); goto err; }
@@ -259,7 +259,7 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...) size = buf->size - buf->use; if ((count = vsnprintf(&buf->content[buf->use], size, format, argptr))< 0) { - buf->error = 1; + virBufferSetError(buf); goto err; } } @@ -299,7 +299,7 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st }
if (VIR_ALLOC_N(escaped, 6 * len + 1)< 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return; }
@@ -386,7 +386,7 @@ virBufferEscapeSexpr(const virBufferPtr buf, }
if (VIR_ALLOC_N(escaped, 2 * len + 1)< 0) { - virBufferNoMemory(buf); + virBufferSetError(buf); return; }
ACK,
Thanks, pushed.
Daniel
participants (3)
-
Daniel Veillard
-
Wen Congyang
-
Wen Congyang