From: Scott MacVicar Date: Fri, 12 Dec 2008 23:17:44 +0000 (+0000) Subject: Fix bug #45989 - json_decode() doesn't return NULL on certain invalid strings X-Git-Tag: php-5.4.0alpha1~191^2~4819 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8e1f9c8acb8eb4af36b61f8e82775df5804cfb32;p=php Fix bug #45989 - json_decode() doesn't return NULL on certain invalid strings --- diff --git a/ext/json/json.c b/ext/json/json.c index 6f5193019e..d00d5bb254 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -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(); } } /* }}} */ diff --git a/ext/json/tests/001.phpt b/ext/json/tests/001.phpt index a106e89f85..3aec70f527 100644 --- a/ext/json/tests/001.phpt +++ b/ext/json/tests/001.phpt @@ -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 diff --git a/ext/json/tests/bug41504.phpt b/ext/json/tests/bug41504.phpt index bef497404f..b1ff543ec7 100644 --- a/ext/json/tests/bug41504.phpt +++ b/ext/json/tests/bug41504.phpt @@ -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 diff --git a/ext/json/tests/bug42090.phpt b/ext/json/tests/bug42090.phpt index f8f370c834..2b24b30d41 100644 --- a/ext/json/tests/bug42090.phpt +++ b/ext/json/tests/bug42090.phpt @@ -16,10 +16,9 @@ var_dump( ?> --EXPECT-- unicode(0) "" -unicode(5) "".."." -unicode(1) """ -unicode(2) """" +NULL +NULL +NULL string(4) ""\""" unicode(1) """ unicode(2) """" - diff --git a/ext/json/tests/fail001.phpt b/ext/json/tests/fail001.phpt index 7b255b18e5..44fc64ed29 100644 --- a/ext/json/tests/fail001.phpt +++ b/ext/json/tests/fail001.phpt @@ -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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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" +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