1 # $NetBSD: varmod-edge.mk,v 1.7 2020/04/27 14:07:22 christos Exp $
3 # Tests for edge cases in variable modifiers.
5 # These tests demonstrate the current implementation in small examples.
6 # They may contain surprising behavior.
8 # Each test consists of:
9 # - INP, the input to the test
10 # - MOD, the expression for testing the modifier
11 # - EXP, the expected output
14 INP.M-paren= (parentheses) {braces} (opening closing) ()
15 MOD.M-paren= ${INP.M-paren:M(*)}
16 EXP.M-paren= (parentheses) ()
18 # The first closing brace matches the opening parenthesis.
19 # The second closing brace actually ends the variable expression.
21 # XXX: This is unexpected but rarely occurs in practice.
23 INP.M-mixed= (paren-brace} (
24 MOD.M-mixed= ${INP.M-mixed:M(*}}
25 EXP.M-mixed= (paren-brace}
27 # After the :M modifier has parsed the pattern, only the closing brace
28 # and the colon are unescaped. The other characters are left as-is.
29 # To actually see this effect, the backslashes in the :M modifier need
30 # to be doubled since single backslashes would simply be unescaped by
33 # XXX: This is unexpected. The opening brace should also be unescaped.
35 INP.M-unescape= ({}): \(\{\}\)\: \(\{}\):
36 MOD.M-unescape= ${INP.M-unescape:M\\(\\{\\}\\)\\:}
37 EXP.M-unescape= \(\{}\):
39 # When the :M and :N modifiers are parsed, the pattern finishes as soon
40 # as open_parens + open_braces == closing_parens + closing_braces. This
41 # means that ( and } form a matching pair.
43 # Nested variable expressions are not parsed as such. Instead, only the
44 # parentheses and braces are counted. This leads to a parse error since
45 # the nested expression is not "${:U*)}" but only "${:U*)", which is
46 # missing the closing brace. The expression is evaluated anyway.
47 # The final brace in the output comes from the end of M.nest-mix.
49 # XXX: This is unexpected but rarely occurs in practice.
51 INP.M-nest-mix= (parentheses)
52 MOD.M-nest-mix= ${INP.M-nest-mix:M${:U*)}}
53 EXP.M-nest-mix= (parentheses)}
54 # make: Unclosed variable specification (expecting '}') for "" (value "*)") modifier U
56 # In contrast to parentheses and braces, the brackets are not counted
57 # when the :M modifier is parsed since Makefile variables only take the
58 # ${VAR} or $(VAR) forms, but not $[VAR].
60 # The final ] in the pattern is needed to close the character class.
62 INP.M-nest-brk= [ [[ [[[
63 MOD.M-nest-brk= ${INP.M-nest-brk:M${:U[[[[[]}}
66 # The pattern in the nested variable has an unclosed character class.
67 # No error is reported though, and the pattern is closed implicitly.
69 # XXX: It is unexpected that no error is reported.
70 # See str.c, function Str_Match.
72 # Before 2019-12-02, this test case triggered an out-of-bounds read
75 INP.M-pat-err= [ [[ [[[
76 MOD.M-pat-err= ${INP.M-pat-err:M${:U[[}}
79 # The first backslash does not escape the second backslash.
80 # Therefore, the second backslash escapes the parenthesis.
81 # This means that the pattern ends there.
82 # The final } in the output comes from the end of MOD.M-bsbs.
84 # If the first backslash were to escape the second backslash, the first
85 # closing brace would match the opening parenthesis (see M-mixed), and
86 # the second closing brace would be needed to close the variable.
87 # After that, the remaining backslash would escape the parenthesis in
88 # the pattern, therefore (} would match.
91 MOD.M-bsbs= ${INP.M-bsbs:M\\(}}
93 #EXP.M-bsbs= (} # If the first backslash were to escape ...
95 # The backslash in \( does not escape the parenthesis, therefore it
96 # counts for the nesting level and matches with the first closing brace.
97 # The second closing brace closes the variable, and the third is copied
100 # The second :M in the pattern is nested between ( and }, therefore it
101 # does not start a new modifier.
103 INP.M-bs1-par= ( (:M (:M} \( \(:M \(:M}
104 MOD.M-bs1-par= ${INP.M-bs1-par:M\(:M*}}}
107 # The double backslash is passed verbatim to the pattern matcher.
108 # The Str_Match pattern is \\(:M*}, and there the backslash is unescaped.
109 # Again, the ( takes place in the nesting level, and there is no way to
110 # prevent this, no matter how many backslashes are used.
112 INP.M-bs2-par= ( (:M (:M} \( \(:M \(:M}
113 MOD.M-bs2-par= ${INP.M-bs2-par:M\\(:M*}}}
114 EXP.M-bs2-par= \(:M}}
116 # Str_Match uses a recursive algorithm for matching the * patterns.
117 # Make sure that it survives patterns with 128 asterisks.
118 # That should be enough for all practical purposes.
119 # To produce a stack overflow, just add more :Qs below.
121 INP.M-128= ${:U\\:Q:Q:Q:Q:Q:Q:Q:S,\\,x,g}
122 PAT.M-128= ${:U\\:Q:Q:Q:Q:Q:Q:Q:S,\\,*,g}
123 MOD.M-128= ${INP.M-128:M${PAT.M-128}}
124 EXP.M-128= ${INP.M-128}
126 # This is the normal SysV substitution. Nothing surprising here.
128 INP.eq-ext= file.c file.cc
129 MOD.eq-ext= ${INP.eq-ext:%.c=%.o}
130 EXP.eq-ext= file.o file.cc
132 # The SysV := modifier is greedy and consumes all the modifier text
133 # up until the closing brace or parenthesis. The :Q may look like a
134 # modifier, but it really isn't, that's why it appears in the output.
136 INP.eq-q= file.c file.cc
137 MOD.eq-q= ${INP.eq-q:%.c=%.o:Q}
138 EXP.eq-q= file.o:Q file.cc
140 # The = in the := modifier can be escaped.
142 INP.eq-bs= file.c file.c=%.o
143 MOD.eq-bs= ${INP.eq-bs:%.c\=%.o=%.ext}
144 EXP.eq-bs= file.c file.ext
146 # Having only an escaped = results in a parse error.
147 # The call to "pattern.lhs = VarGetPattern" fails.
149 INP.eq-esc= file.c file...
150 MOD.eq-esc= ${INP.eq-esc:a\=b}
152 # make: Unclosed substitution for INP.eq-esc (= missing)
155 .for test in ${TESTS}
156 . if ${MOD.${test}} == ${EXP.${test}}
157 @printf 'ok %s\n' ${test:Q}''
159 @printf 'error in %s: expected %s, got %s\n' \
160 ${test:Q}'' ${EXP.${test}:Q}'' ${MOD.${test}:Q}''