xref: /freebsd/contrib/bmake/unit-tests/varmod-loop-delete.mk (revision a4e5e0106ac7145f56eb39a691e302cabb4635be)
1# $NetBSD: varmod-loop-delete.mk,v 1.3 2023/06/01 20:56:35 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.
19# expect+1: Cannot delete variable "VAR" while it is used
20EVAL:=	${VAR}
21.if ${EVAL} != " rest of the value"
22.  error
23.endif
24
25VAR=	${:U:@VAR@@} rest of the value
26all: .PHONY
27	# In the command that is associated with a target, the scope is the
28	# one from the target.  That scope only contains a few variables like
29	# '.TARGET', '.ALLSRC', '.IMPSRC'.  Make does not expect that these
30	# variables get modified from the outside.
31	#
32	# There is no variable named 'VAR' in the local scope, so nothing
33	# happens.
34	: $@: '${VAR}'
35