php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70210 Parser: some ASTs may get wrong line numbers
Submitted: 2015-08-07 23:31 UTC Modified: 2015-08-08 06:32 UTC
Votes:3
Avg. Score:2.3 ± 0.9
Reproduced:0 of 1 (0.0%)
From: duckki at synopsys dot com Assigned: nikic
Status: Assigned Package: *Compile Issues
PHP Version: 7.0.0beta3 OS:
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-08-07 23:31 UTC] duckki at synopsys dot com
Description:
------------
I've noticed some AST nodes (arrays, ++, yield, etc.) may have the wrong line numbers. Here's an example PHP code:

1: <?php
2: yield
3: 
4: // gap
5: 
6: function() { yield; };

PHP 7.0.0beta3 will report:

Fatal error: The "yield" expression can only be used inside a function in wrong-line-number.php on line 6

That message is wrong. The yield on line 6 is correct. The fatal error is actually on line 2.

We can see the AST has the wrong line number. Here's the output from php-ast.

AST_STMT_LIST @ 1
    0: AST_YIELD @ 6
        0: AST_CLOSURE @ 6-6
            flags: 0
            name: {closure}
            0: AST_PARAM_LIST @ 6
            1: null
            2: AST_STMT_LIST @ 6
                0: AST_YIELD @ 6
                    0: null
                    1: null
            3: null
        1: null

The first AST_YIELD has the line number 6. After quick inspection, I see the parser often delay the construction of AST node and capture the line number after parsing 1~2 tokens, which may be a line or more below the actual beginning of the AST node.

The consequences are incorrect line numbers from runtime errors.


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

// gap

function() { yield; };


Expected result:
----------------
Fatal error: The "yield" expression can only be used inside a function in wrong-line-number.php on line 2

Actual result:
--------------
Fatal error: The "yield" expression can only be used inside a function in wrong-line-number.php on line 6

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-08 06:32 UTC] nikic@php.net
Related to: https://bugs.php.net/bug.php?id=70024

This would be fixed by using bison location tracking.
 [2015-08-08 06:32 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sat Aug 19 14:01:35 2017 UTC