varmod-loop-delete.mk revision 1.7 1 1.7 rillig # $NetBSD: varmod-loop-delete.mk,v 1.7 2024/08/29 20:20:36 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for the variable modifier ':@', which as a side effect allows to
4 1.1 rillig # delete an arbitrary variable.
5 1.1 rillig
6 1.1 rillig # A side effect of the modifier ':@' is that the loop variable is created as
7 1.1 rillig # an actual variable in the current evaluation scope (Command/Global/target),
8 1.2 rillig # and at the end of the loop, this variable is deleted. Since var.c 1.204
9 1.2 rillig # from 2016-02-18 and before var.c 1.963 from 2021-12-05, a variable could be
10 1.2 rillig # deleted while it was in use, leading to a use-after-free bug.
11 1.1 rillig #
12 1.1 rillig # See Var_Parse, comment 'the value of the variable must not change'.
13 1.1 rillig
14 1.1 rillig # Set up the variable that deletes itself when it is evaluated.
15 1.1 rillig VAR= ${:U:@VAR@@} rest of the value
16 1.1 rillig
17 1.1 rillig # In an assignment, the scope is 'Global'. Since the variable 'VAR' is
18 1.1 rillig # defined in the global scope, it deletes itself.
19 1.7 rillig # expect+1: Cannot delete variable "VAR" while it is used
20 1.1 rillig EVAL:= ${VAR}
21 1.1 rillig .if ${EVAL} != " rest of the value"
22 1.1 rillig . error
23 1.1 rillig .endif
24 1.1 rillig
25 1.1 rillig VAR= ${:U:@VAR@@} rest of the value
26 1.1 rillig all: .PHONY
27 1.1 rillig # In the command that is associated with a target, the scope is the
28 1.1 rillig # one from the target. That scope only contains a few variables like
29 1.1 rillig # '.TARGET', '.ALLSRC', '.IMPSRC'. Make does not expect that these
30 1.1 rillig # variables get modified from the outside.
31 1.1 rillig #
32 1.1 rillig # There is no variable named 'VAR' in the local scope, so nothing
33 1.1 rillig # happens.
34 1.1 rillig : $@: '${VAR}'
35