[BusyBox-cvs] busybox/editors sed.c,1.139,1.140

Glenn McGrath bug1 at busybox.net
Sun Sep 14 15:24:21 UTC 2003


Update of /var/cvs/busybox/editors
In directory winder:/tmp/cvs-serv28093/editors

Modified Files:
	sed.c 
Log Message:
Cleanup memory usage


Index: sed.c
===================================================================
RCS file: /var/cvs/busybox/editors/sed.c,v
retrieving revision 1.139
retrieving revision 1.140
diff -u -d -r1.139 -r1.140
--- sed.c	14 Sep 2003 08:52:53 -0000	1.139
+++ sed.c	14 Sep 2003 15:24:18 -0000	1.140
@@ -153,6 +153,10 @@
 			free(sed_cmd->sub_match);
 		}
 		free(sed_cmd->replace);
+		free(sed_cmd->editline);
+		free(sed_cmd->filename);
+		free(sed_cmd->translate);
+		free(sed_cmd->label);
 		free(sed_cmd);
 		sed_cmd = sed_cmd_next;
 	}
@@ -803,7 +807,6 @@
 
 		/* Read one line in advance so we can act on the last line, the '$' address */
 		next_line = bb_get_chomped_line_from_file(file);
-
 		linenum++;
 		altered = 0;
 		force_print = 0;
@@ -981,10 +984,12 @@
 					next_line = NULL;
 					break;
 				case 'n':	/* Read next line from input */
-					free(pattern_space);
-					pattern_space = next_line;
-					next_line = bb_get_chomped_line_from_file(file);
-					linenum++;
+					if (next_line) {
+						free(pattern_space);
+						pattern_space = next_line;
+						next_line = bb_get_chomped_line_from_file(file);
+						linenum++;
+					}
 					break;
 				case 'N':	/* Append the next line to the current line */
 					if (next_line) {
@@ -1043,17 +1048,25 @@
 					hold_space = strdup(pattern_space);
 					break;
 				case 'H': {	/* Append newline and pattern space to hold space */
-					int hold_space_size = 0;
+					int hold_space_size = 2;
+					int pattern_space_size = 0;
+
 					if (hold_space) {
-						hold_space_size = strlen(hold_space);
+						hold_space_size += strlen(hold_space);
 					}
-					hold_space = xrealloc(hold_space, hold_space_size + strlen(pattern_space) + 2);
-					if (hold_space_size) {
-						strcat(hold_space, "\n");
+					if (pattern_space) {
+						pattern_space_size = strlen(pattern_space);
+					}
+					hold_space = xrealloc(hold_space, hold_space_size + pattern_space_size);
+
+					if (hold_space_size == 2) {
+						strcpy(hold_space, "\n");
 					} else {
-						hold_space[0] = '\n';
+						strcat(hold_space, "\n");
+					}
+					if (pattern_space) {
+						strcat(hold_space, pattern_space);
 					}
-					strcat(hold_space, pattern_space); 
 					break;
 				}
 				case 'x':{
@@ -1116,11 +1129,13 @@
 {
 	int opt, status = EXIT_SUCCESS;
 
+#if 0 /* This doesnt seem to be working */
 #ifdef CONFIG_FEATURE_CLEAN_UP
 	/* destroy command strings on exit */
 	if (atexit(destroy_cmd_strs) == -1)
 		bb_perror_msg_and_die("atexit");
 #endif
+#endif
 
 	/* do normal option parsing */
 	while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
@@ -1129,10 +1144,7 @@
 			be_quiet++;
 			break;
 		case 'e':{
-			char *str_cmd = strdup(optarg);
-
-			add_cmd_str(str_cmd);
-			free(str_cmd);
+			add_cmd_str(optarg);
 			break;
 		}
 		case 'f':
@@ -1149,7 +1161,7 @@
 		if (argv[optind] == NULL)
 			bb_show_usage();
 		else
-			add_cmd_str(strdup(argv[optind++]));
+			add_cmd_str(argv[optind++]);
 	}
 
 	/* argv[(optind)..(argc-1)] should be names of file to process. If no
@@ -1176,5 +1188,8 @@
 		}
 	}
 
+#ifdef CONFIG_FEATURE_CLEAN_UP
+	destroy_cmd_strs();
+#endif	
 	return status;
 }




More information about the busybox-cvs mailing list