|
| 1 | +--TEST-- |
| 2 | +Bug #81992 (SplFixedArray::setSize() causes use-after-free) - setSize variation |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | +class InvalidDestructor { |
| 6 | + public function __construct( |
| 7 | + private int $desiredSize, |
| 8 | + private SplFixedArray $obj, |
| 9 | + ) {} |
| 10 | + |
| 11 | + public function __destruct() { |
| 12 | + echo "In destructor\n"; |
| 13 | + $this->obj->setSize($this->desiredSize); |
| 14 | + echo "Destroyed, size is now still ", $this->obj->getSize(), "\n"; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +class DestructorLogger { |
| 19 | + public function __construct(private int $id) {} |
| 20 | + |
| 21 | + public function __destruct() { |
| 22 | + echo "Destroyed the logger with id ", $this->id, "\n"; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function test(int $desiredSize) { |
| 27 | + $obj = new SplFixedArray(5); |
| 28 | + $obj[0] = str_repeat("A", 10); |
| 29 | + $obj[1] = new DestructorLogger(1); |
| 30 | + $obj[2] = str_repeat('B', 10); |
| 31 | + $obj[3] = new InvalidDestructor($desiredSize, $obj); |
| 32 | + $obj[4] = new DestructorLogger(4); |
| 33 | + $obj->setSize(2); |
| 34 | + echo "Size is now ", $obj->getSize(), "\n"; |
| 35 | + echo "Done\n"; |
| 36 | +} |
| 37 | + |
| 38 | +echo "--- Smaller size test ---\n"; |
| 39 | +test(1); |
| 40 | +echo "--- Equal size test ---\n"; |
| 41 | +test(2); |
| 42 | +echo "--- Larger size test ---\n"; |
| 43 | +test(10); |
| 44 | +?> |
| 45 | +--EXPECT-- |
| 46 | +--- Smaller size test --- |
| 47 | +In destructor |
| 48 | +Destroyed, size is now still 2 |
| 49 | +Destroyed the logger with id 4 |
| 50 | +Destroyed the logger with id 1 |
| 51 | +Size is now 1 |
| 52 | +Done |
| 53 | +--- Equal size test --- |
| 54 | +In destructor |
| 55 | +Destroyed, size is now still 2 |
| 56 | +Destroyed the logger with id 4 |
| 57 | +Size is now 2 |
| 58 | +Done |
| 59 | +Destroyed the logger with id 1 |
| 60 | +--- Larger size test --- |
| 61 | +In destructor |
| 62 | +Destroyed, size is now still 2 |
| 63 | +Destroyed the logger with id 4 |
| 64 | +Size is now 10 |
| 65 | +Done |
| 66 | +Destroyed the logger with id 1 |
0 commit comments