[hackers] [sbase] [PATCH 3/3] ed: Fix substitutions with non-determinate patterns

From: Wolfgang Corcoran-Mathe <wcm_AT_sigwinch.xyz>
Date: Sun, 24 Jul 2016 21:04:29 -0400

Previously, greedy patterns like /.*/ overran the buffer and
patterns with null matches (e.g. /G*/) never increased the
offset for rematch(). In both cases, the result was an
infinite loop.

Breaking after the first null match follows the behavior of
GNU ed. It has the virtue of simplicity.
---
 ed.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/ed.c b/ed.c
index ae9e6d7..6c05649 100644
--- a/ed.c
+++ b/ed.c
_AT_@ -1005,10 +1005,10 @@ subline(int num, int nth)
 	static size_t siz, cap;
 
 	i = changed = siz = 0;
-	for (m = match(num); m; m = rematch(num)) {
+	for (m = match(num); m && *lastmatch; m = rematch(num)) {
 		addpre(&s, &cap, &siz);
 		changed |= addsub(&s, &cap, &siz, nth, ++i);
-		if (eol || bol)
+		if (eol || bol || matchs[0].rm_eo - matchs[0].rm_so == 0)
 			break;
 	}
 	if (!changed)
-- 
2.9.0
Received on Mon Jul 25 2016 - 03:04:29 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 25 2016 - 03:12:22 CEST