Skip to content

Commit 55afe8b

Browse files
committed
Implement GH-15680: Enhance zend_dump_op_array to Properly Represent Non-Printable Characters in String Literals
Replaces GH-15730 as that PR became stale. But instead of introducing a new helper, reuse smart_str_append_escaped(), this also removes the dependency on ext/standard. Closes GH-15730. Closes GH-17277.
1 parent f055447 commit 55afe8b

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PHP NEWS
1414
Volker Dusch)
1515
. Use `clock_gettime_nsec_np()` for high resolution timer on macOS
1616
if available. (timwolla)
17+
. Implement GH-15680 (Enhance zend_dump_op_array to properly represent
18+
non-printable characters in string literals). (nielsdos, WangYihang)
1719

1820
- Curl:
1921
. Added curl_multi_get_handles(). (timwolla)

Zend/Optimizer/zend_dump.c

+19-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "zend_func_info.h"
2424
#include "zend_call_graph.h"
2525
#include "zend_dump.h"
26-
#include "ext/standard/php_string.h"
26+
#include "zend_smart_str.h"
2727

2828
void zend_dump_ht(HashTable *ht)
2929
{
@@ -66,13 +66,27 @@ void zend_dump_const(const zval *zv)
6666
case IS_DOUBLE:
6767
fprintf(stderr, " float(%g)", Z_DVAL_P(zv));
6868
break;
69-
case IS_STRING:;
70-
zend_string *escaped_string = php_addcslashes(Z_STR_P(zv), "\"\\", 2);
69+
case IS_STRING: {
70+
smart_str escaped_string = {0};
71+
smart_str_append_escaped(&escaped_string, Z_STRVAL_P(zv), Z_STRLEN_P(zv));
72+
smart_str_0(&escaped_string);
7173

72-
fprintf(stderr, " string(\"%s\")", ZSTR_VAL(escaped_string));
74+
fprintf(stderr, " string(\"");
7375

74-
zend_string_release(escaped_string);
76+
/* Also escape '"' */
77+
for (size_t i = 0; i < ZSTR_LEN(escaped_string.s); i++) {
78+
if (ZSTR_VAL(escaped_string.s)[i] == '"') {
79+
fprintf(stderr, "\\\"");
80+
} else {
81+
putc(ZSTR_VAL(escaped_string.s)[i], stderr);
82+
}
83+
}
84+
85+
fprintf(stderr, "\")");
86+
87+
smart_str_free_ex(&escaped_string, false);
7588
break;
89+
}
7690
case IS_ARRAY:
7791
fprintf(stderr, " array(...)");
7892
break;

ext/opcache/tests/match/002.phpt

+2-4
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,14 @@ test:
4444
; (lines=2, args=0, vars=0, tmps=0)
4545
; (after optimizer)
4646
; %s
47-
0000 ECHO string("No match
48-
")
47+
0000 ECHO string("No match\n")
4948
0001 RETURN null
5049

5150
test2:
5251
; (lines=2, args=0, vars=0, tmps=0)
5352
; (after optimizer)
5453
; %s
55-
0000 ECHO string("No match
56-
")
54+
0000 ECHO string("No match\n")
5755
0001 RETURN null
5856
No match
5957
No match

ext/opcache/tests/opt/dce_009.phpt

+2-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ Loop::test:
5050
; (lines=3, args=0, vars=0, tmps=0)
5151
; (after optimizer)
5252
; %sdce_009.php:4-10
53-
0000 ECHO string("Start
54-
")
55-
0001 ECHO string("Done
56-
")
53+
0000 ECHO string("Start\n")
54+
0001 ECHO string("Done\n")
5755
0002 RETURN null
5856

5957
Loop::test2:

ext/opcache/tests/opt/sccp_032.phpt

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ $_main:
3636
0004 INIT_FCALL 1 %d string("var_export")
3737
0005 SEND_VAR CV0($x) 1
3838
0006 DO_ICALL
39-
0007 ECHO string("
40-
")
39+
0007 ECHO string("\n")
4140
0008 JMP 0003
4241
0009 FE_FREE V1
4342
0010 RETURN int(1)

0 commit comments

Comments
 (0)