xref: /openbsd/gnu/usr.bin/perl/t/op/lex.t (revision 3d61058a)
1#!perl
2
3# Tests too complex for t/base/lex.t
4
5use strict;
6use warnings;
7
8BEGIN {
9    chdir "t" if -d "t";
10    require './test.pl';
11    @INC= "../lib";
12}
13
14plan(tests => 53);
15
16{
17    print <<'';   # Yow!
18ok 1
19
20    # previous line intentionally left blank.
21
22    my $yow = "ok 2";
23    print <<"";   # Yow!
24$yow
25
26    # previous line intentionally left blank.
27}
28
29curr_test(3);
30
31
32{
33    my %foo = (aap => "monkey");
34    my $foo = '';
35    is("@{[$foo{'aap'}]}", 'monkey', 'interpolation of hash lookup with space between lexical variable and subscript');
36    is("@{[$foo {'aap'}]}", 'monkey', 'interpolation of hash lookup with space between lexical variable and subscript - test for [perl #70091]');
37
38# Original bug report [perl #70091]
39#  #!perl
40#  use warnings;
41#  my %foo;
42#  my $foo = '';
43#  (my $tmp = $foo) =~ s/^/$foo {$0}/e;
44#  __END__
45#
46#  This program causes a segfault with 5.10.0 and 5.10.1.
47#
48#  The space between '$foo' and '{' is essential, which is why piping
49#  it through perl -MO=Deparse "fixes" it.
50#
51
52}
53
54{
55 delete local $ENV{PERL_UNICODE};
56 fresh_perl_is(
57  'BEGIN{ ++$_ for @INC{"charnames.pm","_charnames.pm"} } "\N{a}"',
58  'Constant(\N{a}) unknown at - line 1, within string' . "\n"
59 ."Execution of - aborted due to compilation errors.\n",
60   { stderr => 1 },
61  'correct output (and no crash) when charnames cannot load for \N{...}'
62 );
63}
64fresh_perl_is(
65  'BEGIN{ ++$_ for @INC{"charnames.pm","_charnames.pm"};
66          $^H{charnames} = "foo" } "\N{a}"',
67  "Undefined subroutine &main::foo called at - line 2.\n"
68 ."Propagated at - line 2, within string\n"
69 ."Execution of - aborted due to compilation errors.\n",
70   { stderr => 1 },
71  'no crash when charnames cannot load and %^H holds string'
72);
73fresh_perl_is(
74  'BEGIN{ ++$_ for @INC{"charnames.pm","_charnames.pm"};
75          $^H{charnames} = \"foo" } "\N{a}"',
76  "Not a CODE reference at - line 2.\n"
77 ."Propagated at - line 2, within string\n"
78 ."Execution of - aborted due to compilation errors.\n",
79   { stderr => 1 },
80  'no crash when charnames cannot load and %^H holds string reference'
81);
82
83# not fresh_perl_is, as it seems to hide the error
84is runperl(
85    nolib => 1, # -Ilib may also hide the error
86    progs => [
87      '*{',
88      '         XS::APItest::gv_fetchmeth_type()',
89      '}'
90    ],
91    stderr => 1,
92   ),
93  "Undefined subroutine &XS::APItest::gv_fetchmeth_type called at -e line "
94 ."2.\n",
95  'no buffer corruption with multiline *{...expr...}'
96;
97
98$_ = "rhubarb";
99is ${no strict; \$_}, "rhubarb", '${no strict; ...}';
100is join("", map{no strict; "rhu$_" } "barb"), 'rhubarb',
101  'map{no strict;...}';
102
103# [perl #123753]
104fresh_perl_is(
105  '$eq = "ok\n"; print $' . "\0eq\n",
106  "ok\n",
107   { stderr => 1 },
108  '$ <null> ident'
109);
110fresh_perl_is(
111  '@eq = "ok\n"; print @' . "\0eq\n",
112  "ok\n",
113   { stderr => 1 },
114  '@ <null> ident'
115);
116fresh_perl_is(
117  '%eq = ("o"=>"k\n"); print %' . "\0eq\n",
118  "ok\n",
119   { stderr => 1 },
120  '% <null> ident'
121);
122fresh_perl_is(
123  'sub eq { "ok\n" } print &' . "\0eq\n",
124  "ok\n",
125   { stderr => 1 },
126  '& <null> ident'
127);
128fresh_perl_is(
129  '$eq = "ok\n"; print ${*' . "\0eq{SCALAR}}\n",
130  "ok\n",
131   { stderr => 1 },
132  '* <null> ident'
133);
134SKIP: {
135    skip "Different output on EBCDIC (presumably)", 3 if $::IS_EBCDIC;
136    fresh_perl_is(
137      qq'"ab}"ax;&\0z\x8Ao}\x82x;', <<gibberish,
138Bareword found where operator expected (Missing operator before "ax"?) at - line 1, near ""ab}"ax"
139syntax error at - line 1, near ""ab}"ax"
140Execution of - aborted due to compilation errors.
141gibberish
142       { stderr => 1 },
143      'gibberish containing &\0z - used to crash [perl #123753]'
144    );
145    fresh_perl_is(
146      qq'"ab}"ax;&{+z}\x8Ao}\x82x;', <<gibberish,
147Bareword found where operator expected (Missing operator before "ax"?) at - line 1, near ""ab}"ax"
148syntax error at - line 1, near ""ab}"ax"
149Execution of - aborted due to compilation errors.
150gibberish
151       { stderr => 1 },
152      'gibberish containing &{+z} - used to crash [perl #123753]'
153    );
154    fresh_perl_is(
155      "\@{\327\n", <<\gibberisi,
156Unrecognized character \xD7; marked by <-- HERE after @{<-- HERE near column 3 at - line 1.
157gibberisi
158       { stderr => 1 },
159      '@ { \327 \n - used to garble output (or fail asan) [perl #128951]'
160    );
161}
162
163fresh_perl_is(
164  '/$a[/<<a',
165  "Missing right curly or square bracket at - line 1, within pattern\n" .
166  "syntax error at - line 1, at EOF\n" .
167  "Execution of - aborted due to compilation errors.\n",
168   { stderr => 1 },
169  '/$a[/<<a with no newline [perl #123712]'
170);
171fresh_perl_is(
172  '/$a[m||/<<a',
173  "Missing right curly or square bracket at - line 1, within pattern\n" .
174  "syntax error at - line 1, at EOF\n" .
175  "Execution of - aborted due to compilation errors.\n",
176   { stderr => 1 },
177  '/$a[m||/<<a with no newline [perl #123712]'
178);
179
180fresh_perl_is(
181  '"@{"',
182  "Missing right curly or square bracket at - line 1, within string\n" .
183  "syntax error at - line 1, at EOF\n" .
184  "Execution of - aborted due to compilation errors.\n",
185   { stderr => 1 },
186  '"@{" [perl #123712]'
187);
188
189fresh_perl_is(
190  '/$0{}/',
191  'syntax error at - line 1, near "{}"' . "\n" .
192  "Execution of - aborted due to compilation errors.\n",
193   { stderr => 1 },
194  '/$0{}/ with no newline [perl #123802]'
195);
196fresh_perl_is(
197  '"\L\L"',
198  'syntax error at - line 1, near "\L\L"' . "\n" .
199  "Execution of - aborted due to compilation errors.\n",
200   { stderr => 1 },
201  '"\L\L" with no newline [perl #123802]'
202);
203fresh_perl_is(
204  '<\L\L>',
205  'syntax error at - line 1, near "\L\L"' . "\n" .
206  "Execution of - aborted due to compilation errors.\n",
207   { stderr => 1 },
208  '<\L\L> with no newline [perl #123802]'
209);
210
211is eval "qq'@\x{ff13}'", "\@\x{ff13}",
212  '"@<fullwidth digit>" [perl #123963]';
213
214fresh_perl_is(
215  "s;\@{<<a;\n",
216  "Can't find string terminator \"a\" anywhere before EOF at - line 1.\n",
217   { stderr => 1 },
218  's;@{<<a; [perl #123995]'
219);
220
221fresh_perl_is(
222  '$_ = q-strict.pm-; 1 ? require : die;'
223 .' print qq-ok\n- if $INC{q-strict.pm-}',
224  "ok\n",
225  {},
226  'foo ? require : bar [perl #128307]'
227);
228
229like runperl(prog => 'sub ub(){0} ub ub', stderr=>1), qr/Bareword found/,
230 '[perl #126482] Assert failure when mentioning a constant twice in a row';
231
232# Other test file(s) (I'm not sure which) can create and then fail to
233# unlink the file t/0 under some circumstances (possibly running under
234# minitest). The next test does the equivalent of "do '0'", which can trip
235# up if there's a real '0' file to load.
236unlink "0";
237
238fresh_perl_is(
239    "do\0"."000000",
240    "",
241    {},
242    '[perl #129069] - no output and valgrind clean'
243);
244
245fresh_perl_is(
246    "00my sub\0",
247    "Missing name in \"my sub\" at - line 1.\n",
248    {},
249    '[perl #129069] - "Missing name" warning and valgrind clean'
250);
251
252fresh_perl_like(
253    "#!perl -i u\nprint 'OK'",
254    qr/OK/,
255    {},
256    '[perl #129336] - #!perl -i argument handling'
257);
258SKIP:
259{
260    ord("A") == 65
261      or skip "These tests won't work on EBCIDIC", 3;
262    fresh_perl_is(
263        "BEGIN{\$^H=hex ~0}\xF3",
264        "Integer overflow in hexadecimal number at - line 1.\n"
265      . "Malformed UTF-8 character: \\xf3 (too short; 1 byte available, need 4) at - line 1.\n"
266      . "Malformed UTF-8 character (fatal) at - line 1.",
267        {},
268        '[perl #128996] - use of PL_op after op is freed'
269    );
270    fresh_perl_like(
271        qq(BEGIN{\$0="";\$^H=-hex join""=>1}""\xFF),
272        qr/Malformed UTF-8 character: \\xff \(too short; 1 byte available, need 13\) at - line 1\./,
273        {},
274        '[perl #128997] - buffer read overflow'
275    );
276    fresh_perl_like(
277        qq(BEGIN{\$^H=0x800000}\n   0m 0\xB5\xB500\xB5\0),
278        qr/Malformed UTF-8 character: \\xb5 \(unexpected continuation byte 0xb5, with no preceding start byte\)/,
279        {},
280        '[perl #129000] read before buffer'
281    );
282}
283# probably only failed under ASAN
284fresh_perl_is(
285    "stat\tt\$#0",
286    <<'EOM',
287$# is no longer supported as of Perl 5.30 at - line 1.
288EOM
289    {},
290    "[perl #129273] heap use after free or overflow"
291);
292
293fresh_perl_like('flock  _$', qr/Not enough arguments for flock/, {stderr => 1},
294                "[perl #129190] intuit_method() invalidates PL_bufptr");
295
296# Below are tests for the single set of Latin1 range paired string delimiters
297# enabled by a feature, when the string isn't UTF-8.  It is more convenient to
298# do all the UTF-8 testing for this feature in t/lib/croak/toke and
299# t/lib/warnings/toke.
300use feature 'evalbytes';
301my $lhs = "\N{U+AB}";
302utf8::downgrade($lhs);
303my $rhs = "\N{U+BB}";
304utf8::downgrade($rhs);
305
306my @warnings;
307local $SIG{__WARN__} = sub {
308    my $warning = $_[0];
309    chomp $warning;
310    push @warnings, ($warning =~ s/\n/\n# /sgr);
311};
312
313evalbytes <<EOS;
314use feature 'extra_paired_delimiters';
315
316my \$warns = q$lhs this string uses paired latin1 delimiters $rhs;
317
318no warnings 'experimental::extra_paired_delimiters';
319
320my \$nowarn = q$lhs this string uses paired latin1 delimiters $rhs;
321no feature 'extra_paired_delimiters';
322my \$warn2= q$lhs this string uses lhs delimiter fore/aft $lhs;
323my \$warn3= q$rhs this string uses rhs delimiter fore/aft $rhs;
324EOS
325
326is($@, "", "Various tests of string delims $lhs/$rhs returned without error");
327is(@warnings, 3, "And the expected number of warnings were generated");
328like($warnings[0],
329     qr/Use of '$lhs' is experimental as a string delimiter at/,
330     'And the first warning is as expected');
331like($warnings[1],
332     qr/Use of '$lhs' is deprecated as a string delimiter at/,
333     'And the second warning is as expected');
334like($warnings[2],
335     qr/Use of '$rhs' is deprecated as a string delimiter at/,
336     'And the third warning is as expected');
337
338undef @warnings;
339evalbytes <<EOS;
340use feature 'extra_paired_delimiters';
341no warnings 'experimental::extra_paired_delimiters';
342my \$warn2= q$lhs this string uses lhs delimiter fore/aft $lhs;
343EOS
344
345like($@, qr/Can't find string terminator "$rhs" anywhere before EOF/,
346     "Using paired delimiter both fore/aft fails as expected");
347is(@warnings, 0, "With no warnings generated");
348
349undef @warnings;
350evalbytes <<EOS;
351no warnings 'deprecated';
352my \$warn2= q$lhs this string uses lhs delimiter fore/aft $rhs;
353EOS
354
355like($@, qr/Can't find string terminator "$lhs" anywhere before EOF/,
356     "Using extra paired delimiter outside scope fails as expected");
357is(@warnings, 0, "With no warnings generated");
358
359undef @warnings;
360evalbytes <<EOS;
361use feature 'extra_paired_delimiters';
362no warnings 'experimental::extra_paired_delimiters';
363my \$warn2= q$rhs this string reverses the delimiters $lhs;
364EOS
365
366is($@, "", "Reversing delimiters works, as expected"
367   . " within scope of extra delims");
368is(@warnings, 0, "With no warnings generated");
369
370undef @warnings;
371evalbytes <<EOS;
372no warnings 'deprecated';
373my \$warn2= q$rhs this string uses lhs delimiter fore/aft $lhs;
374EOS
375
376like($@, qr/Can't find string terminator "$rhs" anywhere before EOF/,
377     "Using terminating paired delimiter fore, opening aft fails as expected"
378   . " outside scope of extra delims");
379is(@warnings, 0, "With no warnings generated");
380
381undef @warnings;
382evalbytes <<EOS;
383no warnings 'experimental::extra_paired_delimiters';
384use feature 'extra_paired_delimiters';
385my \$good= q$lhs this $lhs string has $lhs $lhs nested $rhs paired $rhs $rhs delimiters $rhs;
386EOS
387
388is($@, "", "Using nested extra paired delimiters works");
389is(@warnings, 0, "With no warnings generated");
390
391undef @warnings;
392evalbytes <<EOS;
393no warnings 'experimental::extra_paired_delimiters';
394use feature 'extra_paired_delimiters';
395my \$good= q$lhs this $lhs string has $lhs too few closing $lhs nested $rhs paired rhs $rhs delimiters $rhs;
396EOS
397
398like($@, qr/Can't find string terminator "$rhs" anywhere before EOF/,
399     "Using too few closing delims in nesting fails as expected");
400is(@warnings, 0, "With no warnings generated");
401