svn commit: trunk/busybox/shell

vda at busybox.net vda at busybox.net
Thu Apr 12 17:08:54 UTC 2007


Author: vda
Date: 2007-04-12 10:08:53 -0700 (Thu, 12 Apr 2007)
New Revision: 18417

Log:
fix realloc-of-non-malloced pointer, and reduce size while at it


Modified:
   trunk/busybox/shell/hush.c
   trunk/busybox/shell/lash.c


Changeset:
Modified: trunk/busybox/shell/hush.c
===================================================================
--- trunk/busybox/shell/hush.c	2007-04-12 12:31:02 UTC (rev 18416)
+++ trunk/busybox/shell/hush.c	2007-04-12 17:08:53 UTC (rev 18417)
@@ -423,7 +423,7 @@
 static const char *set_cwd(void)
 {
 	if (cwd == bb_msg_unknown)
-		cwd = NULL;     /* xrealloc_getcwd_or_warn(arg) called free(arg) */
+		cwd = NULL;     /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
 	cwd = xrealloc_getcwd_or_warn((char *)cwd);
 	if (!cwd)
 		cwd = bb_msg_unknown;

Modified: trunk/busybox/shell/lash.c
===================================================================
--- trunk/busybox/shell/lash.c	2007-04-12 12:31:02 UTC (rev 18416)
+++ trunk/busybox/shell/lash.c	2007-04-12 17:08:53 UTC (rev 18417)
@@ -153,7 +153,7 @@
 
 
 /* Globals that are static to this file */
-static const char *cwd;
+static char *cwd;
 static char *local_pending_command;
 static struct jobset job_list = { NULL, NULL };
 static int argc;
@@ -207,6 +207,14 @@
      job_list          becomes  child->family->job_list
  */
 
+
+static void update_cwd(void)
+{
+	cwd = xrealloc_getcwd_or_warn(cwd);
+	if (!cwd)
+		cwd = xstrdup(bb_msg_unknown);
+}
+
 /* built-in 'cd <path>' handler */
 static int builtin_cd(struct child_prog *child)
 {
@@ -220,9 +228,7 @@
 		bb_perror_msg("cd: %s", newdir);
 		return EXIT_FAILURE;
 	}
-	cwd = xrealloc_getcwd_or_warn((char *)cwd);
-	if (!cwd)
-		cwd = bb_msg_unknown;
+	update_cwd();
 	return EXIT_SUCCESS;
 }
 
@@ -347,9 +353,7 @@
 /* built-in 'pwd' handler */
 static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
 {
-	cwd = xrealloc_getcwd_or_warn((char *)cwd);
-	if (!cwd)
-		cwd = bb_msg_unknown;
+	update_cwd();
 	puts(cwd);
 	return EXIT_SUCCESS;
 }
@@ -1452,9 +1456,7 @@
 #if ENABLE_FEATURE_CLEAN_UP
 static void free_memory(void)
 {
-	if (cwd && cwd != bb_msg_unknown) {
-		free((char*)cwd);
-	}
+	free(cwd);
 
 	if (job_list.fg && !job_list.fg->running_progs) {
 		remove_job(&job_list, job_list.fg);
@@ -1571,14 +1573,12 @@
 	}
 
 	/* initialize the cwd -- this is never freed...*/
-	cwd = xrealloc_getcwd_or_warn(NULL);
-	if (!cwd)
-		cwd = bb_msg_unknown;
+	update_cwd();
 
 	if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
 
 	if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt();
 	else PS1 = NULL;
 
-	return (busy_loop(input));
+	return busy_loop(input);
 }




More information about the busybox-cvs mailing list