Skip to content

Fixed bug #64874 ("json_decode handles whitespace and case-sensitivity incorrectly") #457

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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? 20??, PHP 5.7.0

- JSON:
. Fixed bug #64874 ("json_decode handles whitespace and case-sensitivity
incorrectly")

<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>
8 changes: 8 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ PHP X.Y UPGRADE NOTES
========================================


- JSON:
Fixed bug #64874 ("json_decode handles whitespace and case-sensitivity
incorrectly")
This means that when a non-lowercase JSON text containing only JSON true,
false or null is passed to json_decode(), it will error. Please note however
that non-lowercase true, false or null have never been accepted inside JSON
arrays or JSON strings. This only applies to deserialising single values.

========================================
2. New Features
========================================
Expand Down
6 changes: 3 additions & 3 deletions ext/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,14 +712,14 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len,

RETVAL_NULL();
if (trim_len == 4) {
if (!strncasecmp(trim, "null", trim_len)) {
if (!strncmp(trim, "null", trim_len)) {
/* We need to explicitly clear the error because its an actual NULL and not an error */
jp->error_code = PHP_JSON_ERROR_NONE;
RETVAL_NULL();
} else if (!strncasecmp(trim, "true", trim_len)) {
} else if (!strncmp(trim, "true", trim_len)) {
RETVAL_BOOL(1);
}
} else if (trim_len == 5 && !strncasecmp(trim, "false", trim_len)) {
} else if (trim_len == 5 && !strncmp(trim, "false", trim_len)) {
RETVAL_BOOL(0);
}

Expand Down
41 changes: 41 additions & 0 deletions ext/json/tests/bug64874_part2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Case-sensitivity part of bug #64874 ("json_decode handles whitespace and case-sensitivity incorrectly")
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
function decode($json) {
var_dump(json_decode($json));
var_dump(json_last_error() !== 0);
echo "\n";
}

// Only lowercase should work
decode('true');
decode('True');
decode('false');
decode('False');
decode('null');
decode('Null');

echo "Done\n";
--EXPECT--
bool(true)
bool(false)

NULL
bool(true)

bool(false)
bool(false)

NULL
bool(true)

NULL
bool(false)

NULL
bool(true)

Done