Skip to content

Improve match() error messages #7312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/match/006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $x = match (true) {};

?>
--EXPECTF--
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type bool in %s
Fatal error: Uncaught UnhandledMatchError: Unhandled match value: true in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/match/007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ echo get_value(3) . "\n";
1
2

Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in %s
Fatal error: Uncaught UnhandledMatchError: Unhandled match value: 3 in %s:%d
Stack trace:
#0 %s: get_value(3)
#1 {main}
Expand Down
6 changes: 3 additions & 3 deletions Zend/tests/match/037.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ var_dump(match(3) {

?>
--EXPECTF--
string(%d) "UnhandledMatchError: Unhandled match value of type bool in %s037.php:4
string(%d) "UnhandledMatchError: Unhandled match value: true in %s:%d
Stack trace:
#0 {main}"
string(%d) "UnhandledMatchError: Unhandled match value of type int in %s037.php:12
string(%d) "UnhandledMatchError: Unhandled match value: 6 in %s:%d
Stack trace:
#0 {main}"
string(%d) "UnhandledMatchError: Unhandled match value of type string in %s037.php:20
string(%d) "UnhandledMatchError: Unhandled match value: "3" in %s:%d
Stack trace:
#0 {main}"
string(3) "foo"
Expand Down
42 changes: 42 additions & 0 deletions Zend/tests/match/043.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Match expression error messages
--FILE--
<?php

class Beep {}

function test(mixed $var) {
try {
match($var) {};
} catch (UnhandledMatchError $e) {
print $e->getMessage() . PHP_EOL;
}
}

test(1);
test(5.5);
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 value: 1
Unhandled match value: 5.5
Unhandled match value: "foo"
Unhandled match value: true
Unhandled match value: false
Unhandled match value of type array
Unhandled match value of type Beep
Unhandled match value: "eeeeeeeeee..."
Unhandled match value: "e
e
e
e
e
..."
37 changes: 36 additions & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8935,7 +8935,42 @@ ZEND_VM_COLD_CONST_HANDLER(197, ZEND_MATCH_ERROR, CONST|TMPVARCV, UNUSED)

SAVE_OPLINE();
op = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value of type %s", zend_zval_type_name(op));

// For simpler types where there is a convenient stringified version, include that
// in the error message. Otherwise just show its type.
switch (Z_TYPE_P(op)) {
case IS_FALSE:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: false");
break;
case IS_TRUE:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: true");
break;
case IS_LONG:
case IS_DOUBLE: {
zend_string* stringified = zval_get_string(op);
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: %s", ZSTR_VAL(stringified));
zend_string_release_ex(stringified, false);
break;
}
case IS_STRING: {
zend_string* stringified = zval_get_string(op);
// This number was chosen mostly arbitrarily. But in context,
// a match() on a string should only be using short strings so something
// longer is unlikely to happen to begin with.
const int max_strlen = 10;
if (ZSTR_LEN(stringified) > max_strlen) {
stringified = zend_string_realloc(stringified, max_strlen + 3, 0);
memcpy(&ZSTR_VAL(stringified)[max_strlen], "...", sizeof("..."));
}
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: \"%s\"", ZSTR_VAL(stringified));
zend_string_release_ex(stringified, false);
break;
}
default:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value of type %s", zend_zval_type_name(op));
break;
}

HANDLE_EXCEPTION();
}

Expand Down
74 changes: 72 additions & 2 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -10507,7 +10507,42 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_MATCH_ERROR_SPEC_

SAVE_OPLINE();
op = RT_CONSTANT(opline, opline->op1);
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value of type %s", zend_zval_type_name(op));

// For simpler types where there is a convenient stringified version, include that
// in the error message. Otherwise just show its type.
switch (Z_TYPE_P(op)) {
case IS_FALSE:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: false");
break;
case IS_TRUE:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: true");
break;
case IS_LONG:
case IS_DOUBLE: {
zend_string* stringified = zval_get_string(op);
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: %s", ZSTR_VAL(stringified));
zend_string_release_ex(stringified, false);
break;
}
case IS_STRING: {
zend_string* stringified = zval_get_string(op);
// This number was chosen mostly arbitrarily. But in context,
// a match() on a string should only be using short strings so something
// longer is unlikely to happen to begin with.
const int max_strlen = 10;
if (ZSTR_LEN(stringified) > max_strlen) {
stringified = zend_string_realloc(stringified, max_strlen + 3, 0);
memcpy(&ZSTR_VAL(stringified)[max_strlen], "...", sizeof("..."));
}
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: \"%s\"", ZSTR_VAL(stringified));
zend_string_release_ex(stringified, false);
break;
}
default:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value of type %s", zend_zval_type_name(op));
break;
}

HANDLE_EXCEPTION();
}

Expand Down Expand Up @@ -14033,7 +14068,42 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_MATCH_ERROR_SPEC_TMPVARCV_UNUS

SAVE_OPLINE();
op = EX_VAR(opline->op1.var);
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value of type %s", zend_zval_type_name(op));

// For simpler types where there is a convenient stringified version, include that
// in the error message. Otherwise just show its type.
switch (Z_TYPE_P(op)) {
case IS_FALSE:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: false");
break;
case IS_TRUE:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: true");
break;
case IS_LONG:
case IS_DOUBLE: {
zend_string* stringified = zval_get_string(op);
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: %s", ZSTR_VAL(stringified));
zend_string_release_ex(stringified, false);
break;
}
case IS_STRING: {
zend_string* stringified = zval_get_string(op);
// This number was chosen mostly arbitrarily. But in context,
// a match() on a string should only be using short strings so something
// longer is unlikely to happen to begin with.
const int max_strlen = 10;
if (ZSTR_LEN(stringified) > max_strlen) {
stringified = zend_string_realloc(stringified, max_strlen + 3, 0);
memcpy(&ZSTR_VAL(stringified)[max_strlen], "...", sizeof("..."));
}
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value: \"%s\"", ZSTR_VAL(stringified));
zend_string_release_ex(stringified, false);
break;
}
default:
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Unhandled match value of type %s", zend_zval_type_name(op));
break;
}

HANDLE_EXCEPTION();
}

Expand Down