[PATCH v2 2/2] sed: terminate branch commands at }

Jeremy Drake busybox at jdrake.com
Thu Mar 6 16:34:10 UTC 2025


in GNU sed, `echo foo | sed -e '/bar/{b};d'` is valid, but in busybox
sed would fail with unmatched {.
---
v2: maintain static const char[], use static const pointer into it to
minimize size, add test based on existing branch test

 editors/sed.c       | 5 +++--
 testsuite/sed.tests | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/editors/sed.c b/editors/sed.c
index 2c8231448..2f0aaf065 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -133,7 +133,8 @@ typedef struct sed_cmd_s {
 	char cmd;               /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
 } sed_cmd_t;

-static const char semicolon_whitespace[] ALIGN1 = "; \n\r\t\v";
+static const char closebrace_semicolon_whitespace[] ALIGN1 = "}; \n\r\t\v";
+static const char *const semicolon_whitespace = &closebrace_semicolon_whitespace[1];

 struct globals {
 	/* options */
@@ -613,7 +614,7 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
 		int length;

 		cmdstr = skip_whitespace(cmdstr);
-		length = strcspn(cmdstr, semicolon_whitespace);
+		length = strcspn(cmdstr, closebrace_semicolon_whitespace);
 		if (length) {
 			sed_cmd->string = xstrndup(cmdstr, length);
 			cmdstr += length;
diff --git a/testsuite/sed.tests b/testsuite/sed.tests
index 39fd4c772..4721e981e 100755
--- a/testsuite/sed.tests
+++ b/testsuite/sed.tests
@@ -64,6 +64,7 @@ testing "sed s with \\t (GNU ext)" "sed 's/\t/ /'" "one two" "" "one\ttwo"
 testing "sed b (branch)" "sed -e 'b one;p;: one'" "foo\n" "" "foo\n"
 testing "sed b (branch with no label jumps to end)" "sed -e 'b;p'" \
 	"foo\n" "" "foo\n"
+testing "sed b (label terminated by '}')" "sed -e '{bone};p;:one'" "foo\n" "" "foo\n"

 # test and branch
 testing "sed t (test/branch)" "sed -e 's/a/1/;t one;p;: one;p'" \
-- 
2.48.1.windows.1



More information about the busybox mailing list