Fix the case where the pattern space is empty but does not start
authormillert <millert@openbsd.org>
Wed, 28 Aug 2024 14:30:26 +0000 (14:30 +0000)
committermillert <millert@openbsd.org>
Wed, 28 Aug 2024 14:30:26 +0000 (14:30 +0000)
with a NUL character, which might occur after using the D command.
From Mohamed Akram

usr.bin/sed/process.c

index c2d7894..7635f2f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: process.c,v 1.37 2024/07/17 20:57:16 millert Exp $    */
+/*     $OpenBSD: process.c,v 1.38 2024/08/28 14:30:26 millert Exp $    */
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -387,14 +387,12 @@ substitute(struct s_command *cp)
                 * 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;