1 # tests for heredocs besides what is tested in base/lex.t
12 # heredoc without newline (#65838)
14 my $string = <<'HEREDOC';
18 my $code = "<<'HEREDOC';\n${string}HEREDOC"; # HD w/o newline, in eval-string
19 my $hd = eval $code or warn "$@ ---";
20 is($hd, $string, "no terminating newline in string-eval");
26 my $string = "testing for 65838";
29 "print <<'HEREDOC';\n${string}\nHEREDOC",
32 "heredoc at EOF without trailing newline"
36 qq(print <<"";\n$string\n),
38 { switches => ['-X'] },
39 "blank-terminated heredoc at EOF"
42 qq(print <<""\n$string\n),
44 { switches => ['-X'] },
45 "blank-terminated heredoc at EOF and no semicolon"
48 "print <<foo\r\nick and queasy\r\nfoo\r\n",
50 { switches => ['-X'] },
51 "crlf-terminated heredoc"
54 "print qq|\${\\<<foo}|\nick and queasy\nfoo\n",
56 { switches => ['-w'], stderr => 1 },
57 'no warning for qq|${\<<foo}| in file'
62 # here-doc parse failures
65 "print <<HEREDOC;\nwibble\n HEREDOC",
66 qr/find string terminator/,
68 "string terminator must start at newline"
71 # Loop over various lengths to try to force at least one to cause a
72 # reallocation in S_scan_heredoc()
73 # Timing on a modern machine suggests that this loop executes in less than
74 # 0.1s, so it's a very small cost for the default build. The benefit is
75 # that building with ASAN will reveal the bug and any related regressions.
78 qq(print <<"";\n) . "x" x $_,
79 qr/find string terminator/,
80 { switches => ['-X'] },
81 "empty string terminator still needs a newline (length $_)"
86 "print <<ThisTerminatorIsLongerThanTheData;\nno more newlines",
87 qr/find string terminator/,
89 "long terminator fails correctly"
92 # this would read freed memory
95 # valgrind and asan reports an error between these two lines
96 qr/^Number found where operator expected at - line 1, near "<<""0"\s+\(Missing operator/,
98 "don't use an invalid oldoldbufptr"
101 # also read freed memory, but got an invalid oldoldbufptr in a different way
104 # valgrind and asan reports an error between these two lines
107 "don't use an invalid oldoldbufptr (some more)"
110 # [perl #125540] this asserted or crashed
113 qr/Can't find string terminator "" anywhere before EOF at - line 1\./,
115 "Don't assert parsing a here-doc if we hit EOF early"
118 # [perl #129064] heap-buffer-overflow S_scan_heredoc
121 # valgrind and asan reports an error between these two lines
122 qr/^Unterminated delimiter for here document/,
124 "delimcpy(): handle last char being backslash properly"
131 my $string = 'some data';
140 q{' EOF '} => " EOF ",
143 q{" EOF "} => " EOF ",
148 my @modifiers = ("~", "~ ");
150 my @script_ends = ("", "\n");
154 for my $start_delim (sort keys %delimiters) {
155 my $end_delim = $delimiters{$start_delim};
157 for my $modifier (@modifiers) {
158 # For now, "<<~ EOF" and "<<~ \EOF" aren't allowed
159 next if $modifier =~ /\s/ && $start_delim !~ /('|")/n;
161 for my $script_end (@script_ends) {
163 my $test = "print <<$modifier$start_delim\n $string\n"
164 . " $end_delim$script_end";
168 "Indented here-doc: <<$modifier$start_delim with end delim '$end_delim'" . ($script_end ? "\\n" : ""),
172 my $safe_start_delim = $start_delim =~ s/'/\\'/gr;
175 eval 's//<<$modifier$safe_start_delim.\"\"/e; print
177 $end_delim$script_end'
183 "Eval'd Indented here-doc: <<$modifier$start_delim with end delim '$end_delim'" . ($script_end ? "\\n" : ""),
191 "print <<~EOF;\n\t \t$string\n\t \tEOF\n",
193 "indented here-doc with tabs and spaces",
197 "print <<~EOF;\n\t \tx EOF\n\t \t$string\n\t \tEOF\n",
199 "Embedded delimiter ignored",
203 "print <<~EOF;\n\t \t$string\n\t \tTEOF",
204 "Can't find string terminator \"EOF\" anywhere before EOF at - line 1.",
205 "indented here-doc missing terminator error is correct"
209 "print <<~EOF;\n $string\n$string\n $string\n $string\n EOF",
210 "Indentation on line 1 of here-doc doesn't match delimiter at - line 1.\n",
211 "indented here-doc with bad indentation"
215 "print <<~EOF;\n $string\n $string\n$string\n $string\n EOF",
216 "Indentation on line 3 of here-doc doesn't match delimiter at - line 1.\n",
217 "indented here-doc with bad indentation"
220 # If our delim is " EOF ", make sure other spaced version don't match
222 "print <<~' EOF ';\n $string\n EOF\nEOF \n EOF \n EOF \n",
223 " $string\n EOF\nEOF \n EOF \n",
224 "indented here-doc matches final delimiter correctly"
227 for my $test (@tests) {
231 { switches => ['-w'], stderr => 1 },