From 75417838f9b7d7cbf962ca9e4364740b809a2ce9 Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Mon, 29 May 2023 12:54:51 +0300 Subject: [PATCH 1/6] Add string output escaping into zend dump (phpdbg + opcache debug) --- Zend/Optimizer/zend_dump.c | 6 +++++- sapi/phpdbg/tests/print_001.phpt | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Zend/Optimizer/zend_dump.c b/Zend/Optimizer/zend_dump.c index bc697ba8ba9e..f65ebd2838cd 100644 --- a/Zend/Optimizer/zend_dump.c +++ b/Zend/Optimizer/zend_dump.c @@ -23,6 +23,7 @@ #include "zend_func_info.h" #include "zend_call_graph.h" #include "zend_dump.h" +#include "ext/standard/php_string.h" void zend_dump_ht(HashTable *ht) { @@ -49,6 +50,8 @@ void zend_dump_ht(HashTable *ht) void zend_dump_const(const zval *zv) { + zend_string *escaped_string; + switch (Z_TYPE_P(zv)) { case IS_NULL: fprintf(stderr, " null"); @@ -66,7 +69,8 @@ void zend_dump_const(const zval *zv) fprintf(stderr, " float(%g)", Z_DVAL_P(zv)); break; case IS_STRING: - fprintf(stderr, " string(\"%s\")", Z_STRVAL_P(zv)); + escaped_string = php_addcslashes(zv->value.str, "\"\\", 2); + fprintf(stderr, " string(\"%s\")", escaped_string->val); break; case IS_ARRAY: fprintf(stderr, " array(...)"); diff --git a/sapi/phpdbg/tests/print_001.phpt b/sapi/phpdbg/tests/print_001.phpt index c25c5178fef4..a981cb0001f6 100644 --- a/sapi/phpdbg/tests/print_001.phpt +++ b/sapi/phpdbg/tests/print_001.phpt @@ -29,7 +29,7 @@ Foo\Bar::Foo: ; (lines=5, args=1, vars=1, tmps=1) ; %s:5-7 L0005 0000 CV0($bar) = RECV 1 -L0006 0001 INIT_NS_FCALL_BY_NAME 1 string("Foo\var_dump") +L0006 0001 INIT_NS_FCALL_BY_NAME 1 string("Foo\\var_dump") L0006 0002 SEND_VAR_EX CV0($bar) 1 L0006 0003 DO_FCALL L0007 0004 RETURN null @@ -44,10 +44,10 @@ prompt> [Context %s (9 ops)] $_main: ; (lines=9, args=0, vars=0, tmps=4) ; %s:1-21 -L0018 0000 V0 = NEW 0 string("Foo\Bar") +L0018 0000 V0 = NEW 0 string("Foo\\Bar") L0018 0001 DO_FCALL L0018 0002 INIT_METHOD_CALL 1 V0 string("Foo") -L0018 0003 SEND_VAL_EX string("test") 1 +L0018 0003 SEND_VAL_EX string("test \"quotes\"") 1 L0018 0004 DO_FCALL L0019 0005 INIT_FCALL %d %d string("foo") L0019 0006 SEND_VAL string("test") 1 @@ -72,6 +72,6 @@ namespace { var_dump(strrev($baz)); } - (new \Foo\Bar)->Foo("test"); + (new \Foo\Bar)->Foo('test "quotes"'); foo("test"); } From 71d4e12605798bd9f568d2b7c3eb5e802f73b8a0 Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Mon, 29 May 2023 13:08:14 +0300 Subject: [PATCH 2/6] Use ZSTR_VAL macro instead direct string access --- Zend/Optimizer/zend_dump.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zend/Optimizer/zend_dump.c b/Zend/Optimizer/zend_dump.c index f65ebd2838cd..0b10f9c2b8e1 100644 --- a/Zend/Optimizer/zend_dump.c +++ b/Zend/Optimizer/zend_dump.c @@ -70,7 +70,7 @@ void zend_dump_const(const zval *zv) break; case IS_STRING: escaped_string = php_addcslashes(zv->value.str, "\"\\", 2); - fprintf(stderr, " string(\"%s\")", escaped_string->val); + fprintf(stderr, " string(\"%s\")", ZSTR_VAL(escaped_string)); break; case IS_ARRAY: fprintf(stderr, " array(...)"); From 52d1cf707f4534f7eb36349534d5eac089a43bc3 Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Mon, 29 May 2023 13:09:40 +0300 Subject: [PATCH 3/6] Move "escaped_string" into local switch/case scope --- Zend/Optimizer/zend_dump.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Zend/Optimizer/zend_dump.c b/Zend/Optimizer/zend_dump.c index 0b10f9c2b8e1..008de30dea8e 100644 --- a/Zend/Optimizer/zend_dump.c +++ b/Zend/Optimizer/zend_dump.c @@ -50,8 +50,6 @@ void zend_dump_ht(HashTable *ht) void zend_dump_const(const zval *zv) { - zend_string *escaped_string; - switch (Z_TYPE_P(zv)) { case IS_NULL: fprintf(stderr, " null"); @@ -68,7 +66,8 @@ void zend_dump_const(const zval *zv) case IS_DOUBLE: fprintf(stderr, " float(%g)", Z_DVAL_P(zv)); break; - case IS_STRING: + case IS_STRING:; + zend_string *escaped_string; escaped_string = php_addcslashes(zv->value.str, "\"\\", 2); fprintf(stderr, " string(\"%s\")", ZSTR_VAL(escaped_string)); break; From 41f7289cc95908c4ee8b17a3ea3bf88dbc74fc6e Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Mon, 29 May 2023 13:10:48 +0300 Subject: [PATCH 4/6] Add zend_string_release --- Zend/Optimizer/zend_dump.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Zend/Optimizer/zend_dump.c b/Zend/Optimizer/zend_dump.c index 008de30dea8e..fc263b97b6b9 100644 --- a/Zend/Optimizer/zend_dump.c +++ b/Zend/Optimizer/zend_dump.c @@ -68,8 +68,11 @@ void zend_dump_const(const zval *zv) break; case IS_STRING:; zend_string *escaped_string; + escaped_string = php_addcslashes(zv->value.str, "\"\\", 2); fprintf(stderr, " string(\"%s\")", ZSTR_VAL(escaped_string)); + + zend_string_release(escaped_string); break; case IS_ARRAY: fprintf(stderr, " array(...)"); From 3f29d456c98593647fb7c365bbd6ba0d86633bd3 Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Mon, 29 May 2023 13:12:07 +0300 Subject: [PATCH 5/6] Add Z_STR_P macro instead direct string access --- Zend/Optimizer/zend_dump.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zend/Optimizer/zend_dump.c b/Zend/Optimizer/zend_dump.c index fc263b97b6b9..770199fa38e7 100644 --- a/Zend/Optimizer/zend_dump.c +++ b/Zend/Optimizer/zend_dump.c @@ -69,7 +69,7 @@ void zend_dump_const(const zval *zv) case IS_STRING:; zend_string *escaped_string; - escaped_string = php_addcslashes(zv->value.str, "\"\\", 2); + escaped_string = php_addcslashes(Z_STR_P(zv), "\"\\", 2); fprintf(stderr, " string(\"%s\")", ZSTR_VAL(escaped_string)); zend_string_release(escaped_string); From cb23aa42ce3193498f712416913f62289f18b787 Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Mon, 29 May 2023 14:29:10 +0300 Subject: [PATCH 6/6] Merge zend_string declaration and its assigment in one stmt --- Zend/Optimizer/zend_dump.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Zend/Optimizer/zend_dump.c b/Zend/Optimizer/zend_dump.c index 770199fa38e7..9eaca19f18f5 100644 --- a/Zend/Optimizer/zend_dump.c +++ b/Zend/Optimizer/zend_dump.c @@ -67,9 +67,8 @@ void zend_dump_const(const zval *zv) fprintf(stderr, " float(%g)", Z_DVAL_P(zv)); break; case IS_STRING:; - zend_string *escaped_string; + zend_string *escaped_string = php_addcslashes(Z_STR_P(zv), "\"\\", 2); - escaped_string = php_addcslashes(Z_STR_P(zv), "\"\\", 2); fprintf(stderr, " string(\"%s\")", ZSTR_VAL(escaped_string)); zend_string_release(escaped_string);