Skip to content

Fix ast export in array #4324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Fix ast export in array #4324

wants to merge 3 commits into from

Conversation

sunnyeo
Copy link
Contributor

@sunnyeo sunnyeo commented Jun 28, 2019

zend_ast_export function has a bug when export an array with reference.
It misses reference sign (&).

Test case:

<?php
$var = 1;
$arr = array(&$var, $var);

Expected result:

$var = 1;
$arr = [&$var, $var];

Actual result:

$var = 1;
$arr = [$var, $var];

Copy link
Contributor Author

@sunnyeo sunnyeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case:

<?php
$var = 'test';
$str = "$var, $var[1], {$var}[], {$var[1]}[], ${var}[], ${var[1]}[]"

Expected result:

$var = 'test';
$str = "$var, {$var[1]}, {$var}[], {$var[1]}[], {$var}[], {$var[1]}[]"

Actual result:

$var = 'test';
$str = "$var, {$var[1]}, $var[], {$var[1]}[], $var[], {$var[1]}[]"
  • If ZEND_AST_VAR's next list starts with [, ZEND_AST_VAR should be {ZEND_AST_VAR}.
    • $var[] --> {$var}[]
  • If not, it occurs PHP Parse error: syntax error, unexpected ']', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

@nikic
Copy link
Member

nikic commented Jun 28, 2019

@sunnyeo Can you please add these as actual tests? Zend/tests/assert/expect_015.phpt can be used for this.

Zend/zend_ast.c Outdated
!zend_ast_valid_var_char(
*Z_STRVAL_P(
zend_ast_get_zval(list->child[i + 1]))))) {
zend_ast_get_zval(list->child[i + 1])))))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is becoming quite hard to read. Can you maybe extract a function zend_ast_var_needs_braces() that performs these checks?

@@ -1062,6 +1062,13 @@ static ZEND_COLD int zend_ast_valid_var_name(const char *s, size_t len)
return 1;
}

static ZEND_COLD int zend_ast_var_needs_braces(char ch)
{
if (ch != '[' && !zend_ast_valid_var_char(ch))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if is not necessary here:

Suggested change
if (ch != '[' && !zend_ast_valid_var_char(ch))
return ch != '[' && !zend_ast_valid_var_char(ch);

@php-pulls php-pulls closed this in f7327b6 Jun 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants