[libvirt] PATCH: Convert buf.c to use memory alloc wrappers

Daniel P. Berrange berrange at redhat.com
Thu May 22 18:08:22 UTC 2008


This patch coverts the virBuffer code over to using our internal memory
allocation wrappers. It then fixes a bug in xend_internal.c where it
was failing to check for allocation failure, although this was technically
completely harmless, since the  virBuffer routines already protected
against this specific scenario. 

 src/buf.c           |   16 ++++++----------
 src/xend_internal.c |    4 ++++
 2 files changed, 10 insertions(+), 10 deletions(-)

Dan

diff -r 797f37ce08c2 src/buf.c
--- a/src/buf.c	Wed May 21 22:22:48 2008 -0400
+++ b/src/buf.c	Thu May 22 11:42:32 2008 -0400
@@ -21,6 +21,7 @@
 #define __VIR_BUFFER_C__
 
 #include "buf.h"
+#include "memory.h"
 
 
 /* If adding more fields, ensure to edit buf.h to match
@@ -42,8 +43,7 @@
 static void
 virBufferNoMemory(const virBufferPtr buf)
 {
-    free(buf->content);
-    buf->content = NULL;
+    VIR_FREE(buf->content);
     buf->size = 0;
     buf->use = 0;
     buf->error = 1;
@@ -62,7 +62,6 @@
 virBufferGrow(virBufferPtr buf, unsigned int len)
 {
     int size;
-    char *newbuf;
 
     if (buf->error)
         return -1;
@@ -72,12 +71,10 @@
 
     size = buf->use + len + 1000;
 
-    newbuf = realloc(buf->content, size);
-    if (newbuf == NULL) {
+    if (VIR_REALLOC_N(buf->content, size) < 0) {
         virBufferNoMemory(buf);
         return -1;
     }
-    buf->content = newbuf;
     buf->size = size;
     return 0;
 }
@@ -271,8 +268,7 @@
         return;
 
     len = strlen(str);
-    escaped = malloc(5 * len + 1);
-    if (escaped == NULL) {
+    if (VIR_ALLOC_N(escaped, 5 * len + 1) < 0) {
         virBufferNoMemory(buf);
         return;
     }
@@ -316,14 +312,14 @@
         buf->content[buf->use] = 0;
         grow_size = (count > 1000) ? count : 1000;
         if (virBufferGrow(buf, grow_size) < 0) {
-            free(escaped);
+            VIR_FREE(escaped);
             return;
         }
         size = buf->size - buf->use - 1;
     }
     buf->use += count;
     buf->content[buf->use] = '\0';
-    free(escaped);
+    VIR_FREE(escaped);
 }
 
 /**
diff -r 797f37ce08c2 src/xend_internal.c
--- a/src/xend_internal.c	Wed May 21 22:22:48 2008 -0400
+++ b/src/xend_internal.c	Thu May 22 11:42:32 2008 -0400
@@ -924,6 +924,10 @@
             // and build with all available models
             if (STREQ(model, "all")) {
                 int i;
+                if (virBufferError(&buf)) {
+                    free(model);
+                    goto error;
+                }
                 free(virBufferContentAndReset(&buf));
 
                 for (i=0; i < sizeof(sound_models)/sizeof(*sound_models); ++i)

-- 
|: Red Hat, Engineering, Boston   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list