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

Wen Congyang wency at cn.fujitsu.com
Wed Apr 27 05:19:47 UTC 2011


At 04/26/2011 11:43 PM, Laine Stump Write:
> 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).

Yes, virBufferNoMemory() is the only place that buf->error is set to 1 now.
I will rename it and update this patch.

Thaks for reviewing.
Wen Congyang

> 
> 
>>           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;
>>           }
> 
> -- 
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
> 




More information about the libvir-list mailing list