Fixed case part of bug #64874 ("json_decode handles whitespace and case-sensitivity incorrectly") #527
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.
This request fixes the JSON parser's handling of top-level true/false/null. By that I mean a lone true/false/null value, not one inside an array of object. Hence,
json_decode("True");
(currently valid) is affected butjson_decode("[True]");
(currently invalid) is not.At present, there is a bug where non-lowercase forms (e.g.
True
ortRUE
) of these top-level values are accepted by the parser without erroring. This behaviour is not compliant with the JSON.org standard, nor the IETF RFC, nor the ECMA standard. It is also inconsistent with other JSON implementations. Were I to tryJSON.parse('True');
in JavaScript orjson.loads('true')
in Python, I would be met with an error. The function is even inconsistent with itself in this respect, as[True]
will fail butTrue
will not. I suspect that this behaviour was not intended and simply came about because of someone failing to read the specification and assuming case-insensitivity.Hence I propose this patch. It replaces
strcasecmp
withstrcmp
where the former was used incorrectly. It is a minor backwards-compatibility break, so I am targeting this at 5.6. However I believe that it is very unlikely that any code relied on this bug, since all JSON serialisers output lowercase literals, and any hand-written JSON is likely not a lone true, false or null value.In the event that code relied on this bug, they could simply do something like this:
So, I ask that this is merged, as a tiny backwards-compatibility break but bringing PHP into harmony with other JSON implementations and the specification.
EDIT: I should note that this is a literal 3-line change, replacing
strcasecmp
forstrcmp
on three different lines.EDIT 2: dsp asked me where in the JSON standard it says that it must be lowercase. It's in RFC 4627, page 2: