Semgrep: [PHP] Unable to parse $var[] = ... array append operator

Created on 7 Oct 2020  路  7Comments  路  Source: returntocorp/semgrep

Describe the bug
Some PHP files could not be parsed

To Reproduce

Expected behavior
The files should be parsed

Screenshots
Screenshot 2020-10-07 at 10 08 03

Other Affected Files

Environment
Running v0.27.0 on MacOSX 10.15.6 (installed via Brew)

bug php

Most helpful comment

@vortfu $var[] = $val is how you append to arrays in PHP

I understand what the code does, but I think it's what is causing this parse error in semgrep/pfff :)

Minimalized test case as follows:

vortfu:~ vortfu$ cat semgrep-parse-error-001.php 
<?php
$x = array();
$x[] = 1;

vortfu:~ vortfu$ php -l semgrep-parse-error-001.php 
No syntax errors detected in semgrep-parse-error-001.php

vortfu:~ vortfu$ semgrep --version
0.27.0

vortfu:~ vortfu$ semgrep --verbose semgrep-parse-error-001.php 
running 1 rules...
semgrep warn: parse error
  --> semgrep-parse-error-001.php:1
1 | <?php
  | 
= help: If the code appears to be valid, this may be a semgrep bug.
Could not parse semgrep-parse-error-001.php as php

ran 1 rules on 1 files: 0 findings
1 files could not be analyzed; run with --verbose for details or run with --strict to exit non-zero if any file cannot be analyzed

All 7 comments

Hi @erwanlr,

Thanks for bringing this to our attention. I'll investigate and get back to you.

This seems like a bug in our PHP parsing. Pulling in @aryx.

Appears to be related to the $var[] = $val array shorthand?

@vortfu $var[] = $val is how you append to arrays in PHP

@vortfu $var[] = $val is how you append to arrays in PHP

I understand what the code does, but I think it's what is causing this parse error in semgrep/pfff :)

Minimalized test case as follows:

vortfu:~ vortfu$ cat semgrep-parse-error-001.php 
<?php
$x = array();
$x[] = 1;

vortfu:~ vortfu$ php -l semgrep-parse-error-001.php 
No syntax errors detected in semgrep-parse-error-001.php

vortfu:~ vortfu$ semgrep --version
0.27.0

vortfu:~ vortfu$ semgrep --verbose semgrep-parse-error-001.php 
running 1 rules...
semgrep warn: parse error
  --> semgrep-parse-error-001.php:1
1 | <?php
  | 
= help: If the code appears to be valid, this may be a semgrep bug.
Could not parse semgrep-parse-error-001.php as php

ran 1 rules on 1 files: 0 findings
1 files could not be analyzed; run with --verbose for details or run with --strict to exit non-zero if any file cannot be analyzed

Thx a lot for the minimal test @vortfu

Yes internally semgrep is parsing correctly the file, but the part that transform the PHP program in a generic AST has some TODOs related to those assignments. I'll fix it.

Was this page helpful?
0 / 5 - 0 ratings