From 616b6da2a899da27bef785366e7d6c71e275506d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 27 Jan 2025 17:28:24 +0100 Subject: [PATCH 1/2] zend_execute: Fix misleading `UnhandledMatchError` messages when `exception_string_param_max_len=0` This also fixes a leak of non-string values if `zend.exception_ignore_args=1`. --- Zend/tests/match/048.phpt | 42 +++++++++++++++++++++++++++++++++++++++ Zend/tests/match/049.phpt | 42 +++++++++++++++++++++++++++++++++++++++ Zend/zend_execute.c | 7 ++++++- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/match/048.phpt create mode 100644 Zend/tests/match/049.phpt diff --git a/Zend/tests/match/048.phpt b/Zend/tests/match/048.phpt new file mode 100644 index 0000000000000..e5c6bb2e0bcbe --- /dev/null +++ b/Zend/tests/match/048.phpt @@ -0,0 +1,42 @@ +--TEST-- +Match expression error messages (exception_string_param_max_len=0) +--INI-- +zend.exception_string_param_max_len=0 +--FILE-- +getMessage() . PHP_EOL; + } +} + +test(null); +test(1); +test(5.5); +test(5.0); +test("foo"); +test(true); +test(false); +test([1, 2, 3]); +test(new Beep()); +// Testing long strings. +test(str_repeat('e', 100)); +test(str_repeat("e\n", 100)); +?> +--EXPECT-- +Unhandled match case NULL +Unhandled match case 1 +Unhandled match case 5.5 +Unhandled match case 5.0 +Unhandled match case of type string +Unhandled match case true +Unhandled match case false +Unhandled match case of type array +Unhandled match case of type Beep +Unhandled match case of type string +Unhandled match case of type string diff --git a/Zend/tests/match/049.phpt b/Zend/tests/match/049.phpt new file mode 100644 index 0000000000000..2a2385f704ff4 --- /dev/null +++ b/Zend/tests/match/049.phpt @@ -0,0 +1,42 @@ +--TEST-- +Match expression error messages (zend.exception_ignore_args=1) +--INI-- +zend.exception_ignore_args=1 +--FILE-- +getMessage() . PHP_EOL; + } +} + +test(null); +test(1); +test(5.5); +test(5.0); +test("foo"); +test(true); +test(false); +test([1, 2, 3]); +test(new Beep()); +// Testing long strings. +test(str_repeat('e', 100)); +test(str_repeat("e\n", 100)); +?> +--EXPECT-- +Unhandled match case of type null +Unhandled match case of type int +Unhandled match case of type float +Unhandled match case of type float +Unhandled match case of type string +Unhandled match case of type bool +Unhandled match case of type bool +Unhandled match case of type array +Unhandled match case of type Beep +Unhandled match case of type string +Unhandled match case of type string diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index e425e6625e4c5..a686dae5a05e7 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -874,8 +874,13 @@ ZEND_COLD zend_never_inline void zend_magic_get_property_type_inconsistency_erro ZEND_COLD void zend_match_unhandled_error(const zval *value) { + zend_long max_len = EG(exception_string_param_max_len); smart_str msg = {0}; - if (smart_str_append_zval(&msg, value, EG(exception_string_param_max_len)) != SUCCESS) { + if ( + EG(exception_ignore_args) + || (Z_TYPE_P(value) == IS_STRING && max_len == 0) + || smart_str_append_zval(&msg, value, max_len) != SUCCESS + ) { smart_str_appendl(&msg, "of type ", sizeof("of type ")-1); smart_str_appends(&msg, zend_zval_type_name(value)); } From c6e6163a6ee6cde79b09846f890f86b2ed82e74d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 31 Jan 2025 10:25:16 +0100 Subject: [PATCH 2/2] NEWS --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 24f4963802b90..0c5694a7090bb 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,8 @@ PHP NEWS non-printable characters in string literals). (nielsdos, WangYihang) . Add support for backtraces for fatal errors. (enorris) . Fixed bug GH-17442 (Engine UAF with reference assign and dtor). (nielsdos) + . Improved error message of UnhandledMatchError for + zend.exception_string_param_max_len=0. (timwolla) - Curl: . Added curl_multi_get_handles(). (timwolla)