|
| 1 | +--TEST-- |
| 2 | +GH-21927: Use-after-free of self-freeing MultipleIterator children |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class DetachOnRewind implements Iterator { |
| 7 | + public function __construct(private MultipleIterator $parent) {} |
| 8 | + public function rewind(): void { |
| 9 | + $this->parent->detachIterator($this); |
| 10 | + echo "rewind: still alive\n"; |
| 11 | + } |
| 12 | + public function next(): void {} |
| 13 | + public function current(): mixed { return 0; } |
| 14 | + public function key(): mixed { return 0; } |
| 15 | + public function valid(): bool { return false; } |
| 16 | +} |
| 17 | + |
| 18 | +class DetachOnNext implements Iterator { |
| 19 | + public function __construct(private MultipleIterator $parent) {} |
| 20 | + public function rewind(): void {} |
| 21 | + public function next(): void { |
| 22 | + $this->parent->detachIterator($this); |
| 23 | + echo "next: still alive\n"; |
| 24 | + } |
| 25 | + public function current(): mixed { return 0; } |
| 26 | + public function key(): mixed { return 0; } |
| 27 | + public function valid(): bool { return true; } |
| 28 | +} |
| 29 | + |
| 30 | +class DetachOnValid implements Iterator { |
| 31 | + public function __construct(private MultipleIterator $parent) {} |
| 32 | + public function rewind(): void {} |
| 33 | + public function next(): void {} |
| 34 | + public function current(): mixed { return 0; } |
| 35 | + public function key(): mixed { return 0; } |
| 36 | + public function valid(): bool { |
| 37 | + $this->parent->detachIterator($this); |
| 38 | + echo "valid: still alive\n"; |
| 39 | + return true; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class DetachOnCurrent implements Iterator { |
| 44 | + public function __construct(private MultipleIterator $parent) {} |
| 45 | + public function rewind(): void {} |
| 46 | + public function next(): void {} |
| 47 | + public function current(): mixed { |
| 48 | + $this->parent->detachIterator($this); |
| 49 | + echo "current: still alive\n"; |
| 50 | + return 'C'; |
| 51 | + } |
| 52 | + public function key(): mixed { return 'k'; } |
| 53 | + public function valid(): bool { return true; } |
| 54 | +} |
| 55 | + |
| 56 | +class DetachOnKey implements Iterator { |
| 57 | + public function __construct(private MultipleIterator $parent) {} |
| 58 | + public function rewind(): void {} |
| 59 | + public function next(): void {} |
| 60 | + public function current(): mixed { return 'C'; } |
| 61 | + public function key(): mixed { |
| 62 | + $this->parent->detachIterator($this); |
| 63 | + echo "key: still alive\n"; |
| 64 | + return 'K'; |
| 65 | + } |
| 66 | + public function valid(): bool { return true; } |
| 67 | +} |
| 68 | + |
| 69 | +echo "-- detach inside rewind --\n"; |
| 70 | +$mi = new MultipleIterator(); |
| 71 | +$mi->attachIterator(new DetachOnRewind($mi)); |
| 72 | +$mi->rewind(); |
| 73 | +var_dump($mi->countIterators()); |
| 74 | + |
| 75 | +echo "-- detach inside next --\n"; |
| 76 | +$mi = new MultipleIterator(); |
| 77 | +$mi->attachIterator(new DetachOnNext($mi)); |
| 78 | +$mi->rewind(); |
| 79 | +$mi->next(); |
| 80 | +var_dump($mi->countIterators()); |
| 81 | + |
| 82 | +echo "-- detach inside valid --\n"; |
| 83 | +$mi = new MultipleIterator(); |
| 84 | +$mi->attachIterator(new DetachOnValid($mi)); |
| 85 | +var_dump($mi->valid()); |
| 86 | +var_dump($mi->countIterators()); |
| 87 | + |
| 88 | +echo "-- detach inside current (numeric keys) --\n"; |
| 89 | +$mi = new MultipleIterator(); |
| 90 | +$mi->attachIterator(new DetachOnCurrent($mi)); |
| 91 | +var_dump($mi->current()); |
| 92 | +var_dump($mi->countIterators()); |
| 93 | + |
| 94 | +echo "-- detach inside key (numeric keys) --\n"; |
| 95 | +$mi = new MultipleIterator(); |
| 96 | +$mi->attachIterator(new DetachOnKey($mi)); |
| 97 | +var_dump($mi->key()); |
| 98 | +var_dump($mi->countIterators()); |
| 99 | + |
| 100 | +echo "-- detach inside current (assoc keys, refcounted inf) --\n"; |
| 101 | +$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC); |
| 102 | +$mi->attachIterator(new DetachOnCurrent($mi), 'cur_info_string'); |
| 103 | +var_dump($mi->current()); |
| 104 | +var_dump($mi->countIterators()); |
| 105 | + |
| 106 | +echo "-- detach inside key (assoc keys, refcounted inf) --\n"; |
| 107 | +$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC); |
| 108 | +$mi->attachIterator(new DetachOnKey($mi), 'key_info_string'); |
| 109 | +var_dump($mi->key()); |
| 110 | +var_dump($mi->countIterators()); |
| 111 | + |
| 112 | +?> |
| 113 | +--EXPECT-- |
| 114 | +-- detach inside rewind -- |
| 115 | +rewind: still alive |
| 116 | +int(0) |
| 117 | +-- detach inside next -- |
| 118 | +next: still alive |
| 119 | +int(0) |
| 120 | +-- detach inside valid -- |
| 121 | +valid: still alive |
| 122 | +bool(true) |
| 123 | +int(0) |
| 124 | +-- detach inside current (numeric keys) -- |
| 125 | +current: still alive |
| 126 | +array(1) { |
| 127 | + [0]=> |
| 128 | + string(1) "C" |
| 129 | +} |
| 130 | +int(0) |
| 131 | +-- detach inside key (numeric keys) -- |
| 132 | +key: still alive |
| 133 | +array(1) { |
| 134 | + [0]=> |
| 135 | + string(1) "K" |
| 136 | +} |
| 137 | +int(0) |
| 138 | +-- detach inside current (assoc keys, refcounted inf) -- |
| 139 | +current: still alive |
| 140 | +array(1) { |
| 141 | + ["cur_info_string"]=> |
| 142 | + string(1) "C" |
| 143 | +} |
| 144 | +int(0) |
| 145 | +-- detach inside key (assoc keys, refcounted inf) -- |
| 146 | +key: still alive |
| 147 | +array(1) { |
| 148 | + ["key_info_string"]=> |
| 149 | + string(1) "K" |
| 150 | +} |
| 151 | +int(0) |
0 commit comments