Bug #68938 [NEW]: json_decode() decodes empty string without error
| From: | jeremy at bat-country dot us | Date: | Wed, 28 Jan 2015 21:51:08 +0000 |
| Subject: | Bug #68938 [NEW]: json_decode() decodes empty string without error | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-190285@lists.php.net to get a copy of this message | ||
From: jeremy at bat-country dot us
Operating system:
PHP version: 5.6.5
Package: JSON related
Bug Type: Bug
Bug description:json_decode() decodes empty string without error
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 bug report at https://bugs.php.net/bug.php?id=68938&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68938&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68938&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68938&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68938&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68938&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68938&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68938&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68938&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68938&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68938&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68938&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68938&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68938&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68938&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68938&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68938&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68938&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68938&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68938&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68938&r=mysqlcfg