From e9b5c6fe90fee50b61ab49a4b7b90553076d4ddf Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Wed, 12 Feb 2003 23:49:56 +0000
Subject: [PATCH] - winio.c:browser_init(), striponedir(), do_browse_from() -
 Various memory leak fixes (David Benbennick)

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1453 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog |  2 ++
 files.c   | 13 +++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bd34509d..70634397 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -52,6 +52,8 @@ CVS code
 	- Only goto_abort() if we *didnt* abort the command, making
 	  the function seem horribly misnamed ;-) (David Benbennick).
 - winio.c:
+  browser_init(), striponedir(), do_browse_from()
+	- Various memory leak fixes (David Benbennick).
   do_yesno(), do_help()
 	- Add defined(NCURSES_MOUSE_VERSION) to macro so systems that
 	  don't understand MEVENT will compile.
diff --git a/files.c b/files.c
index db4e5faa..40c25a7a 100644
--- a/files.c
+++ b/files.c
@@ -2396,13 +2396,15 @@ void striponedir(char *foo)
 {
     char *tmp;
 
+    assert(foo != NULL);
     /* Don't strip the root dir */
-    if (!strcmp(foo, "/"))
+    if (*foo == '\0' || strcmp(foo, "/") == 0)
 	return;
 
-    tmp = foo + strlen(foo);
+    tmp = foo + strlen(foo) - 1;
+    assert(tmp >= foo);
     if (*tmp == '/')
-	tmp--;
+	*tmp = '\0';
 
     while (*tmp != '/' && tmp != foo)
 	tmp--;
@@ -2464,6 +2466,7 @@ char **browser_init(const char *path, int *longest, int *numents)
 	sprintf(filelist[i], "%s/%s", path, next->d_name);
 	i++;
     }
+    closedir(dir);
 
     if (*longest > COLS - 1)
 	*longest = COLS - 1;
@@ -2841,8 +2844,10 @@ char *do_browse_from(const char *inpath)
 
     if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
 	striponedir(path);
-	if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
+	if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
+	    free(path);
 	    path = getcwd(NULL, PATH_MAX + 1);
+	}
     }
 
 #ifndef DISABLE_OPERATINGDIR
-- 
GitLab