|
| 1 | +--TEST-- |
| 2 | +Assigning to a typed property must not use the object after coercion user code released it |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class Test { |
| 7 | + public string $s = "a"; |
| 8 | + public int $i = 0; |
| 9 | +} |
| 10 | + |
| 11 | +class Releaser { |
| 12 | + public function __toString(): string { |
| 13 | + unset($GLOBALS['a'][0]); |
| 14 | + return "5"; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function attempt(Closure $fn) { |
| 19 | + try { |
| 20 | + $fn(); |
| 21 | + echo "assigned\n"; |
| 22 | + } catch (Error $e) { |
| 23 | + echo $e->getMessage(), "\n"; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function assign_tmp(array &$a) { $a[0]->s = new Releaser; } |
| 28 | +function assign_cv(array &$a, $v) { $a[0]->s = $v; } |
| 29 | + |
| 30 | +echo "--- __toString()\n"; |
| 31 | +for ($k = 0; $k < 3; $k++) { |
| 32 | + $a = [new Test]; |
| 33 | + attempt(function () use (&$a) { assign_tmp($a); }); |
| 34 | + $a = [new Test]; |
| 35 | + $v = new Releaser; |
| 36 | + attempt(function () use (&$a, $v) { assign_cv($a, $v); }); |
| 37 | +} |
| 38 | + |
| 39 | +echo "--- error handler\n"; |
| 40 | +set_error_handler(function () { |
| 41 | + unset($GLOBALS['a'][0]); |
| 42 | + return true; |
| 43 | +}); |
| 44 | +function assign_float_string(array &$a) { $a[0]->i = "1.5"; } |
| 45 | +for ($k = 0; $k < 3; $k++) { |
| 46 | + $a = [new Test]; |
| 47 | + attempt(function () use (&$a) { assign_float_string($a); }); |
| 48 | +} |
| 49 | + |
| 50 | +echo "--- class type: the object is not coerced\n"; |
| 51 | +class Foo {} |
| 52 | +class Holder { |
| 53 | + public Foo $foo; |
| 54 | + public string|Foo $u = ""; |
| 55 | +} |
| 56 | +$a = [new Holder]; |
| 57 | +for ($k = 0; $k < 3; $k++) { |
| 58 | + $a[0]->foo = new Foo; |
| 59 | + var_dump($a[0]->foo instanceof Foo); |
| 60 | +} |
| 61 | + |
| 62 | +echo "--- union with string: the object is coerced\n"; |
| 63 | +function assign_union(array &$a, $v) { $a[0]->u = $v; } |
| 64 | +for ($k = 0; $k < 3; $k++) { |
| 65 | + $a = [new Holder]; |
| 66 | + attempt(function () use (&$a) { assign_union($a, new Releaser); }); |
| 67 | +} |
| 68 | + |
| 69 | +echo "--- object stays alive\n"; |
| 70 | +$a = [new Test]; |
| 71 | +$a[0]->s = 12; |
| 72 | +var_dump($a[0]->s); |
| 73 | + |
| 74 | +?> |
| 75 | +--EXPECT-- |
| 76 | +--- __toString() |
| 77 | +Object was released while assigning to property Test::$s |
| 78 | +Object was released while assigning to property Test::$s |
| 79 | +Object was released while assigning to property Test::$s |
| 80 | +Object was released while assigning to property Test::$s |
| 81 | +Object was released while assigning to property Test::$s |
| 82 | +Object was released while assigning to property Test::$s |
| 83 | +--- error handler |
| 84 | +Object was released while assigning to property Test::$i |
| 85 | +Object was released while assigning to property Test::$i |
| 86 | +Object was released while assigning to property Test::$i |
| 87 | +--- class type: the object is not coerced |
| 88 | +bool(true) |
| 89 | +bool(true) |
| 90 | +bool(true) |
| 91 | +--- union with string: the object is coerced |
| 92 | +Object was released while assigning to property Holder::$u |
| 93 | +Object was released while assigning to property Holder::$u |
| 94 | +Object was released while assigning to property Holder::$u |
| 95 | +--- object stays alive |
| 96 | +string(2) "12" |
0 commit comments