Skip to content

Commit 5052b32

Browse files
authored
zend_execute: Fix misleading UnhandledMatchError messages when exception_string_param_max_len=0 (#17601)
1 parent 8c68fe1 commit 5052b32

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ PHP NEWS
1818
non-printable characters in string literals). (nielsdos, WangYihang)
1919
. Add support for backtraces for fatal errors. (enorris)
2020
. Fixed bug GH-17442 (Engine UAF with reference assign and dtor). (nielsdos)
21+
. Improved error message of UnhandledMatchError for
22+
zend.exception_string_param_max_len=0. (timwolla)
2123

2224
- Curl:
2325
. Added curl_multi_get_handles(). (timwolla)

Zend/tests/match/048.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Match expression error messages (exception_string_param_max_len=0)
3+
--INI--
4+
zend.exception_string_param_max_len=0
5+
--FILE--
6+
<?php
7+
8+
class Beep {}
9+
10+
function test(mixed $var) {
11+
try {
12+
match($var) {};
13+
} catch (UnhandledMatchError $e) {
14+
print $e->getMessage() . PHP_EOL;
15+
}
16+
}
17+
18+
test(null);
19+
test(1);
20+
test(5.5);
21+
test(5.0);
22+
test("foo");
23+
test(true);
24+
test(false);
25+
test([1, 2, 3]);
26+
test(new Beep());
27+
// Testing long strings.
28+
test(str_repeat('e', 100));
29+
test(str_repeat("e\n", 100));
30+
?>
31+
--EXPECT--
32+
Unhandled match case NULL
33+
Unhandled match case 1
34+
Unhandled match case 5.5
35+
Unhandled match case 5.0
36+
Unhandled match case of type string
37+
Unhandled match case true
38+
Unhandled match case false
39+
Unhandled match case of type array
40+
Unhandled match case of type Beep
41+
Unhandled match case of type string
42+
Unhandled match case of type string

Zend/zend_execute.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,12 @@ ZEND_COLD zend_never_inline void zend_magic_get_property_type_inconsistency_erro
874874

875875
ZEND_COLD void zend_match_unhandled_error(const zval *value)
876876
{
877+
zend_long max_len = EG(exception_string_param_max_len);
877878
smart_str msg = {0};
878879
if (
879880
EG(exception_ignore_args)
880-
|| smart_str_append_zval(&msg, value, EG(exception_string_param_max_len)) != SUCCESS
881+
|| (Z_TYPE_P(value) == IS_STRING && max_len == 0)
882+
|| smart_str_append_zval(&msg, value, max_len) != SUCCESS
881883
) {
882884
smart_str_appendl(&msg, "of type ", sizeof("of type ")-1);
883885
smart_str_appends(&msg, zend_zval_type_name(value));

0 commit comments

Comments
 (0)