diff options
author | Matt Kraai | 2001-08-20 21:21:06 +0000 |
---|---|---|
committer | Matt Kraai | 2001-08-20 21:21:06 +0000 |
commit | a3e4f455ac0265d9f2477300054f504f563b0c58 (patch) | |
tree | 96448b8b00f4d257a9820963635044ef93935d75 | |
parent | 54eceffcae3ebf1664e54ba2b32298856a2c4930 (diff) | |
download | busybox-a3e4f455ac0265d9f2477300054f504f563b0c58.zip busybox-a3e4f455ac0265d9f2477300054f504f563b0c58.tar.gz |
Fix a problem with unsatisfied backrefs (noted by Martin Bene).
-rw-r--r-- | editors/sed.c | 10 | ||||
-rw-r--r-- | sed.c | 10 |
2 files changed, 12 insertions, 8 deletions
diff --git a/editors/sed.c b/editors/sed.c index a18cfc7..352c5c9 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -490,7 +490,7 @@ static void load_cmd_file(char *filename) } } -static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch) +static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch, int matches) { int i; @@ -506,8 +506,9 @@ static void print_subst_w_backrefs(const char *line, const char *replace, regmat tmpstr[1] = 0; backref = atoi(tmpstr); /* print out the text held in regmatch[backref] */ - for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) - fputc(line[j], stdout); + if (backref <= matches && regmatch[backref].rm_so != -1) + for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) + fputc(line[j], stdout); } /* if we find a backslash escaped character, print the character */ @@ -555,7 +556,8 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) fputc(hackline[i], stdout); /* then print the substitution string */ - print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch); + print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, + sed_cmd->num_backrefs); /* advance past the match */ hackline += regmatch[0].rm_eo; @@ -490,7 +490,7 @@ static void load_cmd_file(char *filename) } } -static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch) +static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch, int matches) { int i; @@ -506,8 +506,9 @@ static void print_subst_w_backrefs(const char *line, const char *replace, regmat tmpstr[1] = 0; backref = atoi(tmpstr); /* print out the text held in regmatch[backref] */ - for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) - fputc(line[j], stdout); + if (backref <= matches && regmatch[backref].rm_so != -1) + for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) + fputc(line[j], stdout); } /* if we find a backslash escaped character, print the character */ @@ -555,7 +556,8 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) fputc(hackline[i], stdout); /* then print the substitution string */ - print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch); + print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, + sed_cmd->num_backrefs); /* advance past the match */ hackline += regmatch[0].rm_eo; |