Skip to content

Commit 5fd7888

Browse files
committed
[Proposal] Make string length for getTraceAsString() configurable
Add a `throwable_string_param_max_len` ini setting. (same suffix as `log_errors_max_len`) Allow values between 15 and 1000000. Future RFCs may expand that range. Discussion: https://externals.io/message/110717
1 parent c9bc7dd commit 5fd7888

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

Zend/tests/exception_024.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
throwable_string_param_max_len ini setting
3+
--INI--
4+
throwable_string_param_max_len = 23
5+
--FILE--
6+
<?php
7+
8+
function main($arg) {
9+
throw new Exception();
10+
}
11+
main('123456789012345678901234567890');
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: Uncaught Exception in %s:%d
16+
Stack trace:
17+
#0 %s(%d): main('12345678901234567890123...')
18+
#1 {main}
19+
thrown in %s on line %d

Zend/tests/exception_025.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
throwable_string_param_max_len ini setting
3+
--FILE--
4+
<?php
5+
6+
function main($arg) {
7+
echo (new Exception()), "\n";
8+
}
9+
var_dump(ini_set('throwable_string_param_max_len', '14'));
10+
var_dump(ini_set('throwable_string_param_max_len', '1000001'));
11+
var_dump(ini_set('throwable_string_param_max_len', '1000000'));
12+
var_dump(ini_set('throwable_string_param_max_len', '20'));
13+
main('short');
14+
main('123456789012345678901234567890');
15+
16+
?>
17+
--EXPECTF--
18+
bool(false)
19+
bool(false)
20+
string(2) "15"
21+
string(7) "1000000"
22+
Exception in %s:%d
23+
Stack trace:
24+
#0 %s: main('short')
25+
#1 {main}
26+
Exception in %s:%d
27+
Stack trace:
28+
#0 %s: main('12345678901234567890...')
29+
#1 {main}

Zend/zend_exceptions.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "zend_dtrace.h"
2929
#include "zend_smart_str.h"
3030
#include "zend_exceptions_arginfo.h"
31+
#include "php_globals.h"
3132

3233
ZEND_API zend_class_entry *zend_ce_throwable;
3334
ZEND_API zend_class_entry *zend_ce_exception;
@@ -480,8 +481,8 @@ static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
480481
break;
481482
case IS_STRING:
482483
smart_str_appendc(str, '\'');
483-
smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), 15));
484-
if (Z_STRLEN_P(arg) > 15) {
484+
smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), PG(throwable_string_param_max_len)));
485+
if (Z_STRLEN_P(arg) > PG(throwable_string_param_max_len)) {
485486
smart_str_appends(str, "...', ");
486487
} else {
487488
smart_str_appends(str, "', ");

main/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ static PHP_INI_MH(OnSetSerializePrecision)
264264
}
265265
/* }}} */
266266

267+
/* {{{ PHP_INI_MH
268+
*/
269+
static PHP_INI_MH(OnSetThrowableStringParamMaxLen)
270+
{
271+
zend_long i;
272+
273+
ZEND_ATOL(i, ZSTR_VAL(new_value));
274+
if (i >= 15 && i <= 1000000) {
275+
PG(throwable_string_param_max_len) = i;
276+
return SUCCESS;
277+
} else {
278+
return FAILURE;
279+
}
280+
}
281+
/* }}} */
267282

268283
/* {{{ PHP_INI_MH
269284
*/
@@ -725,6 +740,7 @@ PHP_INI_BEGIN()
725740
STD_PHP_INI_BOOLEAN("register_argc_argv", "1", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, register_argc_argv, php_core_globals, core_globals)
726741
STD_PHP_INI_BOOLEAN("auto_globals_jit", "1", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, auto_globals_jit, php_core_globals, core_globals)
727742
STD_PHP_INI_BOOLEAN("short_open_tag", DEFAULT_SHORT_OPEN_TAG, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, short_tags, zend_compiler_globals, compiler_globals)
743+
STD_PHP_INI_ENTRY("throwable_string_param_max_len", "15", PHP_INI_ALL, OnSetThrowableStringParamMaxLen, throwable_string_param_max_len, php_core_globals, core_globals)
728744

729745
STD_PHP_INI_ENTRY("unserialize_callback_func", NULL, PHP_INI_ALL, OnUpdateString, unserialize_callback_func, php_core_globals, core_globals)
730746
STD_PHP_INI_ENTRY("serialize_precision", "-1", PHP_INI_ALL, OnSetSerializePrecision, serialize_precision, php_core_globals, core_globals)

main/php_globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ struct _php_core_globals {
167167
char *syslog_ident;
168168
zend_bool have_called_openlog;
169169
zend_long syslog_filter;
170+
171+
zend_long throwable_string_param_max_len;
170172
};
171173

172174

run-tests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ function main()
340340
'zend.assertions=1',
341341
'zend.exception_ignore_args=0',
342342
'short_open_tag=0',
343+
'throwable_string_param_max_len=15',
343344
);
344345

345346
$no_file_cache = '-d opcache.file_cache= -d opcache.file_cache_only=0';

0 commit comments

Comments
 (0)