php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PHP Interactive shell input state incorrectly handles quoted heredoc literals.

Open MorganLOCode opened this issue 2 years ago • 1 comments

Description

In the interactive shell, starting a heredoc statement puts the shell into a state (indicated by prefixing each input line with <<< instead of php) where subsequent lines are part of the string literal until the delimiter is entered. But if the opening delimiter is double-quoted the input state remains stuck in <<< until a double-quoted delimiter is encountered, when execution resumes.

Interactive shell

php > $I = 'You';
php > $foo = <<<"EOF"
<<< > How do $I
<<< > compare?
<<< > EOF;
<<< > $k = 45+19;
<<< > echo $foo;
<<< > echo " When I'm $k?";
<<< > "EOF";
How do You
compare? When I'm 64?

(I'm guessing that the extra "EOF"; line ends up being interpreted as a complete albeit do-nothing statement.)

What I would expect is the same as if double-quotes had not been used: parsing and execution resumes following the closing delimiter, and doesn't require the quoted closing delimiter to unwedge the input state:

Interactive shell

php > $I = 'You';
php > $foo = <<< EOF
<<< > How do $I
<<< > compare?
<<< > EOF;
php > $k = 45+19;
php > echo $foo;
How do You
compare?
php > echo " When I'm $k?";
 When I'm 64?
php >

For the record, single-quoted nowdoc strings behave as expected:

Interactive shell

php > $I = 'You';
php > $foo = <<< 'EOF'
<<< > How do $I
<<< > compare?
<<< > EOF;
php > $k = 45+19;
php > echo $foo;
How do $I
compare?
php > echo " When I'm $k?";
 When I'm 64?
php >

PHP Version

PHP 8.3.7 (also present in 8.3.6).

Operating System

Windows 10

MorganLOCode avatar May 10 '24 03:05 MorganLOCode

I cannot believe it. I ran into this bug just now, came to report it and see that it was found and fixed only a week ago, when it's been around for over 16 years (if the implemented date in the patch from this RFC is accurate).

Roy-Orbison avatar May 17 '24 05:05 Roy-Orbison