]> xenbits.xensource.com Git - libvirt.git/commitdiff
absolutePathFromBaseFile: don't leak when first arg contains no "/"
authorJim Meyering <meyering@redhat.com>
Thu, 4 Feb 2010 15:55:57 +0000 (16:55 +0100)
committerJim Meyering <meyering@redhat.com>
Fri, 5 Feb 2010 11:31:55 +0000 (12:31 +0100)
* src/util/storage_file.c: Include "dirname.h".
(absolutePathFromBaseFile): Rewrite not to leak, and to require
fewer allocations.
* bootstrap (modules): Add dirname-lgpl.
* .gnulib: Update submodule to the latest.

.gnulib
bootstrap
src/util/storage_file.c

diff --git a/.gnulib b/.gnulib
index 146d9145073e62a2096a2d6b33f75e93908fedf3..9d0ad652de159d08e5f679842f8a2a5658196361 160000 (submodule)
--- a/.gnulib
+++ b/.gnulib
@@ -1 +1 @@
-Subproject commit 146d9145073e62a2096a2d6b33f75e93908fedf3
+Subproject commit 9d0ad652de159d08e5f679842f8a2a5658196361
index cc3c6ef39259ce35e4b035b9b27df9f6369e81bb..113cc0f69774467ec9cb1747323e46f483fe014f 100755 (executable)
--- a/bootstrap
+++ b/bootstrap
@@ -71,6 +71,7 @@ c-ctype
 canonicalize-lgpl
 close
 connect
+dirname-lgpl
 getaddrinfo
 gethostname
 getpass
index 44057d2fceb481ee22a664ea8a048e5d00bb967c..2c79fa9e60b1399de700346d545f3580725a914a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * storage_file.c: file utility functions for FS storage backend
  *
- * Copyright (C) 2007-2009 Red Hat, Inc.
+ * Copyright (C) 2007-2010 Red Hat, Inc.
  * Copyright (C) 2007-2008 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -26,6 +26,7 @@
 
 #include <unistd.h>
 #include <fcntl.h>
+#include "dirname.h"
 #include "memory.h"
 #include "virterror_internal.h"
 
@@ -246,26 +247,15 @@ vmdk4GetBackingStore(virConnectPtr conn,
 static char *
 absolutePathFromBaseFile(const char *base_file, const char *path)
 {
-    size_t base_size, path_size;
-    char *res, *p;
+    char *res;
+    size_t d_len = dir_len (base_file);
 
-    if (*path == '/')
+    /* If path is already absolute, or if dirname(base_file) is ".",
+       just return a copy of path.  */
+    if (*path == '/' || d_len == 0)
         return strdup(path);
 
-    base_size = strlen(base_file) + 1;
-    path_size = strlen(path) + 1;
-    if (VIR_ALLOC_N(res, base_size - 1 + path_size) < 0)
-        return NULL;
-    memcpy(res, base_file, base_size);
-    p = strrchr(res, '/');
-    if (p != NULL)
-        p++;
-    else
-        p = res;
-    memcpy(p, path, path_size);
-    if (VIR_REALLOC_N(res, (p + path_size) - res) < 0) {
-        /* Ignore failure */
-    }
+    virAsprintf(&res, "%.*s/%s", base_file, d_len, path);
     return res;
 }