varmod-edge.mk revision 1.5 1 # $NetBSD: varmod-edge.mk,v 1.5 2019/12/01 23:53:49 rillig Exp $
2 #
3 # Tests for edge cases in variable modifiers.
4 #
5 # These tests demonstrate the current implementation in small examples.
6 # They may contain surprising behavior.
7 #
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
12
13 TESTS+= M-paren
14 INP.M-paren= (parentheses) {braces} (opening closing) ()
15 MOD.M-paren= ${INP.M-paren:M(*)}
16 EXP.M-paren= (parentheses) ()
17
18 # The first closing brace matches the opening parenthesis.
19 # The second closing brace actually ends the variable expression.
20 #
21 # XXX: This is unexpected but rarely occurs in practice.
22 TESTS+= M-mixed
23 INP.M-mixed= (paren-brace} (
24 MOD.M-mixed= ${INP.M-mixed:M(*}}
25 EXP.M-mixed= (paren-brace}
26
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
31 # Str_Match.
32 #
33 # XXX: This is unexpected. The opening brace should also be unescaped.
34 TESTS+= M-unescape
35 INP.M-unescape= ({}): \(\{\}\)\: \(\{}\):
36 MOD.M-unescape= ${INP.M-unescape:M\\(\\{\\}\\)\\:}
37 EXP.M-unescape= \(\{}\):
38
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.
42 #
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.
48 #
49 # XXX: This is unexpected but rarely occurs in practice.
50 TESTS+= M-nest-mix
51 INP.M-nest-mix= (parentheses)
52 MOD.M-nest-mix= ${INP.M-nest-mix:M${:U*)}}
53 EXP.M-nest-mix= (parentheses)}
54
55 # In contrast to parentheses and braces, the brackets are not counted
56 # when the :M modifier is parsed since Makefile variables only take the
57 # ${VAR} or $(VAR) forms, but not $[VAR].
58 #
59 # The final ] in the pattern is needed to close the character class.
60 TESTS+= M-nest-brk
61 INP.M-nest-brk= [ [[ [[[
62 MOD.M-nest-brk= ${INP.M-nest-brk:M${:U[[[[[]}}
63 EXP.M-nest-brk= [
64
65 # The pattern in the nested variable has an unclosed character class.
66 # No error is reported though, and the pattern is closed implicitly.
67 #
68 # XXX: It is unexpected that no error is reported.
69 # See str.c, function Str_Match.
70 #
71 # Before 2019-12-02, this test case triggered an out-of-bounds read
72 # in Str_Match.
73 TESTS+= M-pat-err
74 INP.M-pat-err= [ [[ [[[
75 MOD.M-pat-err= ${INP.M-pat-err:M${:U[[}}
76 EXP.M-pat-err= [
77
78 # The first backslash does not escape the second backslash.
79 # Therefore, the second backslash escapes the parenthesis.
80 # This means that the pattern ends there.
81 # The final } in the output comes from the end of MOD.M-bsbs.
82 #
83 # If the first backslash were to escape the second backslash, the first
84 # closing brace would match the opening parenthesis (see M-mixed), and
85 # the second closing brace would be needed to close the variable.
86 # After that, the remaining backslash would escape the parenthesis in
87 # the pattern, therefore (} would match.
88 TESTS+= M-bsbs
89 INP.M-bsbs= (} \( \(}
90 MOD.M-bsbs= ${INP.M-bsbs:M\\(}}
91 EXP.M-bsbs= \(}
92 #EXP.M-bsbs= (} # If the first backslash were to escape ...
93
94 # The backslash in \( does not escape the parenthesis, therefore it
95 # counts for the nesting level and matches with the first closing brace.
96 # The second closing brace closes the variable, and the third is copied
97 # literally.
98 #
99 # The second :M in the pattern is nested between ( and }, therefore it
100 # does not start a new modifier.
101 TESTS+= M-bs1-par
102 INP.M-bs1-par= ( (:M (:M} \( \(:M \(:M}
103 MOD.M-bs1-par= ${INP.M-bs1-par:M\(:M*}}}
104 EXP.M-bs1-par= (:M}}
105
106 # The double backslash is passed verbatim to the pattern matcher.
107 # The Str_Match pattern is \\(:M*}, and there the backslash is unescaped.
108 # Again, the ( takes place in the nesting level, and there is no way to
109 # prevent this, no matter how many backslashes are used.
110 TESTS+= M-bs2-par
111 INP.M-bs2-par= ( (:M (:M} \( \(:M \(:M}
112 MOD.M-bs2-par= ${INP.M-bs2-par:M\\(:M*}}}
113 EXP.M-bs2-par= \(:M}}
114
115 all:
116 .for test in ${TESTS}
117 . if ${MOD.${test}} == ${EXP.${test}}
118 @printf 'ok %s\n' ${test:Q}''
119 . else
120 @printf 'error in %s: expected %s, got %s\n' \
121 ${test:Q}'' ${EXP.${test}:Q}'' ${MOD.${test}:Q}''
122 . endif
123 .endfor
124