-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix bug #75237 (jsonSerialize() - Returning new instance of self causes segfault) #2763
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
+28
−0
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Bug #75237 (jsonSerialize() - Returning new instance of self causes segfault) | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("json")) die("skip ext/json required"); ?> | ||
--FILE-- | ||
<?php | ||
class Foo implements JsonSerializable { | ||
public function jsonSerialize() { | ||
return new self; | ||
} | ||
} | ||
|
||
try { | ||
var_dump(json_encode(new Foo)); | ||
} catch (Exception $e) { | ||
echo $e->getMessage(); | ||
} | ||
?> | ||
--EXPECT-- | ||
Foo::jsonSerialize() cannot return a new instance of itself |
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.
Are you trying to compare strings here?
ce->name
is azend_string
, so if you want to compare the char values,==
will only compare the addresses.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.
Nice catch! So if I wanted to compare
zend_string
's, would I change it to something like this:ZSTR_VAL(ce->name) == ZSTR_VAL(Z_OBJCE(retval)->name)
?Uh oh!
There was an error while loading. Please reload this page.
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.
You could use
zend_string_equals
, which takes 2zend_string *
arguments. So I believe you could do:zend_string_equals(ce->name, Z_OBJCE(retval)->name)
I'm not sure whatZ_OBJCE(retval)->name
expands to? Is it azend_string *
. or achar *
?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.
Oh awesome - thanks so much! :)
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.
@rmccullagh it's a
zend_string *
,Z_OBJCE
returns a_zend_class_entry
, and you can find the definition of the structure here. Anyway, I recommendzend_string_equals
as well. If there is a wrapper method, then use it.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.
Already merged, but I just watched your video so here's some thoughts:
If you're just trying to match the class, you can compare the ce only. This will be more correct than comparing names and (nominally) more performant.
if ((Z_TYPE_P(retval) == IS_OBJECT) && (Z_OBJCE_P(retval) == ce)) { ... }
The segfault is still easily triggerable via this script. As you mention in your video, there's a much bigger task involved in fixing this properly.
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.
@sgolemon this pr has been closed without merge. and the example is exactly what I gave here. BTW, what do you mean by video? I don't see any video mentioned here
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.
Thanks @sgolemon! I'll update my blog with your comment. :)
@jhdxr I made a little screencast of how I found & patched this bug. :)
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.
@jhdxr Oh, heh. I should really read the conversation first. I was only looking at the code review comments and interpreted the "Closed" as merged because we don't really merge via github. :p