[BusyBox] fix sh memory leak

Matt Kraai kraai at alumni.carnegiemellon.edu
Wed Dec 13 01:17:37 UTC 2000


On Tue, Dec 12, 2000 at 04:43:20PM -0700, Erik Andersen wrote:
> On Tue Dec 12, 2000 at 02:09:30PM -0700, Matt Kraai wrote:
> > @@ -678,7 +678,6 @@
> >  
> >  	/* get User Name and setup prompt */
> >  	strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# ");
> > -	user=xcalloc(sizeof(int), 9);
> >  	my_getpwuid(user, geteuid());
> 
> Ok, I see.  This is a result of the fix to my_getpwuid so it now NULL
> terminates strings.  I'm reminded that any users of my_get* that were working
> before probably have such code in them as well.  <sound of looking>  yup.
> Does the following look ok to you?

Rather than setting the buffer to the empty string before each
call (and forgetting some, like my patch for sh.c :), why not just
drop it into my_getpwuid and my_getgrgid and be done with it?
A patch is attached (which also moves a bunch of buffers from the
stack to the heap and fixes bugs in logname and whoami).

Matt
-------------- next part --------------
Index: id.c
===================================================================
RCS file: /var/cvs/busybox/id.c,v
retrieving revision 1.12
diff -u -r1.12 id.c
--- id.c	2000/12/07 19:56:48	1.12
+++ id.c	2000/12/13 01:12:43
@@ -31,12 +31,11 @@
 extern int id_main(int argc, char **argv)
 {
 	int no_user = 0, no_group = 0, print_real = 0;
-	char *cp, *user, *group;
+	char user[9], group[9];
 	long gid;
 	long pwnam, grnam;
 	int opt;
 	
-	cp = user = group = NULL;
 	gid = 0;
 
 	while ((opt = getopt(argc, argv, "ugr")) > 0) {
@@ -57,11 +56,7 @@
 
 	if (no_user && no_group) usage(id_usage);
 
-	user = argv[optind];
-
-	if (user == NULL) {
-		user = xcalloc(9, sizeof(char));
-		group = xcalloc(9, sizeof(char));
+	if (argv[optind] == NULL) {
 		if (print_real) {
 			my_getpwuid(user, getuid());
 			my_getgrgid(group, getgid());
@@ -70,7 +65,8 @@
 			my_getgrgid(group, getegid());
 		}
 	} else {
-		group = xcalloc(9, sizeof(char));
+		strncpy(user, argv[optind], 8);
+		user[8] = '\0';
 	    gid = my_getpwnamegid(user);
 		my_getgrgid(group, gid);
 	}
Index: logname.c
===================================================================
RCS file: /var/cvs/busybox/logname.c,v
retrieving revision 1.10
diff -u -r1.10 logname.c
--- logname.c	2000/12/07 19:56:48	1.10
+++ logname.c	2000/12/13 01:12:48
@@ -25,13 +25,13 @@
 
 extern int logname_main(int argc, char **argv)
 {
-	char *user = xmalloc(9);
+	char user[9];
 
 	if (argc > 1)
 		usage(logname_usage);
 
 	my_getpwuid(user, geteuid());
-	if (user) {
+	if (*user) {
 		puts(user);
 		return EXIT_SUCCESS;
 	}
Index: ls.c
===================================================================
RCS file: /var/cvs/busybox/ls.c,v
retrieving revision 1.47
diff -u -r1.47 ls.c
--- ls.c	2000/12/07 19:56:48	1.47
+++ ls.c	2000/12/13 01:13:01
@@ -600,13 +600,11 @@
 				break;
 			case LIST_ID_NAME:
 #ifdef BB_FEATURE_LS_USERNAME
-				memset(scratch, 0, sizeof(scratch));
 				my_getpwuid(scratch, dn->dstat.st_uid);
 				if (*scratch)
 					fprintf(stdout, "%-8.8s ", scratch);
 				else
 					fprintf(stdout, "%-8d ", dn->dstat.st_uid);
-				memset(scratch, 0, sizeof(scratch));
 				my_getgrgid(scratch, dn->dstat.st_gid);
 				if (*scratch)
 					fprintf(stdout, "%-8.8s", scratch);
Index: ps.c
===================================================================
RCS file: /var/cvs/busybox/ps.c,v
retrieving revision 1.28
diff -u -r1.28 ps.c
--- ps.c	2000/12/07 19:56:48	1.28
+++ ps.c	2000/12/13 01:13:06
@@ -121,8 +121,8 @@
 	FILE *file;
 	struct dirent *entry;
 	char path[32], sbuf[512];
-	char uidName[10] = "";
-	char groupName[10] = "";
+	char uidName[9];
+	char groupName[9];
 	int len, i, c;
 #ifdef BB_FEATURE_AUTOWIDTH
 	struct winsize win = { 0, 0, 0, 0 };
@@ -146,9 +146,6 @@
 	fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
 			"State", "Command");
 	while ((entry = readdir(dir)) != NULL) {
-		uidName[0] = '\0';
-		groupName[0] = '\0';
-
 		if (!isdigit(*entry->d_name))
 			continue;
 		sprintf(path, "/proc/%s/status", entry->d_name);
@@ -204,8 +201,8 @@
 	pid_t num_pids;
 	pid_t* pid_array = NULL;
 	struct pid_info info;
-	char uidName[10] = "";
-	char groupName[10] = "";
+	char uidName[9];
+	char groupName[9];
 #ifdef BB_FEATURE_AUTOWIDTH
 	struct winsize win = { 0, 0, 0, 0 };
 	int terminal_width = TERMINAL_WIDTH;
@@ -247,8 +244,6 @@
 			"State", "Command");
 
 	for (i=1; i<pid_array[0] ; i++) {
-		uidName[0] = '\0';
-		groupName[0] = '\0';
 	    info.pid = pid_array[i];
 
 	    if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Index: tar.c
===================================================================
RCS file: /var/cvs/busybox/tar.c,v
retrieving revision 1.79
diff -u -r1.79 tar.c
--- tar.c	2000/12/10 01:57:30	1.79
+++ tar.c	2000/12/13 01:13:32
@@ -691,13 +691,11 @@
 				struct tm *tm = localtime (&(header.mtime));
 
 				len=printf("%s ", mode_string(header.mode));
-				memset(buf, 0, 8*sizeof(char));
 				my_getpwuid(buf, header.uid);
 				if (! *buf)
 					len+=printf("%d", header.uid);
 				else
 					len+=printf("%s", buf);
-				memset(buf, 0, 8*sizeof(char));
 				my_getgrgid(buf, header.gid);
 				if (! *buf)
 					len+=printf("/%-d ", header.gid);
Index: utility.c
===================================================================
RCS file: /var/cvs/busybox/utility.c,v
retrieving revision 1.172
diff -u -r1.172 utility.c
--- utility.c	2000/12/12 23:13:54	1.172
+++ utility.c	2000/12/13 01:13:57
@@ -957,12 +957,14 @@
 /* gets a username given a uid */
 void my_getpwuid(char *name, long uid)
 {
+	name[0] = '\0';
 	my_getid("/etc/passwd", name, uid, NULL);
 }
 
 /* gets a groupname given a gid */
 void my_getgrgid(char *group, long gid)
 {
+	group[0] = '\0';
 	my_getid("/etc/group", group, gid, NULL);
 }
 
Index: whoami.c
===================================================================
RCS file: /var/cvs/busybox/whoami.c,v
retrieving revision 1.12
diff -u -r1.12 whoami.c
--- whoami.c	2000/12/07 19:56:48	1.12
+++ whoami.c	2000/12/13 01:13:57
@@ -26,14 +26,14 @@
 
 extern int whoami_main(int argc, char **argv)
 {
-	char *user = xmalloc(9);
+	char user[9];
 	uid_t uid = geteuid();
 
 	if (argc > 1)
 		usage(whoami_usage);
 
 	my_getpwuid(user, uid);
-	if (user) {
+	if (*user) {
 		puts(user);
 		return EXIT_SUCCESS;
 	}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20001212/db2ba30a/attachment.pgp 


More information about the busybox mailing list