Skip to content

Commit 1203bbf

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Respect typed references in catch assignment
2 parents 2daa89b + 4a08ca1 commit 1203bbf

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

Zend/tests/bug53511.phpt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ function test() {
2020
test();
2121
echo "bug\n";
2222
--EXPECTF--
23-
Fatal error: Uncaught Exception: ops 2 in %sbug53511.php:11
24-
Stack trace:
25-
#0 %sbug53511.php(17): test()
26-
#1 {main}
27-
28-
Next Exception: ops 1 in %sbug53511.php:4
23+
Fatal error: Uncaught Exception: ops 1 in %sbug53511.php:4
2924
Stack trace:
3025
#0 %sbug53511.php(12): Foo->__destruct()
3126
#1 %sbug53511.php(17): test()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Variable assignment in catch must respect typed references
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public int $i = 42;
8+
public string $s = "str";
9+
}
10+
11+
$test = new Test;
12+
13+
$ref =& $test->i;
14+
try {
15+
try {
16+
throw new Exception("ex");
17+
} catch (Exception $ref) {
18+
echo "Unreachable\n";
19+
}
20+
} catch (TypeError $e) {
21+
var_dump($test->i);
22+
echo $e . "\n\n";
23+
}
24+
25+
$ref =& $test->s;
26+
try {
27+
try {
28+
throw new Exception("ex");
29+
} catch (Exception $ref) {
30+
echo "Unreachable\n";
31+
}
32+
} catch (TypeError $e) {
33+
var_dump($test->s);
34+
echo $e . "\n\n";
35+
}
36+
37+
?>
38+
--EXPECTF--
39+
int(42)
40+
TypeError: Cannot assign Exception to reference held by property Test::$i of type int in %s:%d
41+
Stack trace:
42+
#0 {main}
43+
44+
string(3) "str"
45+
TypeError: Cannot assign Exception to reference held by property Test::$s of type string in %s:%d
46+
Stack trace:
47+
#0 {main}

Zend/zend_vm_def.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,17 +4487,15 @@ ZEND_VM_HANDLER(107, ZEND_CATCH, CONST, JMP_ADDR, LAST_CATCH|CACHE_SLOT)
44874487

44884488
exception = EG(exception);
44894489
ex = EX_VAR(opline->result.var);
4490-
if (UNEXPECTED(Z_ISREF_P(ex))) {
4491-
ex = Z_REFVAL_P(ex);
4492-
}
4493-
zval_ptr_dtor(ex);
4494-
ZVAL_OBJ(ex, EG(exception));
4495-
if (UNEXPECTED(EG(exception) != exception)) {
4496-
ZVAL_UNDEF(ex);
4497-
HANDLE_EXCEPTION();
4498-
} else {
4490+
{
4491+
/* Always perform a strict assignment. There is a reasonable expectation that if you
4492+
* write "catch (Exception $e)" then $e will actually be instanceof Exception. As such,
4493+
* we should not permit coercion to string here. */
4494+
zval tmp;
4495+
ZVAL_OBJ(&tmp, exception);
44994496
EG(exception) = NULL;
4500-
ZEND_VM_NEXT_OPCODE();
4497+
zend_assign_to_variable(ex, &tmp, IS_TMP_VAR, /* strict */ 1);
4498+
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
45014499
}
45024500
}
45034501

Zend/zend_vm_execute.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,17 +3703,15 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CATCH_SPEC_CONST_HANDLER(ZEND_
37033703

37043704
exception = EG(exception);
37053705
ex = EX_VAR(opline->result.var);
3706-
if (UNEXPECTED(Z_ISREF_P(ex))) {
3707-
ex = Z_REFVAL_P(ex);
3708-
}
3709-
zval_ptr_dtor(ex);
3710-
ZVAL_OBJ(ex, EG(exception));
3711-
if (UNEXPECTED(EG(exception) != exception)) {
3712-
ZVAL_UNDEF(ex);
3713-
HANDLE_EXCEPTION();
3714-
} else {
3706+
{
3707+
/* Always perform a strict assignment. There is a reasonable expectation that if you
3708+
* write "catch (Exception $e)" then $e will actually be instanceof Exception. As such,
3709+
* we should not permit coercion to string here. */
3710+
zval tmp;
3711+
ZVAL_OBJ(&tmp, exception);
37153712
EG(exception) = NULL;
3716-
ZEND_VM_NEXT_OPCODE();
3713+
zend_assign_to_variable(ex, &tmp, IS_TMP_VAR, /* strict */ 1);
3714+
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
37173715
}
37183716
}
37193717

0 commit comments

Comments
 (0)