[libvirt] [PATCH 03/19] util: buffer: Simplify escape buffer allocations

Peter Krempa pkrempa at redhat.com
Thu Oct 24 13:56:21 UTC 2019


Replace combinations of xalloc_oversized and VIR_ALLOC_N_QUIET by using
g_malloc0_n which does the checking internally.

This conversion is done with a semantic difference and slightly higher
memory requirements as I've opted to allocate one chunk more than
necessary rather than trying to accomodate the NUL byte separately.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virbuffer.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
index a2b5aa8508..bcf9042573 100644
--- a/src/util/virbuffer.c
+++ b/src/util/virbuffer.c
@@ -467,11 +467,7 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
         return;
     }

-    if (xalloc_oversized(6, len) ||
-        VIR_ALLOC_N_QUIET(escaped, 6 * len + 1) < 0) {
-        virBufferSetError(buf, errno);
-        return;
-    }
+    escaped = g_malloc0_n(len + 1, 6);

     cur = str;
     out = escaped;
@@ -616,11 +612,7 @@ virBufferEscape(virBufferPtr buf, char escape, const char *toescape,
         return;
     }

-    if (xalloc_oversized(2, len) ||
-        VIR_ALLOC_N_QUIET(escaped, 2 * len + 1) < 0) {
-        virBufferSetError(buf, errno);
-        return;
-    }
+    escaped = g_malloc0_n(len + 1, 2);

     cur = str;
     out = escaped;
@@ -715,11 +707,8 @@ virBufferEscapeShell(virBufferPtr buf, const char *str)

     if (*str) {
         len = strlen(str);
-        if (xalloc_oversized(4, len) ||
-            VIR_ALLOC_N_QUIET(escaped, 4 * len + 3) < 0) {
-            virBufferSetError(buf, errno);
-            return;
-        }
+
+        escaped = g_malloc0_n(len + 1, 4);
     } else {
         virBufferAddLit(buf, "''");
         return;
-- 
2.21.0




More information about the libvir-list mailing list