Module Name: src
Committed By: rillig
Date: Sun Dec 10 14:30:51 UTC 2023
Modified Files:
src/usr.bin/make: var.c
src/usr.bin/make/unit-tests: varmod-subst-regex.mk
Log Message:
make: fix crash in ':C' modifier on an empty expression
Since var.c 1.982 from 2021-12-13.
To generate a diff of this commit:
cvs rdiff -u -r1.1076 -r1.1077 src/usr.bin/make/var.c
cvs rdiff -u -r1.8 -r1.9 src/usr.bin/make/unit-tests/varmod-subst-regex.mk
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1076 src/usr.bin/make/var.c:1.1077
--- src/usr.bin/make/var.c:1.1076 Sat Dec 9 00:13:38 2023
+++ src/usr.bin/make/var.c Sun Dec 10 14:30:50 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: var.c,v 1.1076 2023/12/09 00:13:38 sjg Exp $ */
+/* $NetBSD: var.c,v 1.1077 2023/12/10 14:30:50 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
#include "metachar.h"
/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1076 2023/12/09 00:13:38 sjg Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1077 2023/12/10 14:30:50 rillig Exp $");
/*
* Variables are defined using one of the VAR=value assignments. Their
@@ -1002,7 +1002,7 @@ Var_SetWithFlags(GNode *scope, const cha
* If requested, don't export these in the
* environment individually. We still put
* them in .MAKEOVERRIDES so that the
- * command-line settings continue to override
+ * command-line settings continue to override
* Makefile settings.
*/
if (!opts.varNoExportEnv)
@@ -1629,7 +1629,7 @@ ok:
wp += (size_t)m[0].rm_eo;
if (args->pflags.subGlobal) {
flags |= REG_NOTBOL;
- if (m[0].rm_so == 0 && m[0].rm_eo == 0) {
+ if (m[0].rm_so == 0 && m[0].rm_eo == 0 && *wp != '\0') {
SepBuf_AddBytes(buf, wp, 1);
wp++;
}
Index: src/usr.bin/make/unit-tests/varmod-subst-regex.mk
diff -u src/usr.bin/make/unit-tests/varmod-subst-regex.mk:1.8 src/usr.bin/make/unit-tests/varmod-subst-regex.mk:1.9
--- src/usr.bin/make/unit-tests/varmod-subst-regex.mk:1.8 Sun Nov 19 21:47:52 2023
+++ src/usr.bin/make/unit-tests/varmod-subst-regex.mk Sun Dec 10 14:30:51 2023
@@ -1,4 +1,4 @@
-# $NetBSD: varmod-subst-regex.mk,v 1.8 2023/11/19 21:47:52 rillig Exp $
+# $NetBSD: varmod-subst-regex.mk,v 1.9 2023/12/10 14:30:51 rillig Exp $
#
# Tests for the :C,from,to, variable modifier.
@@ -84,6 +84,31 @@ all: unmatched-subexpression
. error
.endif
+
+# Contrary to the ':S' modifier, the ':C' modifier matches on an expression
+# that contains no words at all, but only if the regular expression matches an
+# empty string, for example, when the regular expression is anchored at the
+# beginning or the end.
+.if "<${U:S,^,prefix,}> <${:U:C,^,prefix,}>" != "<> <prefix>"
+. error
+.endif
+.if "<${U:S,$,suffix,}> <${:U:C,$,suffix,}>" != "<> <suffix>"
+. error
+.endif
+.if "<${U:S,^$,whole,}> <${:U:C,^$,whole,}>" != "<> <whole>"
+. error
+.endif
+.if "<${U:S,^,prefix,g}> <${:U:C,^,prefix,g}>" != "<> <prefix>"
+. error
+.endif
+.if "<${U:S,$,suffix,g}> <${:U:C,$,suffix,g}>" != "<> <suffix>"
+. error
+.endif
+.if "<${U:S,^$,whole,g}> <${:U:C,^$,whole,g}>" != "<> <whole>"
+. error
+.endif
+
+
# Multiple asterisks form an invalid regular expression. This produces an
# error message and (as of 2020-08-28) stops parsing in the middle of the
# expression. The unparsed part of the expression is then copied