Repository navigation
Commit f7b26a5
committed
ext/standard: Fix array_merge() with an empty array argument
When one of the two arguments of array_merge() is empty, the other one is
returned as is instead of being copied. The returned array is the argument
itself, shared through copy-on-write, so it also kept the next free index of the
argument.
array_merge() renumbers integer keys from zero, so the next append to the result
must use the element count. An argument whose next free index was stale, for
example after unset() of its last element, made $result[] = $value use a wrong
key.
The shortcut now applies only when the argument already behaves like the result
of the general code path: a packed array without holes, or an array with string
keys only, whose next free index is the one the general path would produce.
Everything else goes through the general path, which builds a fresh array. Clean
arrays are still returned as is, so the copy is still avoided.
array_merge_recursive() shares the same code path and had the same bug.1 parent 6e8aad5 commit f7b26a5
2 files changed
Lines changed: 43 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4275 | 4275 | | |
4276 | 4276 | | |
4277 | 4277 | | |
4278 | | - | |
4279 | | - | |
| 4278 | + | |
| 4279 | + | |
| 4280 | + | |
| 4281 | + | |
| 4282 | + | |
| 4283 | + | |
| 4284 | + | |
4280 | 4285 | | |
4281 | 4286 | | |
4282 | | - | |
| 4287 | + | |
4283 | 4288 | | |
4284 | 4289 | | |
4285 | 4290 | | |
4286 | | - | |
| 4291 | + | |
4287 | 4292 | | |
4288 | 4293 | | |
4289 | 4294 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
0 commit comments