Skip to content

Don't build prop table during json_encode(JsonSerializable) #9703

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ext/json/json_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,20 @@ static zend_result php_json_escape_string(

static zend_result php_json_encode_serializable_object(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
{
zend_object *obj = Z_OBJ_P(val);
zend_class_entry *ce = Z_OBJCE_P(val);
HashTable* myht = Z_OBJPROP_P(val);
zval retval, fname;
zend_result return_code;

if (myht && GC_IS_RECURSIVE(myht)) {
if (GC_IS_RECURSIVE(obj)) {
encoder->error_code = PHP_JSON_ERROR_RECURSION;
if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
smart_str_appendl(buf, "null", 4);
}
return FAILURE;
}

PHP_JSON_HASH_PROTECT_RECURSION(myht);
GC_PROTECT_RECURSION(obj);

ZVAL_STRING(&fname, "jsonSerialize");

Expand All @@ -560,7 +560,7 @@ static zend_result php_json_encode_serializable_object(smart_str *buf, zval *val
if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
smart_str_appendl(buf, "null", 4);
}
PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
GC_UNPROTECT_RECURSION(obj);
return FAILURE;
}

Expand All @@ -572,19 +572,19 @@ static zend_result php_json_encode_serializable_object(smart_str *buf, zval *val
if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
smart_str_appendl(buf, "null", 4);
}
PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
GC_UNPROTECT_RECURSION(obj);
return FAILURE;
}

if ((Z_TYPE(retval) == IS_OBJECT) &&
(Z_OBJ(retval) == Z_OBJ_P(val))) {
/* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
GC_UNPROTECT_RECURSION(obj);
return_code = php_json_encode_array(buf, &retval, options, encoder);
} else {
/* All other types, encode as normal */
return_code = php_json_encode_zval(buf, &retval, options, encoder);
PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
GC_UNPROTECT_RECURSION(obj);
}

zval_ptr_dtor(&retval);
Expand Down
5 changes: 1 addition & 4 deletions ext/json/tests/bug77843.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@ var_dump(json_encode([&$arr]));

?>
--EXPECT--
object(X)#1 (1) {
["prop"]=>
string(5) "value"
}
*RECURSION*
string(20) "[[{"prop":"value"}]]"
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ array(4) {
[12]=>
int(6)
["test"]=>
object(JsonSerializable@anonymous)#2 (0) {
}
*RECURSION*
}