summaryrefslogtreecommitdiff
path: root/usr.bin/make
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2022-01-27 10:42:02 +0000
committerrillig <rillig@NetBSD.org>2022-01-27 10:42:02 +0000
commitffda960f1924479acfa8cd1b15fef32c6ed672bf (patch)
treeec88fb5f188d8ee8950e3806996a41dd0a9ea66a /usr.bin/make
parent76dd5872bc1ec2aed0f8fc6d9047933e003436d7 (diff)
tests/make: test hash code collisions of variable names
In HashEntry_KeyEquals, the line 'return false' was not covered by any tests before. Since it is an edge case that two variable names (or other keys) have the same 32-bit hash code and still differ, better test for that edge case explicitly since a bug in this place would be rare and hard to find.
Diffstat (limited to 'usr.bin/make')
-rw-r--r--usr.bin/make/unit-tests/varname.mk44
1 files changed, 43 insertions, 1 deletions
diff --git a/usr.bin/make/unit-tests/varname.mk b/usr.bin/make/unit-tests/varname.mk
index f586c7602cb..0fc908c3648 100644
--- a/usr.bin/make/unit-tests/varname.mk
+++ b/usr.bin/make/unit-tests/varname.mk
@@ -1,4 +1,4 @@
-# $NetBSD: varname.mk,v 1.8 2020/11/02 22:59:48 rillig Exp $
+# $NetBSD: varname.mk,v 1.9 2022/01/27 10:42:02 rillig Exp $
#
# Tests for special variables, such as .MAKE or .PARSEDIR.
# And for variable names in general.
@@ -41,4 +41,46 @@ ${VARNAME}= try3
.MAKEFLAGS: -d0
+# All variable names of a scope are stored in the same hash table, using a
+# simple hash function. Ensure that HashEntry_KeyEquals handles collisions
+# correctly and that the correct variable is looked up. The strings "0x" and
+# "1Y" have the same hash code, as 31 * '0' + 'x' == 31 * '1' + 'Y'.
+V.0x= 0x
+V.1Y= 1Y
+.if ${V.0x} != "0x" || ${V.1Y} != "1Y"
+. error
+.endif
+
+# The string "ASDZguv", when used as a prefix of a variable name, keeps the
+# hash code unchanged, its own hash code is 0.
+ASDZguvV.0x= 0x
+ASDZguvV.1Y= 1Y
+.if ${ASDZguvV.0x} != "0x"
+. error
+.elif ${ASDZguvV.1Y} != "1Y"
+. error
+.endif
+
+# Ensure that variables with the same hash code whose name is a prefix of the
+# other can be accessed. In this case, the shorter variable name is defined
+# first to make it appear later in the bucket of the hash table.
+ASDZguv= once
+ASDZguvASDZguv= twice
+.if ${ASDZguv} != "once"
+. error
+.elif ${ASDZguvASDZguv} != "twice"
+. error
+.endif
+
+# Ensure that variables with the same hash code whose name is a prefix of the
+# other can be accessed. In this case, the longer variable name is defined
+# first to make it appear later in the bucket of the hash table.
+ASDZguvASDZguv.param= twice
+ASDZguv.param= once
+.if ${ASDZguv.param} != "once"
+. error
+.elif ${ASDZguvASDZguv.param} != "twice"
+. error
+.endif
+
all: