Skip to content

Commit

Permalink
Fixed issue #2214: Array keys aren't escaped in traces
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Nov 29, 2023
1 parent b9f8a4a commit 83c3d78
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/lib/var_export_line.c
Expand Up @@ -35,19 +35,15 @@ static int xdebug_array_element_export(zval *zv_nptr, zend_ulong index_key, zend
if (HASH_KEY_IS_NUMERIC(hash_key)) { /* numeric key */
xdebug_str_add_fmt(str, XDEBUG_INT_FMT " => ", index_key);
} else { /* string key */
zend_string *tmp, *tmp2;
zend_string *tmp_zstr;

tmp_zstr = php_addcslashes(hash_key, (char*) "'\\\0..\37", 7);

tmp = php_str_to_str(ZSTR_VAL(hash_key), ZSTR_LEN(hash_key), (char*) "'", 1, (char*) "\\'", 2);
tmp2 = php_str_to_str(ZSTR_VAL(tmp), ZSTR_LEN(tmp), (char*) "\0", 1, (char*) "\\0", 2);
if (tmp) {
zend_string_release(tmp);
}
xdebug_str_addc(str, '\'');
if (tmp2) {
xdebug_str_add_zstr(str, tmp2);
zend_string_release(tmp2);
}
xdebug_str_add_zstr(str, tmp_zstr);
xdebug_str_add_literal(str, "' => ");

zend_string_release(tmp_zstr);
}
xdebug_var_export_line(zv, str, level + 2, debug_zval, options);
xdebug_str_add_literal(str, ", ");
Expand Down
24 changes: 24 additions & 0 deletions tests/tracing/bug02214-001.phpt
@@ -0,0 +1,24 @@
--TEST--
Test for bug #2214: Array keys aren't escaped in traces
--INI--
xdebug.mode=trace
xdebug.start_with_request=no
xdebug.trace_format=0
xdebug.collect_return=0
xdebug.collect_assignments=0
--FILE--
<?php
require_once 'capture-trace.inc';

require dirname( __FILE__ ) . '/bug02214.inc';

xdebug_stop_trace();
?>
--EXPECTF--
TRACE START [%d-%d-%d %d:%d:%d.%d]
%w%f %w%d -> dirname($path = '%sbug02214-001.php') %sbug02214-001.php:4
%w%f %w%d -> require(%sbug02214.inc) %sbug02214-001.php:4
%w%f %w%d -> func($a = ['\n' => '\n', '\r' => '\r', '\r\n' => '\r\n']) %sbug02214.inc:%d
%w%f %w%d -> xdebug_stop_trace() %sbug02214-001.php:6
%w%f %w%d
TRACE END [%d-%d-%d %d:%d:%d.%d]
30 changes: 30 additions & 0 deletions tests/tracing/bug02214-002.phpt
@@ -0,0 +1,30 @@
--TEST--
Test for bug #2214: Array keys aren't escaped in traces
--INI--
xdebug.mode=trace
xdebug.start_with_request=no
xdebug.trace_format=1
xdebug.collect_return=0
xdebug.collect_assignments=0
--FILE--
<?php
require_once 'capture-trace.inc';

require dirname( __FILE__ ) . '/bug02214.inc';

xdebug_stop_trace();
?>
--EXPECTF--
Version: %s
File format: 4
TRACE START [%d-%d-%d %d:%d:%d.%d]
2 1 1 %f %d
2 7 0 %f %d dirname 0 %sbug02214-002.php 4 1 '%sbug02214-002.php'
2 7 1 %f %d
2 8 0 %f %d require 1 %sbug02214.inc %sbug02214-002.php 4 0
3 9 0 %f %d func 1 %sbug02214.inc %d 1 ['\n' => '\n', '\r' => '\r', '\r\n' => '\r\n']
3 9 1 %f %d
2 8 1 %f %d
2 10 0 %f %d xdebug_stop_trace 0 %sbug02214-002.php 6 0
%f %d
TRACE END [%d-%d-%d %d:%d:%d.%d]
11 changes: 11 additions & 0 deletions tests/tracing/bug02214.inc
@@ -0,0 +1,11 @@
<?php
function func(array $a) {
return $a;
}

func([
"\n" => "\n",
"\r" => "\r",
"\r\n" => "\r\n",
]);
?>

0 comments on commit 83c3d78

Please sign in to comment.