This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
#101666: horrible regex parsing error error [sic]
[perl5.git] / t / re / reg_mesg.t
1 #!./perl -w
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6         require './test.pl';
7         eval 'require Config'; # assume defaults if this fails
8 }
9
10 use strict;
11
12 ##
13 ## If the markers used are changed (search for "MARKER1" in regcomp.c),
14 ## update only these two regexs, and leave the {#} in the @death/@warning
15 ## arrays below. The {#} is a meta-marker -- it marks where the marker should
16 ## go.
17 ##
18 sub fixup_expect {
19     my $expect = shift;
20     $expect =~ s/{\#}/<-- HERE/;
21     $expect =~ s/{\#}/ <-- HERE /;
22     $expect .= " at ";
23     return $expect;
24 }
25
26 my $inf_m1 = ($Config::Config{reg_infty} || 32767) - 1;
27 my $inf_p1 = $inf_m1 + 2;
28
29 ##
30 ## Key-value pairs of code/error of code that should have fatal errors.
31 ##
32 my @death =
33 (
34  '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions in regex; marked by {#} in m/[[=foo=]{#}]/',
35
36  '/(?<= .*)/' =>  'Variable length lookbehind not implemented in regex m/(?<= .*)/',
37
38  '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= x{1000})/',
39
40  '/(?@)/' => 'Sequence (?@...) not implemented in regex; marked by {#} in m/(?@{#})/',
41
42  '/(?{ 1/' => 'Missing right curly or square bracket',
43
44  '/(?(1x))/' => 'Switch condition not recognized in regex; marked by {#} in m/(?(1x{#}))/',
45
46  '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches in regex; marked by {#} in m/(?(1)x|y|{#}z)/',
47
48  '/(?(x)y|x)/' => 'Unknown switch condition (?(x) in regex; marked by {#} in m/(?({#}x)y|x)/',
49
50  '/(?/' => 'Sequence (? incomplete in regex; marked by {#} in m/(?{#}/',
51
52  '/(?;x/' => 'Sequence (?;...) not recognized in regex; marked by {#} in m/(?;{#}x/',
53  '/(?<;x/' => 'Group name must start with a non-digit word character in regex; marked by {#} in m/(?<;{#}x/',
54  '/(?\ix/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}ix/',
55  '/(?\mx/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}mx/',
56  '/(?\:x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}:x/',
57  '/(?\=x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}=x/',
58  '/(?\!x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}!x/',
59  '/(?\<=x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}<=x/',
60  '/(?\<!x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}<!x/',
61  '/(?\>x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}>x/',
62  '/(?^-i:foo)/' => 'Sequence (?^-...) not recognized in regex; marked by {#} in m/(?^-{#}i:foo)/',
63  '/(?^-i)foo/' => 'Sequence (?^-...) not recognized in regex; marked by {#} in m/(?^-{#}i)foo/',
64  '/(?^d:foo)/' => 'Sequence (?^d...) not recognized in regex; marked by {#} in m/(?^d{#}:foo)/',
65  '/(?^d)foo/' => 'Sequence (?^d...) not recognized in regex; marked by {#} in m/(?^d{#})foo/',
66  '/(?^lu:foo)/' => 'Regexp modifiers "l" and "u" are mutually exclusive in regex; marked by {#} in m/(?^lu{#}:foo)/',
67  '/(?^lu)foo/' => 'Regexp modifiers "l" and "u" are mutually exclusive in regex; marked by {#} in m/(?^lu{#})foo/',
68 '/(?da:foo)/' => 'Regexp modifiers "d" and "a" are mutually exclusive in regex; marked by {#} in m/(?da{#}:foo)/',
69 '/(?lil:foo)/' => 'Regexp modifier "l" may not appear twice in regex; marked by {#} in m/(?lil{#}:foo)/',
70 '/(?aaia:foo)/' => 'Regexp modifier "a" may appear a maximum of twice in regex; marked by {#} in m/(?aaia{#}:foo)/',
71 '/(?i-l:foo)/' => 'Regexp modifier "l" may not appear after the "-" in regex; marked by {#} in m/(?i-l{#}:foo)/',
72
73  '/((x)/' => 'Unmatched ( in regex; marked by {#} in m/({#}(x)/',
74
75  "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 in regex; marked by {#} in m/x{{#}$inf_p1}/",
76
77  '/x{3,1}/' => 'Can\'t do {n,m} with n > m in regex; marked by {#} in m/x{3,1}{#}/',
78
79  '/x**/' => 'Nested quantifiers in regex; marked by {#} in m/x**{#}/',
80
81  '/x[/' => 'Unmatched [ in regex; marked by {#} in m/x[{#}/',
82
83  '/*/', => 'Quantifier follows nothing in regex; marked by {#} in m/*{#}/',
84
85  '/\p{x/' => 'Missing right brace on \p{} in regex; marked by {#} in m/\p{{#}x/',
86
87  '/[\p{x]/' => 'Missing right brace on \p{} in regex; marked by {#} in m/[\p{{#}x]/',
88
89  '/(x)\2/' => 'Reference to nonexistent group in regex; marked by {#} in m/(x)\2{#}/',
90
91  'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
92
93  '/\x{1/' => 'Missing right brace on \x{} in regex; marked by {#} in m/\x{{#}1/',
94
95  '/[\x{X]/' => 'Missing right brace on \x{} in regex; marked by {#} in m/[\x{{#}X]/',
96
97  '/\o{1/' => 'Missing right brace on \o{ in regex; marked by {#} in m/\o{{#}1/',
98
99  '/[\o{X]/' => 'Missing right brace on \o{ in regex; marked by {#} in m/[\o{{#}X]/',
100
101  '/[[:barf:]]/' => 'POSIX class [:barf:] unknown in regex; marked by {#} in m/[[:barf:]{#}]/',
102
103  '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions in regex; marked by {#} in m/[[=barf=]{#}]/',
104
105  '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions in regex; marked by {#} in m/[[.barf.]{#}]/',
106   
107  '/[z-a]/' => 'Invalid [] range "z-a" in regex; marked by {#} in m/[z-a{#}]/',
108
109  '/\p/' => 'Empty \p{} in regex; marked by {#} in m/\p{#}/',
110
111  '/\P{}/' => 'Empty \P{} in regex; marked by {#} in m/\P{{#}}/',
112 );
113
114 ##
115 ## Key-value pairs of code/error of code that should have non-fatal warnings.
116 ##
117 my @warning = (
118     'm/\b*/' => '\b* matches null string many times in regex; marked by {#} in m/\b*{#}/',
119
120     'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes in regex; marked by {#} in m/[:blank:]{#}/',
121
122     "m'[\\y]'"     => 'Unrecognized escape \y in character class passed through in regex; marked by {#} in m/[\y{#}]/',
123
124     'm/[a-\d]/' => 'False [] range "a-\d" in regex; marked by {#} in m/[a-\d{#}]/',
125     'm/[\w-x]/' => 'False [] range "\w-" in regex; marked by {#} in m/[\w-{#}x]/',
126     'm/[a-\pM]/' => 'False [] range "a-\pM" in regex; marked by {#} in m/[a-\pM{#}]/',
127     'm/[\pM-x]/' => 'False [] range "\pM-" in regex; marked by {#} in m/[\pM-{#}x]/',
128     "m'\\y'"     => 'Unrecognized escape \y passed through in regex; marked by {#} in m/\y{#}/',
129 );
130
131 while (my ($regex, $expect) = splice @death, 0, 2) {
132     my $expect = fixup_expect($expect);
133     # skip the utf8 test on EBCDIC since they do not die
134     next if $::IS_EBCDIC && $regex =~ /utf8/;
135
136     warning_is(sub {
137                    $_ = "x";
138                    eval $regex;
139                    like($@, qr/\Q$expect/);
140                }, undef, "$regex died without any other warnings");
141 }
142
143 while (my ($regex, $expect) = splice @warning, 0, 2) {
144     my $expect = fixup_expect($expect);
145     warning_like(sub {
146                      $_ = "x";
147                      eval $regex;
148                      is($@, '', "$regex did not die");
149                  }, qr/\Q$expect/);
150 }
151
152 done_testing();