Module Name:    src
Committed By:   rillig
Date:           Sun Mar 30 01:27:13 UTC 2025

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: moderrs.exp varmod-select-words.exp
            varmod-select-words.mk varmod.exp varmod.mk

Log Message:
make: fix error message for invalid ":[...]" modifier

The unparsed value of the modifier argument can already be seen in the
stack trace, but the modifier argument may contain expressions and the
expanded value of these expressions was hidden up to now. See the EMPTY
test at the bottom of varmod-select-words.mk for details.


To generate a diff of this commit:
cvs rdiff -u -r1.1156 -r1.1157 src/usr.bin/make/var.c
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/make/unit-tests/moderrs.exp
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/make/unit-tests/varmod-select-words.exp
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/make/unit-tests/varmod-select-words.mk
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/make/unit-tests/varmod.exp
cvs rdiff -u -r1.25 -r1.26 src/usr.bin/make/unit-tests/varmod.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.1156 src/usr.bin/make/var.c:1.1157
--- src/usr.bin/make/var.c:1.1156	Sun Mar 30 01:09:41 2025
+++ src/usr.bin/make/var.c	Sun Mar 30 01:27:12 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.1156 2025/03/30 01:09:41 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.1157 2025/03/30 01:27:12 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -128,7 +128,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1156 2025/03/30 01:09:41 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1157 2025/03/30 01:27:12 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -1966,10 +1966,6 @@ FormatTime(const char *fmt, time_t t, bo
  * and stores the result back in ch->expr->value via Expr_SetValueOwn or
  * Expr_SetValueRefer.
  *
- * If evaluating fails, the fallback error message "Bad modifier" is printed.
- * TODO: Add proper error handling to Var_Subst, Var_Parse, ApplyModifiers and
- * ModifyWords.
- *
  * Some modifiers such as :D and :U turn undefined expressions into defined
  * expressions using Expr_Define.
  */
@@ -3232,7 +3228,7 @@ ApplyModifier_Words(const char **pp, Mod
 {
 	Expr *expr = ch->expr;
 	int first, last;
-	const char *p, *mod = *pp;
+	const char *p;
 	LazyBuf argBuf;
 	FStr arg;
 
@@ -3243,8 +3239,12 @@ ApplyModifier_Words(const char **pp, Mod
 	arg = LazyBuf_DoneGet(&argBuf);
 	p = arg.str;
 
-	if (!IsDelimiter(**pp, ch))
-		goto bad_modifier;		/* Found junk after ']' */
+	if (!IsDelimiter(**pp, ch)) {
+		Parse_Error(PARSE_FATAL,
+		    "Extra text after \"[%s]\"", arg.str);
+		FStr_Done(&arg);
+		return AMR_CLEANUP;
+	}
 
 	if (!ModChain_ShouldEval(ch))
 		goto ok;
@@ -3309,10 +3309,8 @@ ok:
 	return AMR_OK;
 
 bad_modifier:
+	Parse_Error(PARSE_FATAL, "Invalid modifier \":[%s]\"", arg.str);
 	FStr_Done(&arg);
-	/* Take a guess at where the modifier ends. */
-	Parse_Error(PARSE_FATAL, "Bad modifier \":%.*s\"",
-	    (int)strcspn(mod, ":)}"), mod);
 	return AMR_CLEANUP;
 }
 

Index: src/usr.bin/make/unit-tests/moderrs.exp
diff -u src/usr.bin/make/unit-tests/moderrs.exp:1.52 src/usr.bin/make/unit-tests/moderrs.exp:1.53
--- src/usr.bin/make/unit-tests/moderrs.exp:1.52	Sun Mar 30 00:35:52 2025
+++ src/usr.bin/make/unit-tests/moderrs.exp	Sun Mar 30 01:27:13 2025
@@ -43,7 +43,7 @@ make: Unfinished modifier after "#}", ex
 	in command "@echo ${UNDEF:U1 2 3:[#}"
 	in target "words-2"
 13=
-make: Bad modifier ":[123451234512345123451234512345]"
+make: Invalid modifier ":[123451234512345123451234512345]"
 	while evaluating variable "UNDEF" with value "1 2 3"
 	in command "@echo 12345=${UNDEF:U1 2 3:[123451234512345123451234512345]:S,^$,ok,:S,^3$,ok,}"
 	in target "words-3"

Index: src/usr.bin/make/unit-tests/varmod-select-words.exp
diff -u src/usr.bin/make/unit-tests/varmod-select-words.exp:1.7 src/usr.bin/make/unit-tests/varmod-select-words.exp:1.8
--- src/usr.bin/make/unit-tests/varmod-select-words.exp:1.7	Sat Mar 29 10:39:49 2025
+++ src/usr.bin/make/unit-tests/varmod-select-words.exp	Sun Mar 30 01:27:13 2025
@@ -1,4 +1,4 @@
-make: Bad modifier ":[]"
+make: Invalid modifier ":[]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[]="${LIST:[]}" is an error'"
 	in target "mod-squarebrackets-empty"
@@ -39,11 +39,11 @@ REALLYSPACE=" "
 REALLYSPACE:[1]="" == "" ?
 REALLYSPACE:[*]:[1]=" " == " " ?
 LIST:[1]="one"
-make: Bad modifier ":[1.]"
+make: Invalid modifier ":[1.]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[1.]="${LIST:[1.]}" is an error'"
 	in target "mod-squarebrackets-n-error-1"
-make: Bad modifier ":[1]."
+make: Extra text after "[1]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[1].="${LIST:[1].}" is an error'"
 	in target "mod-squarebrackets-n-error-2"
@@ -51,11 +51,11 @@ LIST:[2]="two"
 LIST:[6]="six"
 LIST:[7]=""
 LIST:[999]=""
-make: Bad modifier ":[-]"
+make: Invalid modifier ":[-]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[-]="${LIST:[-]}" is an error'"
 	in target "mod-squarebrackets-n-error-3"
-make: Bad modifier ":[--]"
+make: Invalid modifier ":[--]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[--]="${LIST:[--]}" is an error'"
 	in target "mod-squarebrackets-n-error-4"
@@ -77,20 +77,20 @@ LIST:[*]:C/ /,/:[2]=""
 LIST:[*]:C/ /,/:[*]:[2]=""
 LIST:[*]:C/ /,/:[@]:[2]="three"
 LONGLIST:[012..0x12]="10 11 12 13 14 15 16 17 18"
-make: Bad modifier ":[1.]"
+make: Invalid modifier ":[1.]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[1.]="${LIST:[1.]}" is an error'"
 	in target "mod-squarebrackets-start-end-error-1"
-make: Bad modifier ":[1..]"
+make: Invalid modifier ":[1..]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[1..]="${LIST:[1..]}" is an error'"
 	in target "mod-squarebrackets-start-end-error-2"
-make: Bad modifier ":[1.. ]"
+make: Invalid modifier ":[1.. ]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[1.. ]="${LIST:[1.. ]}" is an error'"
 	in target "mod-squarebrackets-start-end-error-3"
 LIST:[1..1]="one"
-make: Bad modifier ":[1..1.]"
+make: Invalid modifier ":[1..1.]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[1..1.]="${LIST:[1..1.]}" is an error'"
 	in target "mod-squarebrackets-start-end-error-4"
@@ -98,11 +98,11 @@ LIST:[1..2]="one two"
 LIST:[2..1]="two one"
 LIST:[3..-2]="three four five"
 LIST:[-4..4]="three four"
-make: Bad modifier ":[0..1]"
+make: Invalid modifier ":[0..1]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[0..1]="${LIST:[0..1]}" is an error'"
 	in target "mod-squarebrackets-start-end-error-5"
-make: Bad modifier ":[-1..0]"
+make: Invalid modifier ":[-1..0]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[-1..0]="${LIST:[-1..0]}" is an error'"
 	in target "mod-squarebrackets-start-end-error-6"
@@ -119,7 +119,7 @@ LIST:[${ONE}]="one"
 LIST:[${MINUSONE}]="six"
 LIST:[${STAR}]="one two three four five six"
 LIST:[${AT}]="one two three four five six"
-make: Bad modifier ":[${EMPTY"
+make: Invalid modifier ":[]"
 	while evaluating variable "LIST" with value "one two three four five six"
 	in command "@echo 'LIST:[$${EMPTY}]="${LIST:[${EMPTY}]}" is an error'"
 	in target "mod-squarebrackets-nested-error-1"

Index: src/usr.bin/make/unit-tests/varmod-select-words.mk
diff -u src/usr.bin/make/unit-tests/varmod-select-words.mk:1.6 src/usr.bin/make/unit-tests/varmod-select-words.mk:1.7
--- src/usr.bin/make/unit-tests/varmod-select-words.mk:1.6	Thu Aug 29 20:20:36 2024
+++ src/usr.bin/make/unit-tests/varmod-select-words.mk	Sun Mar 30 01:27:13 2025
@@ -1,4 +1,4 @@
-# $NetBSD: varmod-select-words.mk,v 1.6 2024/08/29 20:20:36 rillig Exp $
+# $NetBSD: varmod-select-words.mk,v 1.7 2025/03/30 01:27:13 rillig Exp $
 #
 # Tests for the :[...] variable modifier, which selects a single word
 # or a range of words from a variable.
@@ -50,7 +50,7 @@ mod-squarebrackets: \
 	mod-squarebrackets-space
 
 mod-squarebrackets-empty:
-# expect: make: Bad modifier ":[]"
+# expect: make: Invalid modifier ":[]"
 	@echo 'LIST:[]="${LIST:[]}" is an error'
 
 mod-squarebrackets-0-star-at:
@@ -96,10 +96,10 @@ mod-squarebrackets-n-ok-1:
 	@echo 'REALLYSPACE:[*]:[1]="${REALLYSPACE:[*]:[1]}" == " " ?'
 	@echo 'LIST:[1]="${LIST:[1]}"'
 mod-squarebrackets-n-error-1:
-# expect: make: Bad modifier ":[1.]"
+# expect: make: Invalid modifier ":[1.]"
 	@echo 'LIST:[1.]="${LIST:[1.]}" is an error'
 mod-squarebrackets-n-error-2:
-# expect: make: Bad modifier ":[1]."
+# expect: make: Extra text after "[1]"
 	@echo 'LIST:[1].="${LIST:[1].}" is an error'
 mod-squarebrackets-n-ok-2:
 	@echo 'LIST:[2]="${LIST:[2]}"'
@@ -107,10 +107,10 @@ mod-squarebrackets-n-ok-2:
 	@echo 'LIST:[7]="${LIST:[7]}"'
 	@echo 'LIST:[999]="${LIST:[999]}"'
 mod-squarebrackets-n-error-3:
-# expect: make: Bad modifier ":[-]"
+# expect: make: Invalid modifier ":[-]"
 	@echo 'LIST:[-]="${LIST:[-]}" is an error'
 mod-squarebrackets-n-error-4:
-# expect: make: Bad modifier ":[--]"
+# expect: make: Invalid modifier ":[--]"
 	@echo 'LIST:[--]="${LIST:[--]}" is an error'
 mod-squarebrackets-n-ok-3:
 	@echo 'LIST:[-1]="${LIST:[-1]}"'
@@ -133,18 +133,18 @@ mod-squarebrackets-n-ok-3:
 	@echo 'LONGLIST:[012..0x12]="${LONGLIST:[012..0x12]}"'
 
 mod-squarebrackets-start-end-error-1:
-# expect: make: Bad modifier ":[1.]"
+# expect: make: Invalid modifier ":[1.]"
 	@echo 'LIST:[1.]="${LIST:[1.]}" is an error'
 mod-squarebrackets-start-end-error-2:
-# expect: make: Bad modifier ":[1..]"
+# expect: make: Invalid modifier ":[1..]"
 	@echo 'LIST:[1..]="${LIST:[1..]}" is an error'
 mod-squarebrackets-start-end-error-3:
-# expect: make: Bad modifier ":[1.. ]"
+# expect: make: Invalid modifier ":[1.. ]"
 	@echo 'LIST:[1.. ]="${LIST:[1.. ]}" is an error'
 mod-squarebrackets-start-end-ok-1:
 	@echo 'LIST:[1..1]="${LIST:[1..1]}"'
 mod-squarebrackets-start-end-error-4:
-# expect: make: Bad modifier ":[1..1.]"
+# expect: make: Invalid modifier ":[1..1.]"
 	@echo 'LIST:[1..1.]="${LIST:[1..1.]}" is an error'
 mod-squarebrackets-start-end-ok-2:
 	@echo 'LIST:[1..2]="${LIST:[1..2]}"'
@@ -152,10 +152,10 @@ mod-squarebrackets-start-end-ok-2:
 	@echo 'LIST:[3..-2]="${LIST:[3..-2]}"'
 	@echo 'LIST:[-4..4]="${LIST:[-4..4]}"'
 mod-squarebrackets-start-end-error-5:
-# expect: make: Bad modifier ":[0..1]"
+# expect: make: Invalid modifier ":[0..1]"
 	@echo 'LIST:[0..1]="${LIST:[0..1]}" is an error'
 mod-squarebrackets-start-end-error-6:
-# expect: make: Bad modifier ":[-1..0]"
+# expect: make: Invalid modifier ":[-1..0]"
 	@echo 'LIST:[-1..0]="${LIST:[-1..0]}" is an error'
 mod-squarebrackets-start-end-ok-3:
 	@echo 'LIST:[-1..1]="${LIST:[-1..1]}"'
@@ -174,7 +174,7 @@ mod-squarebrackets-nested-ok-1:
 	@echo 'LIST:[$${STAR}]="${LIST:[${STAR}]}"'
 	@echo 'LIST:[$${AT}]="${LIST:[${AT}]}"'
 mod-squarebrackets-nested-error-1:
-# expect: make: Bad modifier ":[${EMPTY"
+# expect: make: Invalid modifier ":[]"
 	@echo 'LIST:[$${EMPTY}]="${LIST:[${EMPTY}]}" is an error'
 mod-squarebrackets-nested-ok-2:
 	@echo 'LIST:[$${LONGLIST:[21]:S/2//}]="${LIST:[${LONGLIST:[21]:S/2//}]}"'

Index: src/usr.bin/make/unit-tests/varmod.exp
diff -u src/usr.bin/make/unit-tests/varmod.exp:1.20 src/usr.bin/make/unit-tests/varmod.exp:1.21
--- src/usr.bin/make/unit-tests/varmod.exp:1.20	Sat Mar 29 20:19:58 2025
+++ src/usr.bin/make/unit-tests/varmod.exp	Sun Mar 30 01:27:13 2025
@@ -5,9 +5,9 @@ make: "varmod.mk" line 117: Dollar follo
 make: "varmod.mk" line 127: Missing delimiter ':' after modifier "P"
 	while evaluating variable "VAR" with value "VAR"
 make: "varmod.mk" line 129: Missing argument for ".error"
-make: "varmod.mk" line 135: Bad modifier ":[99333000222000111000]"
+make: "varmod.mk" line 135: Invalid modifier ":[99333000222000111000]"
 	while evaluating variable "word" with value "word"
-make: "varmod.mk" line 138: Bad modifier ":[2147483648]"
+make: "varmod.mk" line 138: Invalid modifier ":[2147483648]"
 	while evaluating variable "word" with value "word"
 make: "varmod.mk" line 144: Invalid number "99333000222000111000}" for ':range' modifier
 	while evaluating variable "word" with value "word"
@@ -22,7 +22,7 @@ make: "varmod.mk" line 172: Dollar follo
 	while evaluating variable "VAR" with value "value$ appended$"
 make: "varmod.mk" line 182: Dollar followed by nothing
 	while evaluating variable "word" with value "word"
-make: "varmod.mk" line 186: Bad modifier ":[$]"
+make: "varmod.mk" line 186: Invalid modifier ":[$]"
 	while evaluating variable "word" with value ""
 make: "varmod.mk" line 203: Dollar followed by nothing
 	while evaluating variable "VAR" with value "value$ appended$"

Index: src/usr.bin/make/unit-tests/varmod.mk
diff -u src/usr.bin/make/unit-tests/varmod.mk:1.25 src/usr.bin/make/unit-tests/varmod.mk:1.26
--- src/usr.bin/make/unit-tests/varmod.mk:1.25	Sun Mar 30 00:35:52 2025
+++ src/usr.bin/make/unit-tests/varmod.mk	Sun Mar 30 01:27:13 2025
@@ -1,4 +1,4 @@
-# $NetBSD: varmod.mk,v 1.25 2025/03/30 00:35:52 rillig Exp $
+# $NetBSD: varmod.mk,v 1.26 2025/03/30 01:27:13 rillig Exp $
 #
 # Tests for variable modifiers, such as :Q, :S,from,to or :Ufallback.
 #
@@ -131,10 +131,10 @@ VAR=	STOP
 
 # Test the word selection modifier ':[n]' with a very large number that is
 # larger than ULONG_MAX for any supported platform.
-# expect+1: Bad modifier ":[99333000222000111000]"
+# expect+1: Invalid modifier ":[99333000222000111000]"
 .if ${word:L:[99333000222000111000]}
 .endif
-# expect+1: Bad modifier ":[2147483648]"
+# expect+1: Invalid modifier ":[2147483648]"
 .if ${word:L:[2147483648]}
 .endif
 
@@ -182,7 +182,7 @@ ${:U }=		<space>
 .if ${word:L:@w@$w$@} != "word"
 .  error
 .endif
-# expect+1: Bad modifier ":[$]"
+# expect+1: Invalid modifier ":[$]"
 .if ${word:[$]}
 .  error
 .else

Reply via email to