From 59feb4b9bbd0ca4974fb3cecf8451bffaa57f6e7 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Wed, 22 Aug 2001 05:31:15 +0000 Subject: [PATCH] Backport Matt's non-existent backreference sed fix to stable --- busybox/editors/sed.c | 10 ++++++---- busybox/sed.c | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/busybox/editors/sed.c b/busybox/editors/sed.c index a18cfc7c3..352c5c94f 100644 --- a/busybox/editors/sed.c +++ b/busybox/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; diff --git a/busybox/sed.c b/busybox/sed.c index a18cfc7c3..352c5c94f 100644 --- a/busybox/sed.c +++ b/busybox/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; -- 2.25.1