[libvirt] [PATCH] Use virAsprintf, rather than VIR_ALLOC + strcpy + strcat

Cole Robinson crobinso at redhat.com
Mon May 18 17:57:49 UTC 2009


---
 qemud/remote.c                |    9 ++++-----
 src/remote_internal.c         |    7 ++-----
 src/storage_backend.c         |    9 +++------
 src/storage_backend_fs.c      |   29 +++++++++++++----------------
 src/storage_backend_logical.c |    9 ++++-----
 src/test.c                    |   18 ++++++------------
 6 files changed, 32 insertions(+), 49 deletions(-)

diff --git a/qemud/remote.c b/qemud/remote.c
index a92dea9..f9aa926 100644
--- a/qemud/remote.c
+++ b/qemud/remote.c
@@ -52,7 +52,9 @@
 #include "datatypes.h"
 #include "qemud.h"
 #include "memory.h"
+#include "util.h"
 
+#define VIR_FROM_THIS VIR_FROM_REMOTE
 #define REMOTE_DEBUG(fmt, ...) DEBUG(fmt, __VA_ARGS__)
 
 static void remoteDispatchFormatError (remote_error *rerr,
@@ -2602,14 +2604,11 @@ static char *addrToString(remote_error *rerr,
         return NULL;
     }
 
-    if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) {
-        remoteDispatchOOMError(rerr);
+    if (virAsprintf(&addr, "%s;%s", host, port) == -1) {
+        virReportOOMError(NULL);
         return NULL;
     }
 
-    strcpy(addr, host);
-    strcat(addr, ";");
-    strcat(addr, port);
     return addr;
 }
 
diff --git a/src/remote_internal.c b/src/remote_internal.c
index 1ca7784..542f4ad 100644
--- a/src/remote_internal.c
+++ b/src/remote_internal.c
@@ -5189,14 +5189,11 @@ static char *addrToString(struct sockaddr_storage *sa, socklen_t salen)
         return NULL;
     }
 
-    if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) {
-        virReportOOMError (NULL);
+    if (virAsprintf(&addr, "%s;%s", host, port) == -1) {
+        virReportOOMError(NULL);
         return NULL;
     }
 
-    strcpy(addr, host);
-    strcat(addr, ";");
-    strcat(addr, port);
     return addr;
 }
 
diff --git a/src/storage_backend.c b/src/storage_backend.c
index 2db314d..96cf37c 100644
--- a/src/storage_backend.c
+++ b/src/storage_backend.c
@@ -342,17 +342,14 @@ virStorageBackendStablePath(virConnectPtr conn,
         if (dent->d_name[0] == '.')
             continue;
 
-        if (VIR_ALLOC_N(stablepath, strlen(pool->def->target.path) +
-                        1 + strlen(dent->d_name) + 1) < 0) {
+        if (virAsprintf(&stablepath, "%s/%s",
+                        pool->def->target.path,
+                        dent->d_name) == -1) {
             virReportOOMError(conn);
             closedir(dh);
             return NULL;
         }
 
-        strcpy(stablepath, pool->def->target.path);
-        strcat(stablepath, "/");
-        strcat(stablepath, dent->d_name);
-
         if (virFileLinkPointsTo(stablepath, devpath)) {
             closedir(dh);
             return stablepath;
diff --git a/src/storage_backend_fs.c b/src/storage_backend_fs.c
index fac43df..ff1eac2 100644
--- a/src/storage_backend_fs.c
+++ b/src/storage_backend_fs.c
@@ -668,14 +668,13 @@ virStorageBackendFileSystemMount(virConnectPtr conn,
     }
 
     if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
-        if (VIR_ALLOC_N(src, strlen(pool->def->source.host.name) +
-                        1 + strlen(pool->def->source.dir) + 1) < 0) {
+        if (virAsprintf(&src, "%s:%s",
+                        pool->def->source.host.name,
+                        pool->def->source.dir) == -1) {
             virReportOOMError(conn);
             return -1;
         }
-        strcpy(src, pool->def->source.host.name);
-        strcat(src, ":");
-        strcat(src, pool->def->source.dir);
+
     } else {
         if ((src = strdup(pool->def->source.devices[0].path)) == NULL) {
             virReportOOMError(conn);
@@ -829,13 +828,11 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn,
 
         vol->type = VIR_STORAGE_VOL_FILE;
         vol->target.format = VIR_STORAGE_VOL_FILE_RAW; /* Real value is filled in during probe */
-        if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
-                        1 + strlen(vol->name) + 1) < 0)
+        if (virAsprintf(&vol->target.path, "%s/%s",
+                        pool->def->target.path,
+                        vol->name) == -1)
             goto no_memory;
 
-        strcpy(vol->target.path, pool->def->target.path);
-        strcat(vol->target.path, "/");
-        strcat(vol->target.path, vol->name);
         if ((vol->key = strdup(vol->target.path)) == NULL)
             goto no_memory;
 
@@ -995,15 +992,15 @@ virStorageBackendFileSystemVolCreate(virConnectPtr conn,
                                      virStorageVolDefPtr vol)
 {
 
-    if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
-                    1 + strlen(vol->name) + 1) < 0) {
+    vol->type = VIR_STORAGE_VOL_FILE;
+
+    if (virAsprintf(&vol->target.path, "%s/%s",
+                    pool->def->target.path,
+                    vol->name) == -1) {
         virReportOOMError(conn);
         return -1;
     }
-    vol->type = VIR_STORAGE_VOL_FILE;
-    strcpy(vol->target.path, pool->def->target.path);
-    strcat(vol->target.path, "/");
-    strcat(vol->target.path, vol->name);
+
     vol->key = strdup(vol->target.path);
     if (vol->key == NULL) {
         virReportOOMError(conn);
diff --git a/src/storage_backend_logical.c b/src/storage_backend_logical.c
index cbd2765..1bd00d4 100644
--- a/src/storage_backend_logical.c
+++ b/src/storage_backend_logical.c
@@ -594,14 +594,13 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
         /* A target path passed to CreateVol has no meaning */
         VIR_FREE(vol->target.path);
     }
-    if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
-                    1 + strlen(vol->name) + 1) < 0) {
+
+    if (virAsprintf(&vol->target.path, "%s/%s",
+                    pool->def->target.path,
+                    vol->name) == -1) {
         virReportOOMError(conn);
         return -1;
     }
-    strcpy(vol->target.path, pool->def->target.path);
-    strcat(vol->target.path, "/");
-    strcat(vol->target.path, vol->name);
 
     if (virRun(conn, cmdargv, NULL) < 0)
         return -1;
diff --git a/src/test.c b/src/test.c
index f5ec6ec..6cb5425 100644
--- a/src/test.c
+++ b/src/test.c
@@ -3107,16 +3107,13 @@ testStorageVolumeCreateXML(virStoragePoolPtr pool,
         goto cleanup;
     }
 
-    if (VIR_ALLOC_N(privvol->target.path,
-                    strlen(privpool->def->target.path) +
-                    1 + strlen(privvol->name) + 1) < 0) {
+    if (virAsprintf(&privvol->target.path, "%s/%s",
+                    privpool->def->target.path,
+                    privvol->name) == -1) {
         virReportOOMError(pool->conn);
         goto cleanup;
     }
 
-    strcpy(privvol->target.path, privpool->def->target.path);
-    strcat(privvol->target.path, "/");
-    strcat(privvol->target.path, privvol->name);
     privvol->key = strdup(privvol->target.path);
     if (privvol->key == NULL) {
         virReportOOMError(pool->conn);
@@ -3201,16 +3198,13 @@ testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
         goto cleanup;
     }
 
-    if (VIR_ALLOC_N(privvol->target.path,
-                    strlen(privpool->def->target.path) +
-                    1 + strlen(privvol->name) + 1) < 0) {
+    if (virAsprintf(&privvol->target.path, "%s/%s",
+                    privpool->def->target.path,
+                    privvol->name) == -1) {
         virReportOOMError(pool->conn);
         goto cleanup;
     }
 
-    strcpy(privvol->target.path, privpool->def->target.path);
-    strcat(privvol->target.path, "/");
-    strcat(privvol->target.path, privvol->name);
     privvol->key = strdup(privvol->target.path);
     if (privvol->key == NULL) {
         virReportOOMError(pool->conn);
-- 
1.6.2.2




More information about the libvir-list mailing list