]> granicus.if.org Git - php/commitdiff
json_decode() should generate a syntax error when given "".
authorAdam Harvey <aharvey@php.net>
Mon, 2 Feb 2015 11:07:34 +0000 (11:07 +0000)
committerAdam Harvey <aharvey@php.net>
Mon, 2 Feb 2015 11:07:34 +0000 (11:07 +0000)
Fixes bug #68938 (json_decode() decodes empty string without error).
Patch by jeremy at bat-country dot us.

NEWS
ext/json/json.c
ext/json/tests/bug54484.phpt
ext/json/tests/bug68938.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 7a15879ebfff8d01c4c4830bcbf92d351cca968f..bcf021f528fc2a181b0103ab6b7002042a7f3b3b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,10 @@ PHP                                                                        NEWS
   . Fixed bug #68571 (core dump when webserver close the socket).
     (redfoxli069 at gmail dot com, Laruence)
 
+- JSON:
+  . Fixed bug #68938 (json_decode() decodes empty string without error).
+    (jeremy at bat-country dot us)
+
 - Libxml:
   . Fixed bug #64938 (libxml_disable_entity_loader setting is shared
     between threads). (Martin Jansen)
index 5b71eb06f6ef8d1f781fad0c29b7a47c6fc74916..27aed969d96d67136fd2a5edc5feef371f6fd8ea 100644 (file)
@@ -818,6 +818,7 @@ static PHP_FUNCTION(json_decode)
        JSON_G(error_code) = 0;
 
        if (!str_len) {
+               JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
                RETURN_NULL();
        }
 
index d698ab5416420d6a664f5866a4819404782b9a3a..e56d8bd86be92e80c0b66a51e5619e7cccda0691 100644 (file)
@@ -15,11 +15,16 @@ json_decode("invalid json");
 var_dump(json_last_error());
 
 
+json_decode("\001 invalid json");
+var_dump(json_last_error());
+
+
 json_decode("");
 var_dump(json_last_error());
 ?>
 --EXPECT--
 int(0)
-int(0)
 int(4)
-int(0)
+int(4)
+int(3)
+int(4)
diff --git a/ext/json/tests/bug68938.phpt b/ext/json/tests/bug68938.phpt
new file mode 100644 (file)
index 0000000..f6291ff
--- /dev/null
@@ -0,0 +1,11 @@
+--TEST--
+Bug #68938 (json_decode() decodes empty string without indicating error)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+json_decode("");
+var_dump(json_last_error());
+?>
+--EXPECT--
+int(4)