json_decode() incorrectly decodes JSON objects with empty string keys
| Bug #41504 | json_decode() incorrectly decodes JSON objects with empty string keys | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2007-05-26 07:21 UTC | Modified: | 2008-11-18 10:58 UTC |
|
||||||||||
| From: | jmorrow at jessejmorrow dot com | Assigned: | iliaa (profile) | |||||||||||
| Status: | Closed | Package: | JSON related | |||||||||||
| PHP Version: | 5.2.2 | OS: | OS X | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2007-05-26 07:21 UTC] jmorrow at jessejmorrow dot com
Description:
------------
The json_decode() function improperly and incosistently decodes JSON
objects that have an empty string as one of their object keys/property
names. In some cases the server crashes, some cases work properly, and
other cases return "_empty_" as the array key.
The "_empty_" key and crashes are show stoppers for me.
Having an empty object key/property such as {"": "value"} is totally
valid, and even common, JSON.
As a side note, I'll open a separate bug for this, json_encode(array
(""=>"value")) does not work either.
Reproduce code:
---------------
1) var_dump(json_decode('{"":"value"}', true));
2) var_dump(json_decode('{"":"value", "key":"value"}', true));
3) var_dump(json_decode('{"key":"value", "":"value"}', true));
Expected result:
----------------
1) array(1) { [""]=> string(5) "value" }
2) array(2) { [""]=> string(5) "value" ["key"]=> string(5) "value" }
3) array(2) { ["key"]=> string(5) "value" [""]=> string(5) "value" }
Actual result:
--------------
1) Page fails to return - some kind of script crash
2) array(2) { ["_empty_"]=> string(5) "value" ["key"]=> string(5)
"value" } (the "_empty_" key coming up is the most common case when
there the key is an empty string)
3) array(2) { ["key"]=> string(5) "value" [""]=> string(5) "value" }
(works as expected)
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2007-05-26 20:20 UTC] iliaa@php.net
I am unable to reproduce the crash with var_dump(json_decode('{"":"value"}', true)); it returns "_empty_" => "value" for me. Analysis through valgrind does not report any errors either. The issue of "" being returned as "_empty_" is a bug and will be fixed.[2007-05-26 20:27 UTC] jmorrow at jessejmorrow dot com
[2007-05-27 16:31 UTC] iliaa@php.net