]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Fix off-by-1 in virFileAbsPath.
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 22 Feb 2011 17:08:12 +0000 (17:08 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 23 Feb 2011 11:11:45 +0000 (11:11 +0000)
The virFileAbsPath was not taking into account the '/' directory
separator when allocating memory for combining cwd + path. Convert
to use virAsprintf to avoid this type of bug completely.

* src/util/util.c: Convert virFileAbsPath to use virAsprintf

src/util/util.c

index 965e96def8d03d92a8f8ba2df6602e99d3bd05b4..ee08d474002b495f35ab4f58862718f28d27327d 100644 (file)
@@ -1990,30 +1990,22 @@ cleanup:
 int virFileAbsPath(const char *path, char **abspath)
 {
     char *buf;
-    int cwdlen;
 
     if (path[0] == '/') {
-        buf = strdup(path);
-        if (buf == NULL)
-            return(-1);
+        if (!(*abspath = strdup(path)))
+            return -1;
     } else {
         buf = getcwd(NULL, 0);
         if (buf == NULL)
-            return(-1);
+            return -1;
 
-        cwdlen = strlen(buf);
-        /* cwdlen includes the null terminator */
-        if (VIR_REALLOC_N(buf, cwdlen + strlen(path) + 1) < 0) {
+        if (virAsprintf(abspath, "%s/%s", buf, path) < 0) {
             VIR_FREE(buf);
-            errno = ENOMEM;
-            return(-1);
+            return -1;
         }
-
-        buf[cwdlen] = '/';
-        strcpy(&buf[cwdlen + 1], path);
+        VIR_FREE(buf);
     }
 
-    *abspath = buf;
     return 0;
 }