-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-15833: Segmentation fault (access null pointer) in ext/spl/spl_array.c #17235
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
GH-15833 (Segmentation fault (access null pointer) in ext/spl/spl_array.c) | ||
--CREDITS-- | ||
YuanchengJiang | ||
--FILE-- | ||
<?php | ||
class C { | ||
public int $a = 1; | ||
} | ||
$reflector = new ReflectionClass(C::class); | ||
$obj = $reflector->newLazyProxy(function ($obj) { | ||
$obj = new C(); | ||
return $obj; | ||
}); | ||
$recursiveArrayIterator = new RecursiveArrayIterator($obj); | ||
var_dump($recursiveArrayIterator->current()); | ||
$recursiveArrayIterator->next(); | ||
var_dump($recursiveArrayIterator->current()); | ||
?> | ||
--EXPECT-- | ||
int(1) | ||
NULL |
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,41 @@ | ||
--TEST-- | ||
GH-15833 (Segmentation fault (access null pointer) in ext/spl/spl_array.c) | ||
--CREDITS-- | ||
YuanchengJiang | ||
--FILE-- | ||
<?php | ||
class C { | ||
public int $a = 1; | ||
} | ||
$reflector = new ReflectionClass(C::class); | ||
$obj = $reflector->newLazyProxy(function ($obj) { | ||
static $counter = 0; | ||
throw new Error('nope ' . ($counter++)); | ||
}); | ||
$recursiveArrayIterator = new RecursiveArrayIterator($obj); | ||
try { | ||
var_dump($recursiveArrayIterator->current()); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
var_dump($recursiveArrayIterator->current()); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
$recursiveArrayIterator->next(); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
var_dump($recursiveArrayIterator->current()); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
nope 0 | ||
nope 1 | ||
nope 2 | ||
nope 3 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zend_lazy_object_init()
may returnNULL
when initialization fails (in which caseEG(exception)
is set, too).Unfortunately I don't see other solution than to return
NULL
fromspl_array_get_hash_table_ptr
and to handle that in every caller. (An alternative would be to return a ptr-ptr to an empty array, and to rely on the exception to discard the result of the operation, but then we need to manage that array somehow.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch, okay, I'll do the NULL thing then :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I believe we can just store the empty array in
intern->array
. I pushed a commit that does that, so the total patch stays simple. The only downside is that any further action won't do anything on the object, which may be fine as the object failed initialization anyway. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if further actions on the ArrayObject was carried normally, causing a new attempt to initialize the object and probably a new exception. Otherwise the silent failure could be surprising.
Storing the empty array in a new field in
spl_array_object
would be fine I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I also added some checks to avoid unexpected modification to the sentinel