Repository navigation
Fix GH-17935: Separate ArrayObject and ArrayIterator storage on write #25432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nicolas-grekas
wants to merge
1
commit into
php:master
Choose a base branch
from
nicolas-grekas:spl-array-separate-on-write
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
ext/spl/tests/ArrayObject/ArrayObject_separate_on_write.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| --TEST-- | ||
| ArrayObject and ArrayIterator separate their storage from the arrays they hand out before writing to it | ||
| --FILE-- | ||
| <?php | ||
| // __serialize() and __debugInfo() hand out the storage without copying it: | ||
| // every write path separates the storage first | ||
|
|
||
| function check(string $label, callable $write, array $classes = ['ArrayIterator', 'ArrayObject']) { | ||
| foreach ($classes as $class) { | ||
| $expected = ['a' => 1, 'b' => ['x' => 1], 'c' => 3]; | ||
| $object = new $class($expected, ArrayObject::ARRAY_AS_PROPS); | ||
| $serialized = $object->__serialize()[1]; | ||
| $debug = array_values($object->__debugInfo())[0]; | ||
| $write($object); | ||
| if ($serialized !== $expected || $debug !== $expected) { | ||
| echo "$class $label: FAIL\n"; | ||
| var_dump($serialized, $debug); | ||
| } | ||
| echo "$class $label: ", json_encode($object->getArrayCopy()), "\n"; | ||
| } | ||
| } | ||
|
|
||
| check('offsetSet', function ($o) { $o['a'] = 2; }); | ||
| check('offsetSet()', function ($o) { $o->offsetSet('a', 2); }); | ||
| check('append', function ($o) { $o[] = 4; }); | ||
| check('append()', function ($o) { $o->append(4); }); | ||
| check('offsetUnset', function ($o) { unset($o['a']); }); | ||
| check('offsetUnset() missing', function ($o) { $o->offsetUnset('z'); }); | ||
| check('compound assign', function ($o) { $o['a'] += 10; }); | ||
| check('increment', function ($o) { $o['a']++; }); | ||
| check('nested write', function ($o) { $o['b']['y'] = 2; }); | ||
| check('nested append', function ($o) { $o['b'][] = 2; }); | ||
| check('nested unset', function ($o) { unset($o['b']['x']); }); | ||
| check('reference', function ($o) { $r = &$o['a']; $r = 5; }); | ||
| check('property', function ($o) { $o->a = 6; }); | ||
| check('property compound assign', function ($o) { $o->a .= 'x'; }); | ||
| check('property nested write', function ($o) { $o->b['z'] = 3; }); | ||
| check('property reference', function ($o) { $r = &$o->c; $r = 7; }); | ||
| check('property unset', function ($o) { unset($o->c); }); | ||
| check('foreach by reference', function ($o) { foreach ($o as &$v) { $v = 8; } }); | ||
| check('uasort', function ($o) { $o->uasort(fn ($a, $b) => is_array($a) <=> is_array($b)); }, ['ArrayObject']); | ||
| check('ksort', function ($o) { $o->ksort(); $o['d'] = 0; }, ['ArrayObject']); | ||
| ?> | ||
| --EXPECT-- | ||
| ArrayIterator offsetSet: {"a":2,"b":{"x":1},"c":3} | ||
| ArrayObject offsetSet: {"a":2,"b":{"x":1},"c":3} | ||
| ArrayIterator offsetSet(): {"a":2,"b":{"x":1},"c":3} | ||
| ArrayObject offsetSet(): {"a":2,"b":{"x":1},"c":3} | ||
| ArrayIterator append: {"a":1,"b":{"x":1},"c":3,"0":4} | ||
| ArrayObject append: {"a":1,"b":{"x":1},"c":3,"0":4} | ||
| ArrayIterator append(): {"a":1,"b":{"x":1},"c":3,"0":4} | ||
| ArrayObject append(): {"a":1,"b":{"x":1},"c":3,"0":4} | ||
| ArrayIterator offsetUnset: {"b":{"x":1},"c":3} | ||
| ArrayObject offsetUnset: {"b":{"x":1},"c":3} | ||
| ArrayIterator offsetUnset() missing: {"a":1,"b":{"x":1},"c":3} | ||
| ArrayObject offsetUnset() missing: {"a":1,"b":{"x":1},"c":3} | ||
| ArrayIterator compound assign: {"a":11,"b":{"x":1},"c":3} | ||
| ArrayObject compound assign: {"a":11,"b":{"x":1},"c":3} | ||
| ArrayIterator increment: {"a":2,"b":{"x":1},"c":3} | ||
| ArrayObject increment: {"a":2,"b":{"x":1},"c":3} | ||
| ArrayIterator nested write: {"a":1,"b":{"x":1,"y":2},"c":3} | ||
| ArrayObject nested write: {"a":1,"b":{"x":1,"y":2},"c":3} | ||
| ArrayIterator nested append: {"a":1,"b":{"x":1,"0":2},"c":3} | ||
| ArrayObject nested append: {"a":1,"b":{"x":1,"0":2},"c":3} | ||
| ArrayIterator nested unset: {"a":1,"b":[],"c":3} | ||
| ArrayObject nested unset: {"a":1,"b":[],"c":3} | ||
| ArrayIterator reference: {"a":5,"b":{"x":1},"c":3} | ||
| ArrayObject reference: {"a":5,"b":{"x":1},"c":3} | ||
| ArrayIterator property: {"a":6,"b":{"x":1},"c":3} | ||
| ArrayObject property: {"a":6,"b":{"x":1},"c":3} | ||
| ArrayIterator property compound assign: {"a":"1x","b":{"x":1},"c":3} | ||
| ArrayObject property compound assign: {"a":"1x","b":{"x":1},"c":3} | ||
| ArrayIterator property nested write: {"a":1,"b":{"x":1,"z":3},"c":3} | ||
| ArrayObject property nested write: {"a":1,"b":{"x":1,"z":3},"c":3} | ||
| ArrayIterator property reference: {"a":1,"b":{"x":1},"c":7} | ||
| ArrayObject property reference: {"a":1,"b":{"x":1},"c":7} | ||
| ArrayIterator property unset: {"a":1,"b":{"x":1}} | ||
| ArrayObject property unset: {"a":1,"b":{"x":1}} | ||
| ArrayIterator foreach by reference: {"a":8,"b":8,"c":8} | ||
| ArrayObject foreach by reference: {"a":8,"b":8,"c":8} | ||
| ArrayObject uasort: {"a":1,"c":3,"b":{"x":1}} | ||
| ArrayObject ksort: {"a":1,"b":{"x":1},"c":3,"d":0} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.