From 7e777743111fe6383e2a03513f9157a258a2d8db Mon Sep 17 00:00:00 2001 From: "Pedro F. Giffuni" Date: Wed, 1 Jun 2016 16:53:02 +0000 Subject: [PATCH] sed(1): Fix a mismatch and sync with the OpenBSD's commit. This was causing some strange behaviour. Reported by: olivier Obtained from: OpenBSD (CVS rev. 1.28) --- usr.bin/sed/process.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index 5c883c4229ff..b70a492349ad 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -394,7 +394,7 @@ substitute(struct s_command *cp) linenum, fname, cp->u.s->maxbref); } } - if (!regexec_e(re, s, 0, 0, 0, psl)) + if (!regexec_e(re, ps, 0, 0, 0, psl)) return (0); SS.len = 0; /* Clean substitute space. */ @@ -404,7 +404,7 @@ substitute(struct s_command *cp) do { /* Copy the leading retained string. */ - if (n <= 1 && match[0].rm_so - le) + if (n <= 1 && (match[0].rm_so > le)) cspace(&SS, s, match[0].rm_so - le, APPEND); /* Skip zero-length matches right after other matches. */ @@ -425,8 +425,8 @@ substitute(struct s_command *cp) } /* Move past this match. */ - s += (match[0].rm_eo - le); - slen -= (match[0].rm_eo - le); + s = ps + match[0].rm_eo; + slen = psl - match[0].rm_eo; le = match[0].rm_eo; /* @@ -446,7 +446,8 @@ substitute(struct s_command *cp) } else lastempty = 0; - } while (n >= 0 && slen >= 0 && regexec_e(re, ps, 0, 0, le, psl)); + } while (n >= 0 && slen >= 0 && + regexec_e(re, ps, REG_NOTBOL, 0, le, psl)); /* Did not find the requested number of matches. */ if (n > 1)