From 4fe954c14851d2f913c41c581cbe49300b0984e4 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Tue, 21 Dec 2021 21:52:29 +0900 Subject: [PATCH] sed: do not ignore 'g' modifier when match starts with ^ It is perfectly valid to start a regex with ^ and have other patterns with \| that can match more than once, e.g. the following example should print ca, as illustrated with gnu sed: $ echo 'abca' | sed -e 's/^a\|b//g' ca busybox before patch: $ echo 'abca' | busybox sed -e 's/^a\|b//g' bca busybox after patch: $ echo 'abca' | ./busybox sed -e 's/^a\|b//g' ca regcomp handles ^ perfectly well as illustrated with the second 'a' that did not match in the example, we ca leave the non-repeating to it if appropriate. The check had been added before using regcomp and was required at the time (f36635cec6da) but no longer makes sense now. (tested with glibc and musl libc) function old new delta add_cmd 1189 1176 -13 Signed-off-by: Dominique Martinet Signed-off-by: Denys Vlasenko --- editors/sed.c | 3 +-- testsuite/sed.tests | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/editors/sed.c b/editors/sed.c index a6845a979..e8c82ac63 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -435,8 +435,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr) switch (substr[idx]) { /* Replace all occurrences */ case 'g': - if (match[0] != '^') - sed_cmd->which_match = 0; + sed_cmd->which_match = 0; break; /* Print pattern space */ case 'p': diff --git a/testsuite/sed.tests b/testsuite/sed.tests index 67ff87e93..2b78c9b12 100755 --- a/testsuite/sed.tests +++ b/testsuite/sed.tests @@ -399,6 +399,12 @@ testing "sed uses previous regexp" \ "" \ "q\nw\ne\nr\n" +testing "sed ^ OR not^" \ + "sed -e 's/^a\|b//g'" \ + "ca\n" \ + "" \ + "abca\n" + # testing "description" "commands" "result" "infile" "stdin" exit $FAILCOUNT