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