From a5bf5415619d672d0d8c9d7484bc824eb7388aa7 Mon Sep 17 00:00:00 2001
From: root <root@gabhart-ubuntu.gabhart-ubuntu.d2.internal.cloudapp.net>
Date: Fri, 26 Jun 2015 18:42:29 +0000
Subject: [PATCH] Fixed URI handling leak

---
 fish/uri.c | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/fish/uri.c b/fish/uri.c
index 1566cdf..f6f13b3 100644
--- a/fish/uri.c
+++ b/fish/uri.c
@@ -105,7 +105,6 @@ parse (const char *arg, char **path_ret, char **protocol_ret,
 {
   CLEANUP_XMLFREEURI xmlURIPtr uri = NULL;
   CLEANUP_FREE char *socket = NULL;
-  char *path;
 
   uri = xmlParseURI (arg);
   if (!uri) {
@@ -178,28 +177,44 @@ parse (const char *arg, char **path_ret, char **protocol_ret,
     }
   }
 
-  if (asprintf(&path, "%s?%s", uri->path, uri->query_raw) == -1) {
-    perror ("asprintf: path + query_raw");
-    free (*protocol_ret);
-    guestfs_int_free_string_list (*server_ret);
-    free (*username_ret);
-    free (*password_ret);
-    return -1;
-  }
-
   /* We may have to adjust the path depending on the protocol.  For
    * example ceph/rbd URIs look like rbd:///pool/disk, but the
    * exportname expected will be "pool/disk".  Here, uri->path will be
    * "/pool/disk" so we have to knock off the leading '/' character.
    */
-  if (path && path[0] == '/' &&
+  char *tmpPath = uri->path;
+  if (tmpPath && tmpPath[0] == '/' &&
       (STREQ (uri->scheme, "gluster") ||
        STREQ (uri->scheme, "iscsi") ||
        STREQ (uri->scheme, "rbd") ||
        STREQ (uri->scheme, "sheepdog")))
-    path++;
+    tmpPath++;
+
+  int pathGenRes = -1;
+  if (tmpPath) {
+    if (uri->query_raw) {
+      pathGenRes = asprintf(path_ret, "%s?%s", tmpPath, uri->query_raw);
+    } else {
+      pathGenRes = asprintf(path_ret, "%s", tmpPath);
+    }
+  } else {
+    *path_ret = strdup(""); 
+    pathGenRes = 0;
+  }
+
+  if (pathGenRes == -1) {
+    perror ("asprintf: path + query_raw");
+    if (*path_ret) {
+      free (*path_ret);
+    }
+
+    free (*protocol_ret);
+    guestfs_int_free_string_list (*server_ret);
+    free (*username_ret);
+    free (*password_ret);
+    return -1;
+  }
 
-  *path_ret = strdup (path ? path : "");
   return 0;
 }
 
-- 
1.9.1

