From: Ian Jackson Subject: [PATCH 02/11] xenstore: In xenstore_client, avoid stack buffer in recursive function Date: Thu, 10 Jul 2014 17:14:31 +0100 Message-ID: <1405008880-21843-3-git-send-email-ian.jackson@eu.citrix.com> References: <1405008880-21843-1-git-send-email-ian.jackson@eu.citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1405008880-21843-1-git-send-email-ian.jackson@eu.citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xensource.com Cc: Ian Jackson , Ian Campbell List-Id: xen-devel@lists.xenproject.org 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 Acked-by: Ian Campbell --- tools/xenstore/xenstore_client.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/xenstore/xenstore_client.c b/tools/xenstore/xenstore_client.c index 56b9309..1054f18 100644 --- a/tools/xenstore/xenstore_client.c +++ b/tools/xenstore/xenstore_client.c @@ -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 -- 1.7.10.4