Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Zend/Zend.m4
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ int emu(const opcode_handler_t *ip, void *fp) {
while ((*ip)());
FP = orig_fp;
IP = orig_ip;
return 0;
}], [])],
[php_cv_have_global_register_vars=yes],
[php_cv_have_global_register_vars=no])
Expand Down
26 changes: 20 additions & 6 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,23 @@ static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, const zen
}
}

static ZEND_COLD void zend_ast_export_quoted_str(smart_str *str, zend_string *s)
{
size_t i;

for (i = 0; i < ZSTR_LEN(s); i++) {
if ((unsigned char) ZSTR_VAL(s)[i] < ' ') {
smart_str_appendc(str, '"');
zend_ast_export_qstr(str, '"', s);
smart_str_appendc(str, '"');
return;
}
}
smart_str_appendc(str, '\'');
zend_ast_export_str(str, s);
smart_str_appendc(str, '\'');
}

static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent)
{
while (indent > 0) {
Expand Down Expand Up @@ -1907,9 +1924,7 @@ static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int p
str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ true);
break;
case IS_STRING:
smart_str_appendc(str, '\'');
zend_ast_export_str(str, Z_STR_P(zv));
smart_str_appendc(str, '\'');
zend_ast_export_quoted_str(str, Z_STR_P(zv));
break;
case IS_ARRAY: {
zend_long idx;
Expand All @@ -1924,9 +1939,8 @@ static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int p
smart_str_appends(str, ", ");
}
if (key) {
smart_str_appendc(str, '\'');
zend_ast_export_str(str, key);
smart_str_appends(str, "' => ");
zend_ast_export_quoted_str(str, key);
smart_str_appends(str, " => ");
} else {
smart_str_append_long(str, idx);
smart_str_appends(str, " => ");
Expand Down
39 changes: 39 additions & 0 deletions ext/standard/tests/assert/gh22290.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-22290: AST pretty printing does not correctly handle strings containing NUL
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php

try {
$string = "Foo\x00bar";
assert(!str_contains($string, "\x00"));
} catch (AssertionError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(["a\x00b" => 1] === []);
} catch (AssertionError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert("tab\there" === "");
} catch (AssertionError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(str_contains("plain", "zzz"));
} catch (AssertionError $e) {
echo $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
assert(!str_contains($string, "\000"))
assert(["a\000b" => 1] === [])
assert("tab\there" === '')
assert(str_contains('plain', 'zzz'))
Loading