[PATCH] fix sed parsing bug

Rich Felker dalias at aerifal.cx
Sat Jul 22 20:51:58 PDT 2006


bb sed incorrectly considers brackets as special characters in the
replacement text in a s/regex/replacement/ command, resulting in parse
errors on valid sed programs such as:

's/enum \(.*\) {/let names_of_\1 = [|/'

(example taken from the ocaml build process). the attached patch
corrects the problem, perhaps not in the most graceful way, but with
minimal impact on size/performance.

rich

-------------- next part --------------
Index: editors/sed.c
===================================================================
--- editors/sed.c	(revision 15108)
+++ editors/sed.c	(working copy)
@@ -208,7 +208,7 @@
  * a backslash ('\').
  */
 static int index_of_next_unescaped_regexp_delim(const char delimiter,
-	const char *str)
+	const char *str, int bracket_char)
 {
 	int bracket = -1;
 	int escaped = 0;
@@ -224,7 +224,7 @@
 			escaped = 0;
 		else if (ch == '\\')
 			escaped = 1;
-		else if (ch == '[')
+		else if (ch == bracket_char)
 			bracket = idx;
 		else if (ch == delimiter)
 			return idx;
@@ -249,7 +249,7 @@
 	delimiter = *(cmdstr_ptr++);
 
 	/* save the match string */
-	idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
+	idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr, '[');
 	if (idx == -1) {
 		bb_error_msg_and_die(bad_format_in_subst);
 	}
@@ -257,7 +257,7 @@
 
 	/* save the replacement string */
 	cmdstr_ptr += idx + 1;
-	idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
+	idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr, 256);
 	if (idx == -1) {
 		bb_error_msg_and_die(bad_format_in_subst);
 	}
@@ -286,7 +286,7 @@
 
 		if (*my_str == '\\') delimiter = *(++pos);
 		else delimiter = '/';
-		next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
+		next = index_of_next_unescaped_regexp_delim(delimiter, ++pos, '[');
 		if (next == -1)
 			bb_error_msg_and_die("unterminated match expression");
 


More information about the busybox mailing list