sed: fix a buglet in s///NUM handling
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 15 Sep 2014 23:11:13 +0000 (01:11 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 15 Sep 2014 23:11:13 +0000 (01:11 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/sed.c
testsuite/sed.tests

index e18e48ab5ba0fc0265ee688456e5da9569de9186..e1b8352fd033b315b9f1ea24c92bba246fa371e2 100644 (file)
@@ -395,7 +395,9 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
        /* process the flags */
 
        sed_cmd->which_match = 1;
+       dbg("s flags:'%s'", substr + idx + 1);
        while (substr[++idx]) {
+               dbg("s flag:'%c'", substr[idx]);
                /* Parse match number */
                if (isdigit(substr[idx])) {
                        if (match[0] != '^') {
@@ -403,7 +405,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
                                const char *pos = substr + idx;
 /* FIXME: error check? */
                                sed_cmd->which_match = (unsigned)strtol(substr+idx, (char**) &pos, 10);
-                               idx = pos - substr;
+                               idx = pos - substr - 1;
                        }
                        continue;
                }
@@ -443,6 +445,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
                case '}':
                        goto out;
                default:
+                       dbg("s bad flags:'%s'", substr + idx);
                        bb_error_msg_and_die("bad option in substitution expression");
                }
        }
index 9494ac2deafcf0722ff1a7c11d9f862d0824ebfc..19f2915ced55e4152868e35b1df13a56122fb1c0 100755 (executable)
@@ -329,6 +329,10 @@ line with \\
 continuation
 "
 
+testing "sed s///NUM test" \
+       "sed -e 's/a/b/2; s/a/c/g'" \
+       "cb\n" "" "aa\n"
+
 # testing "description" "commands" "result" "infile" "stdin"
 
 exit $FAILCOUNT