svn commit: trunk/busybox/libbb

vda at busybox.net vda at busybox.net
Thu Sep 27 10:17:16 UTC 2007


Author: vda
Date: 2007-09-27 03:17:16 -0700 (Thu, 27 Sep 2007)
New Revision: 20060

Log:
lineedit: plug memory leak



Modified:
   trunk/busybox/libbb/lineedit.c


Changeset:
Modified: trunk/busybox/libbb/lineedit.c
===================================================================
--- trunk/busybox/libbb/lineedit.c	2007-09-27 10:12:02 UTC (rev 20059)
+++ trunk/busybox/libbb/lineedit.c	2007-09-27 10:17:16 UTC (rev 20060)
@@ -81,8 +81,9 @@
 #endif
 
 #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
-static char *user_buf = (char*)"";
-static char *home_pwd_buf = (char*)"";
+static const char null_str[] = "";
+static char *user_buf;
+static char *home_pwd_buf = (char*)null_str;
 #endif
 
 /* Put 'command_ps[cursor]', cursor++.
@@ -311,7 +312,7 @@
 
 	if (with_shash_flg) {           /* "~/..." or "~user/..." */
 		char *sav_ud = ud - 1;
-		char *home = 0;
+		char *home = NULL;
 		char *temp;
 
 		if (*ud == '/') {       /* "~/..."     */
@@ -1119,7 +1120,7 @@
 				switch (c) {
 #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
 				case 'u':
-					pbuf = user_buf;
+					pbuf = user_buf ? user_buf : (char*)"";
 					break;
 #endif
 				case 'h':
@@ -1143,7 +1144,7 @@
 				case 'w':
 					pbuf = pwd_buf;
 					l = strlen(home_pwd_buf);
-					if (home_pwd_buf[0] != 0
+					if (l != 0
 					 && strncmp(home_pwd_buf, pbuf, l) == 0
 					 && (pbuf[l]=='/' || pbuf[l]=='\0')
 					 && strlen(pwd_buf+l)<PATH_MAX
@@ -1310,8 +1311,14 @@
 
 		entry = getpwuid(geteuid());
 		if (entry) {
+			/* If we enter read_line_input for the Nth time,
+			 * they may be already allocated! Need to free. */
+			free(user_buf);
+			if (home_pwd_buf != null_str);
+				free(home_pwd_buf);
 			user_buf = xstrdup(entry->pw_name);
 			home_pwd_buf = xstrdup(entry->pw_dir);
+			/* They are not freed on exit (too small to bother) */
 		}
 	}
 #endif




More information about the busybox-cvs mailing list