Skip to content

zend_ast: Preserve heredocs in zend_ast_export() #18690

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Zend/tests/heredoc_nowdoc/heredoc_print.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Dump heredoc tags
--INI--
zend.assertions=1
--FILE--
<?php
try {
assert(false &&
<<<HTML
val inside
HTML
Comment on lines +9 to +11
Copy link
Member

Choose a reason for hiding this comment

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

What would happen if:

  • The string contains a line HEREDOC?
  • The string contains special characters that would be escaped by zend_ast_export_str()?

Is it guaranteed that the output is valid PHP in all cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll cover these with tests, thanks.

);
} catch(\Throwable $e) {
echo $e->getMessage();
}

?>
--EXPECT--
assert(false && <<<HEREDOC
val inside
HEREDOC)
18 changes: 13 additions & 5 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1630,8 +1630,10 @@ static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, zend_ast_list *lis
smart_str_appendc(str, '}');
}

static ZEND_COLD void zend_ast_export_zval(smart_str *str, zval *zv, int priority, int indent)
static ZEND_COLD void zend_ast_export_zval(smart_str *str, zend_ast *ast, int priority, int indent)
{
zval *zv = zend_ast_get_zval(ast);

ZVAL_DEREF(zv);
switch (Z_TYPE_P(zv)) {
case IS_NULL:
Expand All @@ -1651,9 +1653,15 @@ static ZEND_COLD void zend_ast_export_zval(smart_str *str, zval *zv, int priorit
str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ false);
break;
case IS_STRING:
smart_str_appendc(str, '\'');
zend_ast_export_str(str, Z_STR_P(zv));
smart_str_appendc(str, '\'');
if (ast->attr & ZEND_NAME_HEREDOC) {
smart_str_appends(str, "<<<HEREDOC\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
smart_str_appends(str, "<<<HEREDOC\n");
smart_str_appends(str, "<<<'EOD'\n");

zend_ast_export_str(str, Z_STR_P(zv));
smart_str_appends(str, "\nHEREDOC");
} else {
smart_str_appendc(str, '\'');
zend_ast_export_str(str, Z_STR_P(zv));
smart_str_appendc(str, '\'');
}
break;
case IS_ARRAY: {
zend_long idx;
Expand Down Expand Up @@ -1878,7 +1886,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
switch (ast->kind) {
/* special nodes */
case ZEND_AST_ZVAL:
zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent);
zend_ast_export_zval(str, ast, priority, indent);
break;
case ZEND_AST_CONSTANT: {
zend_string *name = zend_ast_get_constant_name(ast);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
#define ZEND_NAME_FQ 0
#define ZEND_NAME_NOT_FQ 1
#define ZEND_NAME_RELATIVE 2
#define ZEND_NAME_HEREDOC 3

/* ZEND_FETCH_ flags in class name AST of new const expression must not clash with ZEND_NAME_ flags */
#define ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT 2
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -3132,6 +3132,10 @@ emit_token_with_val:
if (PARSER_MODE()) {
ZEND_ASSERT(Z_TYPE_P(zendlval) != IS_UNDEF);
elem->ast = zend_ast_create_zval_with_lineno(zendlval, start_line);
zend_ptr_stack *stack = &SCNG(heredoc_label_stack);
if (stack->elements) {
elem->ast->attr |= ZEND_NAME_HEREDOC;
}
}

emit_token:
Expand Down
Loading