WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH 2 of 3] libxl: avoid multiple allocations in libxl_uu

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH 2 of 3] libxl: avoid multiple allocations in libxl_uuid2string
From: Ian Campbell <ian.campbell@xxxxxxxxxx>
Date: Fri, 13 Aug 2010 15:30:09 +0100
Cc: Ian Campbell <ian.campbell@xxxxxxxxxx>
Delivery-date: Fri, 13 Aug 2010 07:32:27 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <patchbomb.1281709807@xxxxxxxxxxxxxxxxxxxxx>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1281709509 -3600
# Node ID cf21a4ad0e798f76adb91453769a36e0856ccd12
# Parent  72d92574410da1d388dd33e5fe10e955fd1a93ec
libxl: avoid multiple allocations in libxl_uuid2string

The pointer returned by libxl_uuid2string is the callers
responsibility but the function currently allocates the string into
the current context and then duplicates the result.

Instead of allocating the memory twice (and immediately throwing one
away) just allocate the memory ourselves.

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r 72d92574410d -r cf21a4ad0e79 tools/libxl/libxl_dom.c
--- a/tools/libxl/libxl_dom.c   Fri Aug 13 15:25:08 2010 +0100
+++ b/tools/libxl/libxl_dom.c   Fri Aug 13 15:25:09 2010 +0100
@@ -444,17 +444,22 @@ int save_device_model(libxl_ctx *ctx, ui
 
 char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid)
 {
-    libxl_gc gc = LIBXL_INIT_GC(ctx);
-    char *s = string_of_uuid(&gc, uuid);
-    char *ret;
-    if (!s) {
-        XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate for uuid");
-        ret = NULL;
-    }else{
-        ret = strdup(s);
-    }
-    libxl_free_all(&gc);
-    return ret;
+    char *s;
+    int ret;
+
+    ret = snprintf(NULL, 0, UUID_FMT,
+                   uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], 
uuid[6], uuid[7],
+                   uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], 
uuid[14], uuid[15]);
+
+    if (ret < 0)
+        return NULL;
+
+    s = malloc(ret + 1);
+    snprintf(s, ret + 1, UUID_FMT,
+                   uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], 
uuid[6], uuid[7],
+                   uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], 
uuid[14], uuid[15]);
+
+    return s;
 }
 
 static const char *userdata_path(libxl_gc *gc, uint32_t domid,

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel