commit 7d1e7ae620f30e40f8863b6e653dbbf86eafb1f6
parent c15aa18ff3958f4f72137dc8139744c5f028e83f
Author: Santtu Lakkala <inz@inz.fi>
Date: Thu, 20 Nov 2025 15:27:51 +0200
ed: Fix regex matching and no end-of-line files
Remove trailing newline from lines before passing them to regexec to
avoid false-positive matches for ^$ (or similar).
Also fix handling of files not ending in newline by appending the
newline instead of replacing the last character.
Diffstat:
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/ed.c b/ed.c
@@ -442,6 +442,7 @@ static int
match(int num)
{
lastmatch = gettxt(num);
+ text.str[text.siz - 2] = '\0';
return !regexec(pattern, lastmatch, 10, matchs, 0);
}
@@ -814,11 +815,13 @@ doread(const char *fname)
for (cnt = 0; (n = getline(&s, &len, fp)) > 0; cnt += (size_t)n) {
chksignals();
if (s[n-1] != '\n') {
- if (len == SIZE_MAX || !(p = realloc(s, ++len)))
- error("out of memory");
- s = p;
- s[n-1] = '\n';
- s[n] = '\0';
+ if (n + 1 >= len) {
+ if (len == SIZE_MAX || !(p = realloc(s, ++len)))
+ error("out of memory");
+ s = p;
+ }
+ s[n] = '\n';
+ s[n+1] = '\0';
}
inject(s, AFTER);
}