Skip to content

Commit 0301ab7

Browse files
committed
Fix const/cv freeing on failed reference assignment
1 parent 9458f7e commit 0301ab7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
CONST/CV should not be freed on failed reference assignment
3+
--FILE--
4+
<?php
5+
class Test {
6+
public ?Type $prop;
7+
}
8+
$obj = new Test;
9+
$ref =& $obj->prop;
10+
try {
11+
$ref = [1];
12+
} catch (TypeError $e) {
13+
echo $e->getMessage(), "\n";
14+
}
15+
try {
16+
$ary = [1];
17+
$ref = $ary;
18+
} catch (TypeError $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
var_dump($ref);
22+
?>
23+
--EXPECT--
24+
Cannot assign array to reference held by property Test::$prop of type ?Type
25+
Cannot assign array to reference held by property Test::$prop of type ?Type
26+
NULL

Zend/zend_execute.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3187,7 +3187,9 @@ ZEND_API zval* zend_assign_to_typed_ref(zval *variable_ptr, zval *value, zend_uc
31873187
Z_TRY_DELREF_P(value);
31883188
}
31893189
if (!ret) {
3190-
zval_ptr_dtor(value);
3190+
if (value_type & (IS_VAR|IS_TMP_VAR)) {
3191+
zval_ptr_dtor(value);
3192+
}
31913193
return Z_REFVAL_P(variable_ptr);
31923194
}
31933195

0 commit comments

Comments
 (0)