On 04/25/2011 11:42 PM, 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.
---
src/util/buf.c | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/src/util/buf.c b/src/util/buf.c
index 7557ad1..fdb7660 100644
--- a/src/util/buf.c
+++ b/src/util/buf.c
@@ -241,6 +241,9 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
size = buf->size - buf->use;
if ((count = vsnprintf(&buf->content[buf->use],
size, format, argptr))< 0) {
+ VIR_FREE(buf->content);
+ buf->size = 0;
+ buf->use = 0;
buf->error = 1;
These four lines are equivalent to calling virBufferNoMemory(buf). It
would be more compact to do that instead. (as a matter of fact,
virBufferNoMemory is the only other place that buf->error is ever set to
1, so it really is a "virBufferSetError() function).
goto err;
}
@@ -259,6 +262,9 @@ virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
size = buf->size - buf->use;
if ((count = vsnprintf(&buf->content[buf->use],
size, format, argptr))< 0) {
+ VIR_FREE(buf->content);
+ buf->size = 0;
+ buf->use = 0;
buf->error = 1;
goto err;
}