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