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.
---
 ed.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/ed.c b/ed.c
index 6aa5d81..288e01e 100644
--- 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);
        }
-- 
2.42.0


Reply via email to