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