php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70983 commenting a printf line causes script to fail with "Unexpected end of file"
Submitted: 2015-11-27 06:35 UTC Modified: 2017-01-09 05:48 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at rossw dot net Assigned:
Status: Wont fix Package: Reproducible crash
PHP Version: 5.6.16 OS: FreeBSD 10.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at rossw dot net
New email:
PHP Version: OS:

 

 [2015-11-27 06:35 UTC] php at rossw dot net
Description:
------------
A quoted string containing ?> is treated correctly inside an active code block, but causes errors due to being handled differently if the line is commented out.

I can see how it could be argued that ?> should not appear, but there are times you might actually WANT to print it. Simply commenting out the line should not materially alter the way the (otherwise perfectly healthy) script runs.

Test script:
---------------
<?php

	printf("This is a test\n");
	if(1 > 0) 
	{
//		printf("?> Debug code\n");
		printf("Got here\n");
	}
	printf("Finished\n");
?>


Expected result:
----------------
php is seeing the "?> Debug code" as quoted text, and handles it correctly while the line is not commented. As soon as the line is commented, php is seeing the quoted ?> as the end of the code and errors.


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-02 12:09 UTC] codekestrel at googlemail dot com
That behaviour is explicitly defined here:

<ST_IN_SCRIPTING>"#"|"//" {
        while (YYCURSOR < YYLIMIT) {
                switch (*YYCURSOR++) {
                        case '\r':
                                if (*YYCURSOR == '\n') {
                                        YYCURSOR++;
                                }
                                /* fall through */
                        case '\n':
                                CG(zend_lineno)++;
                                break;
                        case '?':
                                if (*YYCURSOR == '>') {
                                        YYCURSOR--;
                                        break;
                                }
                                /* fall through */
                        default:
                                continue;
                }

                break;
        }

        yyleng = YYCURSOR - SCNG(yy_text);

        RETURN_TOKEN(T_COMMENT);
}

Removing the "case '?':" clause would break the following inline code:

<?php echo "Hello, World!"; // my comment ?> Remainder

Which should print: Hello, World! Remainder

Everything on the same line after the comment including after the closing tag are commented out.
 [2016-07-26 16:05 UTC] dave at mudsite dot com
I understand this request.  PHP ought to know if this is a single-line execute, and handle accordingly.

  <?php echo "hello"; // comment ?> output
and
  <?= echo "hello"; // comment ?> output
should each run with output:
  hello output

That said, the request here:
<?php
  if (foo) {
    // ?>
  }
}
?>

Should not throw a syntax error.  I have no idea what the consensus on this kind of issue is, however, (i've done) and it would be simple enough to track what line <?php, or <?, or <?= start on, and what line we see // ?>, and if they aren't the same, then ignore the ?> inside the comment.

ie:
if (*YYCURSOR == '>' && (CG(zend_opentag_lineno) == CG(zend_lineno))) {
 [2017-01-09 05:48 UTC] krakjoe@php.net
-Status: Open +Status: Wont fix
 [2017-01-09 05:48 UTC] krakjoe@php.net
Since this is against a security fix only branch, and since a patch against a supported branch would need to be different, please can you open a PR on github against a supported branch.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 02:01:26 2024 UTC