Index | Thread | Search

From:
Mohamed Akram <mohd.akram@outlook.com>
Subject:
sed: check length before append in substitute
To:
"tech@openbsd.org" <tech@openbsd.org>
Date:
Fri, 16 Aug 2024 14:33:29 +0000

Download raw body.

Thread
This resolves the case where the pattern space is empty but does not
start with a null character, which might occur after using the D
command. This can be seen in the following sed program, which should
print its input as-is:

	printf 'foo\n\n' | sed -e 's/^//p' -e '$!N' -e D

--- src/usr.bin/sed/process.c.orig	2024-08-16 17:35:20
+++ src/usr.bin/sed/process.c	2024-08-16 17:35:52
@@ -387,14 +387,12 @@
 		 * and at the end of the line, terminate.
 		 */
 		if (match[0].rm_so == match[0].rm_eo) {
-			if (*s == '\0' || *s == '\n')
-				slen = -1;
-			else
-				slen--;
-			if (*s != '\0') {
+			if (slen > 0) {
 				cspace(&SS, s++, 1, APPEND);
+				slen--;
 				le++;
-			}
+			} else
+				slen = -1;
 			lastempty = 1;
 		} else
 			lastempty = 0;