Skip to content

Commit 05ef633

Browse files
committed
Fix bug #81303 improve match errors
1 parent 6048481 commit 05ef633

18 files changed

+143
-69
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.0beta2
44

5+
- Core:
6+
. Fixed bug #81303 (match error message improvements). (krakjoe)
7+
58
- Mbstring:
69
. Fixed bug #81298 (mb_detect_encoding() segfaults when 7bit encoding is
710
specified). (Nikita)

Zend/tests/match/006.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ $x = match (true) {};
77

88
?>
99
--EXPECTF--
10-
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type bool in %s
10+
Fatal error: Uncaught UnhandledMatchError: Unhandled match case true in %s:%d
1111
Stack trace:
1212
#0 {main}
1313
thrown in %s on line %d

Zend/tests/match/007.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ echo get_value(3) . "\n";
1919
1
2020
2
2121

22-
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in %s
22+
Fatal error: Uncaught UnhandledMatchError: Unhandled match case 3 in %s:%d
2323
Stack trace:
2424
#0 %s: get_value(3)
2525
#1 {main}

Zend/tests/match/037.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ var_dump(match(3) {
5353

5454
?>
5555
--EXPECTF--
56-
string(%d) "UnhandledMatchError: Unhandled match value of type bool in %s037.php:4
56+
string(%d) "UnhandledMatchError: Unhandled match case true in %s:%d
5757
Stack trace:
5858
#0 {main}"
59-
string(%d) "UnhandledMatchError: Unhandled match value of type int in %s037.php:12
59+
string(%d) "UnhandledMatchError: Unhandled match case 6 in %s:%d
6060
Stack trace:
6161
#0 {main}"
62-
string(%d) "UnhandledMatchError: Unhandled match value of type string in %s037.php:20
62+
string(%d) "UnhandledMatchError: Unhandled match case '3' in %s:%d
6363
Stack trace:
6464
#0 {main}"
6565
string(3) "foo"

Zend/tests/match/043.phpt

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

Zend/tests/type_declarations/explicit_weak_include_strict.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require 'weak_include_strict_2.inc';
1313
--EXPECTF--
1414
Fatal error: Uncaught TypeError: takes_int(): Argument #1 ($x) must be of type int, float given, called in %s:%d
1515
Stack trace:
16-
#0 %s(%d): takes_int(1)
16+
#0 %s(%d): takes_int(1.0)
1717
#1 %s(%d): require('%s')
1818
#2 {main}
1919
thrown in %sweak_include_strict_2.inc on line 5

Zend/tests/type_declarations/strict_call_weak.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ function_declared_in_weak_mode(1.0);
1515
--EXPECTF--
1616
Fatal error: Uncaught TypeError: function_declared_in_weak_mode(): Argument #1 ($x) must be of type int, float given, called in %s:%d
1717
Stack trace:
18-
#0 %s(%d): function_declared_in_weak_mode(1)
18+
#0 %s(%d): function_declared_in_weak_mode(1.0)
1919
#1 {main}
2020
thrown in %sstrict_call_weak_2.inc on line 5

Zend/tests/type_declarations/strict_call_weak_explicit.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ function_declared_in_weak_mode(1.0);
1515
--EXPECTF--
1616
Fatal error: Uncaught TypeError: function_declared_in_weak_mode(): Argument #1 ($x) must be of type int, float given, called in %s:%d
1717
Stack trace:
18-
#0 %s(%d): function_declared_in_weak_mode(1)
18+
#0 %s(%d): function_declared_in_weak_mode(1.0)
1919
#1 {main}
2020
thrown in %sstrict_call_weak_explicit_2.inc on line 5

Zend/tests/type_declarations/weak_include_strict.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require 'weak_include_strict_2.inc';
1313
--EXPECTF--
1414
Fatal error: Uncaught TypeError: takes_int(): Argument #1 ($x) must be of type int, float given, called in %s:%d
1515
Stack trace:
16-
#0 %s(%d): takes_int(1)
16+
#0 %s(%d): takes_int(1.0)
1717
#1 %s(%d): require('%s')
1818
#2 {main}
1919
thrown in %sweak_include_strict_2.inc on line 5

Zend/zend_exceptions.c

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -491,49 +491,28 @@ static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
491491
*/
492492

493493
ZVAL_DEREF(arg);
494-
switch (Z_TYPE_P(arg)) {
495-
case IS_NULL:
496-
smart_str_appends(str, "NULL, ");
497-
break;
498-
case IS_STRING:
499-
smart_str_appendc(str, '\'');
500-
smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), EG(exception_string_param_max_len)));
501-
if (Z_STRLEN_P(arg) > EG(exception_string_param_max_len)) {
502-
smart_str_appends(str, "...', ");
503-
} else {
504-
smart_str_appends(str, "', ");
494+
495+
if (Z_TYPE_P(arg) <= IS_STRING) {
496+
smart_str_append_scalar(str, arg, EG(exception_string_param_max_len));
497+
smart_str_appends(str, ", ");
498+
} else {
499+
switch (Z_TYPE_P(arg)) {
500+
case IS_RESOURCE:
501+
smart_str_appends(str, "Resource id #");
502+
smart_str_append_long(str, Z_RES_HANDLE_P(arg));
503+
smart_str_appends(str, ", ");
504+
break;
505+
case IS_ARRAY:
506+
smart_str_appends(str, "Array, ");
507+
break;
508+
case IS_OBJECT: {
509+
zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
510+
smart_str_appends(str, "Object(");
511+
smart_str_appends(str, ZSTR_VAL(class_name));
512+
smart_str_appends(str, "), ");
513+
zend_string_release_ex(class_name, 0);
514+
break;
505515
}
506-
break;
507-
case IS_FALSE:
508-
smart_str_appends(str, "false, ");
509-
break;
510-
case IS_TRUE:
511-
smart_str_appends(str, "true, ");
512-
break;
513-
case IS_RESOURCE:
514-
smart_str_appends(str, "Resource id #");
515-
smart_str_append_long(str, Z_RES_HANDLE_P(arg));
516-
smart_str_appends(str, ", ");
517-
break;
518-
case IS_LONG:
519-
smart_str_append_long(str, Z_LVAL_P(arg));
520-
smart_str_appends(str, ", ");
521-
break;
522-
case IS_DOUBLE:
523-
smart_str_append_double(
524-
str, Z_DVAL_P(arg), (int) EG(precision), /* zero_fraction */ false);
525-
smart_str_appends(str, ", ");
526-
break;
527-
case IS_ARRAY:
528-
smart_str_appends(str, "Array, ");
529-
break;
530-
case IS_OBJECT: {
531-
zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
532-
smart_str_appends(str, "Object(");
533-
smart_str_appends(str, ZSTR_VAL(class_name));
534-
smart_str_appends(str, "), ");
535-
zend_string_release_ex(class_name, 0);
536-
break;
537516
}
538517
}
539518
}

0 commit comments

Comments
 (0)