Fix bug #45989 - json_decode() doesn't return NULL on certain invalid strings

This commit is contained in:
Scott MacVicar 2008-12-12 23:17:44 +00:00
parent 1095c62869
commit 8e1f9c8acb
5 changed files with 69 additions and 82 deletions

View File

@ -544,13 +544,7 @@ static PHP_FUNCTION(json_decode)
RETURN_DOUBLE(d);
}
}
if (str_len > 1 && *str.s == '"' && str.s[str_len-1] == '"') {
RETURN_STRINGL(str.s+1, str_len-2, 1);
} else if (*str.s == '{' || *str.s == '[') { /* invalid JSON string */
RETURN_NULL();
} else {
RETURN_STRINGL(str.s, str_len, 1);
}
RETURN_NULL();
}
else
{
@ -577,13 +571,7 @@ static PHP_FUNCTION(json_decode)
RETURN_DOUBLE(d);
}
}
if (str_len > 1 && *str.u == 0x22 /*'"'*/ && str.u[str_len-1] == 0x22 /*'"'*/) {
RETURN_UNICODEL(str.u+1, str_len-2, 1);
} else if (*str.u == 0x7b /*'{'*/ || *str.u == 0x5b /*'['*/ ) { /* invalid JSON string */
RETURN_NULL();
} else {
RETURN_UNICODEL(str.u, str_len, 1);
}
RETURN_NULL();
}
}
/* }}} */

View File

@ -31,12 +31,12 @@ NULL
NULL
NULL
NULL
unicode(1) "."
unicode(1) "."
unicode(3) "<?>"
unicode(1) ";"
unicode(6) "руссиш"
unicode(4) "blah"
NULL
NULL
NULL
NULL
NULL
NULL
NULL
object(stdClass)#%d (1) {
[u"test"]=>
@ -66,6 +66,6 @@ object(stdClass)#%d (1) {
unicode(0) ""
}
}
unicode(16) "{ "": { "": "" }"
unicode(16) "{ "": "": "" } }"
NULL
NULL
Done

View File

@ -13,19 +13,19 @@ echo "Done\n";
?>
--EXPECT--
array(1) {
[""]=>
string(5) "value"
[u""]=>
unicode(5) "value"
}
array(2) {
[""]=>
string(5) "value"
["key"]=>
string(5) "value"
[u""]=>
unicode(5) "value"
[u"key"]=>
unicode(5) "value"
}
array(2) {
["key"]=>
string(5) "value"
[""]=>
string(5) "value"
[u"key"]=>
unicode(5) "value"
[u""]=>
unicode(5) "value"
}
Done

View File

@ -16,10 +16,9 @@ var_dump(
?>
--EXPECT--
unicode(0) ""
unicode(5) "".."."
unicode(1) """
unicode(2) """"
NULL
NULL
NULL
string(4) ""\"""
unicode(1) """
unicode(2) """"

View File

@ -50,116 +50,116 @@ AS ARRAY
unicode(58) "A JSON payload should be an object or array, not a string."
Testing: ["Unclosed array"
AS OBJECT
unicode(17) "["Unclosed array""
NULL
AS ARRAY
unicode(17) "["Unclosed array""
NULL
Testing: {unquoted_key: "keys must be quoted}
AS OBJECT
unicode(36) "{unquoted_key: "keys must be quoted}"
NULL
AS ARRAY
unicode(36) "{unquoted_key: "keys must be quoted}"
NULL
Testing: ["extra comma",]
AS OBJECT
unicode(16) "["extra comma",]"
NULL
AS ARRAY
unicode(16) "["extra comma",]"
NULL
Testing: ["double extra comma",,]
AS OBJECT
unicode(24) "["double extra comma",,]"
NULL
AS ARRAY
unicode(24) "["double extra comma",,]"
NULL
Testing: [ , "<-- missing value"]
AS OBJECT
unicode(26) "[ , "<-- missing value"]"
NULL
AS ARRAY
unicode(26) "[ , "<-- missing value"]"
NULL
Testing: ["Comma after the close"],
AS OBJECT
unicode(26) "["Comma after the close"],"
NULL
AS ARRAY
unicode(26) "["Comma after the close"],"
NULL
Testing: ["Extra close"]]
AS OBJECT
unicode(16) "["Extra close"]]"
NULL
AS ARRAY
unicode(16) "["Extra close"]]"
NULL
Testing: {"Extra comma": true,}
AS OBJECT
unicode(22) "{"Extra comma": true,}"
NULL
AS ARRAY
unicode(22) "{"Extra comma": true,}"
NULL
Testing: {"Extra value after close": true} "misplaced quoted value"
AS OBJECT
unicode(58) "{"Extra value after close": true} "misplaced quoted value""
NULL
AS ARRAY
unicode(58) "{"Extra value after close": true} "misplaced quoted value""
NULL
Testing: {"Illegal expression": 1 + 2}
AS OBJECT
unicode(29) "{"Illegal expression": 1 + 2}"
NULL
AS ARRAY
unicode(29) "{"Illegal expression": 1 + 2}"
NULL
Testing: {"Illegal invocation": alert()}
AS OBJECT
unicode(31) "{"Illegal invocation": alert()}"
NULL
AS ARRAY
unicode(31) "{"Illegal invocation": alert()}"
NULL
Testing: {"Numbers cannot have leading zeroes": 013}
AS OBJECT
unicode(43) "{"Numbers cannot have leading zeroes": 013}"
NULL
AS ARRAY
unicode(43) "{"Numbers cannot have leading zeroes": 013}"
NULL
Testing: {"Numbers cannot be hex": 0x14}
AS OBJECT
unicode(31) "{"Numbers cannot be hex": 0x14}"
NULL
AS ARRAY
unicode(31) "{"Numbers cannot be hex": 0x14}"
NULL
Testing: ["Illegal backslash escape: \x15"]
AS OBJECT
unicode(34) "["Illegal backslash escape: \x15"]"
NULL
AS ARRAY
unicode(34) "["Illegal backslash escape: \x15"]"
NULL
Testing: ["Illegal backslash escape: \'"]
AS OBJECT
unicode(32) "["Illegal backslash escape: \'"]"
NULL
AS ARRAY
unicode(32) "["Illegal backslash escape: \'"]"
NULL
Testing: ["Illegal backslash escape: \017"]
AS OBJECT
unicode(34) "["Illegal backslash escape: \017"]"
NULL
AS ARRAY
unicode(34) "["Illegal backslash escape: \017"]"
NULL
Testing: [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
NULL
Testing: {"Missing colon" null}
AS OBJECT
unicode(22) "{"Missing colon" null}"
NULL
AS ARRAY
unicode(22) "{"Missing colon" null}"
NULL
Testing: {"Double colon":: null}
AS OBJECT
unicode(23) "{"Double colon":: null}"
NULL
AS ARRAY
unicode(23) "{"Double colon":: null}"
NULL
Testing: {"Comma instead of colon", null}
AS OBJECT
unicode(32) "{"Comma instead of colon", null}"
NULL
AS ARRAY
unicode(32) "{"Comma instead of colon", null}"
NULL
Testing: ["Colon instead of comma": false]
AS OBJECT
unicode(33) "["Colon instead of comma": false]"
NULL
AS ARRAY
unicode(33) "["Colon instead of comma": false]"
NULL
Testing: ["Bad value", truth]
AS OBJECT
unicode(20) "["Bad value", truth]"
NULL
AS ARRAY
unicode(20) "["Bad value", truth]"
NULL
Testing: ['single quote']
AS OBJECT
unicode(16) "['single quote']"
NULL
AS ARRAY
unicode(16) "['single quote']"
NULL