From 68aab8641cb0e997a171783f99eb30c5fc85f1be Mon Sep 17 00:00:00 2001
From: Jussi Kivilinna <jussi.kivilinna@haltian.com>
Date: Tue, 13 Oct 2015 14:57:28 +0300
Subject: [PATCH] apps/nshlib: nsh_fscmds: fix corrupting one memory byte with
 'ls' and 'cp'

g_iobuffer[MAX_PATH] accesses memory beyond g_iobuffer is not large enough.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@haltian.com>
---
 apps/nshlib/nsh_fscmds.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/apps/nshlib/nsh_fscmds.c b/apps/nshlib/nsh_fscmds.c
index 25e2427b..299f11ca 100644
--- a/apps/nshlib/nsh_fscmds.c
+++ b/apps/nshlib/nsh_fscmds.c
@@ -147,19 +147,26 @@ static void trim_dir(char *arg)
 #if CONFIG_NFILE_DESCRIPTORS > 0
 static char *nsh_getdirpath(const char *path, const char *file)
 {
+  char *dirpath = NULL;
+  int ret;
+
   /* Handle the case where all that is left is '/' */
 
   if (strcmp(path, "/") == 0)
     {
-      sprintf(g_iobuffer, "/%s", file);
+      ret = asprintf(&dirpath, "/%s", file);
     }
   else
     {
-      sprintf(g_iobuffer, "%s/%s", path, file);
+      ret = asprintf(&dirpath, "%s/%s", path, file);
+    }
+
+  if (ret < 0)
+    {
+      dirpath = NULL;
     }
 
-  g_iobuffer[PATH_MAX] = '\0';
-  return strdup(g_iobuffer);
+  return dirpath;
 }
 #endif
 
-- 
GitLab