xenstore: In xenstore_client, avoid stack buffer in recursive function
authorIan Jackson <ian.jackson@eu.citrix.com>
Fri, 20 Jun 2014 10:54:56 +0000 (11:54 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Mon, 14 Jul 2014 15:07:50 +0000 (16:07 +0100)
do_ls is recursive.  It had a buffer of size around 5K allocated on
the stack.  This combination is not a very good idea: some
environments (eg, Mini-OS) have limited stack sizes (eg 64K).

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
tools/xenstore/xenstore_client.c

index 56b93092650cc81295e1480f8a249806bbf7c279..1054f18c71cfed926266b8c3781c611ac422207b 100644 (file)
@@ -128,11 +128,15 @@ static int show_whole_path = 0;
 static void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms)
 {
     char **e;
-    char newpath[STRING_MAX], *val;
+    char *newpath, *val;
     int newpath_len;
     int i;
     unsigned int num, len;
 
+    newpath = malloc(STRING_MAX);
+    if (!newpath)
+      err(1, "malloc in do_ls");
+
     e = xs_directory(h, XBT_NULL, path, &num);
     if (e == NULL)
         err(1, "xs_directory (%s)", path);
@@ -144,7 +148,7 @@ static void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms
         int linewid;
 
         /* Compose fullpath */
-        newpath_len = snprintf(newpath, sizeof(newpath), "%s%s%s", path, 
+        newpath_len = snprintf(newpath, STRING_MAX, "%s%s%s", path,
                 path[strlen(path)-1] == '/' ? "" : "/", 
                 e[i]);
 
@@ -161,7 +165,7 @@ static void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms
         }
 
        /* Fetch value */
-        if ( newpath_len < sizeof(newpath) ) {
+        if ( newpath_len < STRING_MAX ) {
             val = xs_read(h, XBT_NULL, newpath, &len);
         }
         else {
@@ -217,6 +221,7 @@ static void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms
         do_ls(h, newpath, cur_depth+1, show_perms); 
     }
     free(e);
+    free(newpath);
 }
 
 static void