Bug #68938 [Opn->Csd]: json_decode() decodes empty string without error
| From: | aharvey@php.net | Date: | Mon, 02 Feb 2015 11:13:43 +0000 |
| Subject: | Bug #68938 [Opn->Csd]: json_decode() decodes empty string without error | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190407@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68938&edit=1
ID: 68938
Updated by: aharvey@php.net
Reported by: jeremy at bat-country dot us
Summary: json_decode() decodes empty string without error
-Status: Open
+Status: Closed
Type: Bug
Package: JSON related
PHP Version: 5.6.5
-Assigned To:
+Assigned To: aharvey
Block user comment: N
Private report: N
New Comment:
Sorry; I dropped the ball on the original bug report, so the least I can do is merge in your quite
correct patch. Thanks!
Previous Comments:
------------------------------------------------------------------------
[2015-01-28 21:51:07] jeremy at bat-country dot us
Description:
------------
PHP's json_decode() function accepts an empty string as JSON data and returns NULL without
setting json_last_error(). An empty string is not valid JSON; see productions in RFC 4627 <https://tools.ietf.org/html/rfc4627> and RFC 7159
<https://tools.ietf.org/html/rfc7159>, or
try decoding an empty string using builtin JSON parsers in Python, Go, or V8: all of them raise
syntax errors.
To be consistent with other errors, trying to decode an empty string should return NULL but set
json_last_error() to indicate a syntax error. For example, this is what happens when
json_decode(" ") is called.
This affects PHP 5.4.32 on RHEL 5, PHP 5.6.5 built from the Git tag on Mac OS X 10.9, and probably
any other configuration with the JSON extension.
It looks like this behavior was discovered during the course of bug #54484 <https://bugs.php.net/bug.php?id=54484> but never
fixed. I've included a patch that fixes it, a regression test, and an update to the test for
bug #54484.
Test script:
---------------
--TEST--
Bug #XXXXX (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)
Actual result:
--------------
int(0)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68938&edit=1