[BusyBox] [PATCH] Changes strdup to xstrdup

Steve Merrifield steve at labyrinth.net.au
Sat Nov 10 19:19:48 UTC 2001


Here is a patch that updates strdup() to xstrdup().
 
It only modifies those files where memory was not previously allocated,
or there was no NULL checking. For example, in the case of:
 
char *temp;
temp = strdup();
 
This is now:
 
char *temp;
temp = xstrdup();
 
However, things like this have not been changed:
 
struct name *temp;
temp = xmalloc();
temp->var = strdup();
 
Likewise, things like this have not been changed:
 
char *temp;
temp = strdup();
if (temp == NULL) {...}
 
Diffed against CVS, 2001-11-11
 
steve
-------------- next part --------------
diff -Naur busybox-orig/archival/bunzip2.c busybox/archival/bunzip2.c
--- busybox-orig/archival/bunzip2.c	Fri Oct  5 13:48:57 2001
+++ busybox/archival/bunzip2.c	Sun Nov 11 11:11:50 2001
@@ -2324,7 +2324,7 @@
 		show_usage();
 	}
 	src_stream = xfopen(argv[1], "r");
-	save_name = strdup(argv[1]);
+	save_name = xstrdup(argv[1]);
 	save_name_ptr = strrchr(save_name, '.');
 	if (save_name_ptr == NULL) {
 		return(FALSE);
diff -Naur busybox-orig/archival/gunzip.c busybox/archival/gunzip.c
--- busybox-orig/archival/gunzip.c	Fri Jul 13 16:43:03 2001
+++ busybox/archival/gunzip.c	Sun Nov 11 11:12:14 2001
@@ -114,7 +114,7 @@
 	if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
 		flags |= gunzip_to_stdout;
 	} else {
-		if_name = strdup(argv[optind]);
+		if_name = xstrdup(argv[optind]);
 		/* Open input file */
 		in_file = xfopen(if_name, "r");
 
diff -Naur busybox-orig/editors/sed.c busybox/editors/sed.c
--- busybox-orig/editors/sed.c	Sun Nov 11 09:50:30 2001
+++ busybox/editors/sed.c	Sun Nov 11 11:12:46 2001
@@ -173,7 +173,7 @@
  */
 static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, regex_t **regex)
 {
-	char *my_str = strdup(str);
+	char *my_str = xstrdup(str);
 	int idx = 0;
 	char olddelimiter;
 	olddelimiter = sed_cmd->delimiter;
diff -Naur busybox-orig/editors/vi.c busybox/editors/vi.c
--- busybox-orig/editors/vi.c	Wed Oct 24 14:59:23 2001
+++ busybox/editors/vi.c	Sun Nov 11 11:13:33 2001
@@ -385,7 +385,7 @@
 			editing = 1;	// 0=exit, 1=one file, 2+ =many files
 			if (cfn != 0)
 				free(cfn);
-			cfn = (Byte *) strdup(argv[optind]);
+			cfn = (Byte *) xstrdup(argv[optind]);
 			edit_file(cfn);
 		}
 	}
@@ -1085,7 +1085,7 @@
 		// Stuff the last_modifying_cmd back into stdin
 		// and let it be re-executed.
 		if (last_modifying_cmd != 0) {
-			ioq = ioq_start = (Byte *) strdup((char *) last_modifying_cmd);
+			ioq = ioq_start = (Byte *) xstrdup((char *) last_modifying_cmd);
 		}
 		break;
 #endif							/* CONFIG_FEATURE_VI_DOT_CMD */
@@ -1102,7 +1102,7 @@
 			if (last_search_pattern != 0) {
 				free(last_search_pattern);
 			}
-			last_search_pattern = (Byte *) strdup((char *) q);
+			last_search_pattern = (Byte *) xstrdup((char *) q);
 			goto dc3;	// now find the pattern
 		}
 		// user changed mind and erased the "/"-  do nothing
@@ -1655,7 +1655,7 @@
 			*q++ = *p;
 			*q = '\0';
 		}
-		pat = (Byte *) strdup((char *) buf);	// save copy of pattern
+		pat = (Byte *) xstrdup((char *) buf);	// save copy of pattern
 		if (*p == '/')
 			p++;
 		q = char_search(dot, pat, FORWARD, FULL);
@@ -1852,7 +1852,7 @@
 
 		// There is a read-able regular file
 		// make this the current file
-		q = (Byte *) strdup((char *) fn);	// save the cfn
+		q = (Byte *) xstrdup((char *) fn);	// save the cfn
 		if (cfn != 0)
 			free(cfn);		// free the old name
 		cfn = q;			// remember new cfn
@@ -1905,7 +1905,7 @@
 			// user wants a new filename
 			if (cfn != NULL)
 				free(cfn);
-			cfn = (Byte *) strdup((char *) args);
+			cfn = (Byte *) xstrdup((char *) args);
 		} else {
 			// user wants file status info
 			edit_status();
@@ -3465,7 +3465,7 @@
 	refresh(FALSE);
 	if (obufp != NULL)
 		free(obufp);
-	obufp = (Byte *) strdup((char *) buf);
+	obufp = (Byte *) xstrdup((char *) buf);
 	return (obufp);
 }
 
diff -Naur busybox-orig/findutils/grep.c busybox/findutils/grep.c
--- busybox-orig/findutils/grep.c	Sun Nov 11 09:50:31 2001
+++ busybox/findutils/grep.c	Sun Nov 11 12:06:57 2001
@@ -155,7 +155,7 @@
 				if(lines_before) {
 					if(before_buf[curpos])
 						free(before_buf[curpos]);
-					before_buf[curpos] = strdup(line);
+					before_buf[curpos] = xstrdup(line);
 					curpos = (curpos + 1) % lines_before;
 				}
 			}
@@ -306,13 +306,13 @@
 				lines_before = strtoul(optarg, &junk, 10);
 				if(*junk != '\0')
 					error_msg_and_die("invalid context length argument");
-				before_buf = (char **)calloc(lines_before, sizeof(char *));
+				before_buf = (char **)xcalloc(lines_before, sizeof(char *));
 				break;
 			case 'C':
 				lines_after = lines_before = strtoul(optarg, &junk, 10);
 				if(*junk != '\0')
 					error_msg_and_die("invalid context length argument");
-				before_buf = (char **)calloc(lines_before, sizeof(char *));
+				before_buf = (char **)xcalloc(lines_before, sizeof(char *));
 				break;
 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
 			default:
diff -Naur busybox-orig/modutils/insmod.c busybox/modutils/insmod.c
--- busybox-orig/modutils/insmod.c	Wed Oct 24 14:59:54 2001
+++ busybox/modutils/insmod.c	Sun Nov 11 11:14:54 2001
@@ -724,7 +724,7 @@
 	if (fullname[0] == '\0')
 		return (FALSE);
 	else {
-		char *tmp, *tmp1 = strdup(filename);
+		char *tmp, *tmp1 = xstrdup(filename);
 		tmp = get_last_path_component(tmp1);
 		if (strcmp(tmp, fullname) == 0) {
 			free(tmp1);
diff -Naur busybox-orig/shell/ash.c busybox/shell/ash.c
--- busybox-orig/shell/ash.c	Wed Oct 31 22:05:49 2001
+++ busybox/shell/ash.c	Sun Nov 11 12:10:04 2001
@@ -12092,7 +12092,7 @@
 			vpp = hashvar(ip->text);
 			vp->next = *vpp;
 			*vpp = vp;
-			vp->text = strdup(ip->text);
+			vp->text = xstrdup(ip->text);
 			vp->flags = ip->flags;
 			vp->func = ip->func;
 		}
@@ -12104,7 +12104,7 @@
 		vpp = hashvar("PS1=");
 		vps1.next = *vpp;
 		*vpp = &vps1;
-		vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
+		vps1.text = xstrdup(geteuid() ? "PS1=$ " : "PS1=# ");
 		vps1.flags = VSTRFIXED|VTEXTFIXED;
 	}
 }
diff -Naur busybox-orig/shell/lash.c busybox/shell/lash.c
--- busybox-orig/shell/lash.c	Sun Oct 28 16:12:19 2001
+++ busybox/shell/lash.c	Sun Nov 11 12:12:30 2001
@@ -820,7 +820,7 @@
 
 	/* We need a clean copy, so strsep can mess up the copy while
 	 * we write stuff into the original (in a minute) */
-	cmd = cmd_copy = strdup(command);
+	cmd = cmd_copy = xstrdup(command);
 	*command = '\0';
 	for (ix = 0, tmpcmd = cmd; 
 			(tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
diff -Naur busybox-orig/sysklogd/syslogd.c busybox/sysklogd/syslogd.c
--- busybox-orig/sysklogd/syslogd.c	Sun Oct 28 16:12:20 2001
+++ busybox/sysklogd/syslogd.c	Sun Nov 11 11:11:25 2001
@@ -589,11 +589,11 @@
 				doFork = FALSE;
 				break;
 			case 'O':
-				logFilePath = strdup(optarg);
+				logFilePath = xstrdup(optarg);
 				break;
 #ifdef CONFIG_FEATURE_REMOTE_LOG
 			case 'R':
-				RemoteHost = strdup(optarg);
+				RemoteHost = xstrdup(optarg);
 				if ( (p = strchr(RemoteHost, ':'))){
 					RemotePort = atoi(p+1);
 					*p = '\0';
diff -Naur busybox-orig/util-linux/mount.c busybox/util-linux/mount.c
--- busybox-orig/util-linux/mount.c	Wed Oct 24 15:00:29 2001
+++ busybox/util-linux/mount.c	Sun Nov 11 11:44:06 2001
@@ -463,9 +463,9 @@
 			
 			strcpy(device, m->mnt_fsname);
 			strcpy(directory, m->mnt_dir);
-			filesystemType = strdup(m->mnt_type);
+			filesystemType = xstrdup(m->mnt_type);
 singlemount:			
-			string_flags = strdup(string_flags);
+			string_flags = xstrdup(string_flags);
 			rc = EXIT_SUCCESS;
 #ifdef CONFIG_NFSMOUNT
 			if (strchr(device, ':') != NULL)


More information about the busybox mailing list