php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54484 Empty string in json_decode doesn't set/reset json_last_error
Submitted: 2011-04-07 14:24 UTC Modified: 2012-10-22 02:35 UTC
Votes:3
Avg. Score:3.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: wildcat at the-wildcat dot de Assigned: aharvey (profile)
Status: Closed Package: JSON related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
 [2011-04-07 14:24 UTC] wildcat at the-wildcat dot de
Description:
------------
If you decode an empty json value, the internal error state doesn't change.

Decode a valid json value and after that an empty value, the error state is 0

Decode an invalid json value and after that an empty value, the error state remains 4 (JSON_ERROR_SYNTAX) (I think it's the same for all error states, but haven't actually tested)

I'm not quite sure whether an empty string is valid json and should reset the error state to 0 or whether it is invalid json and should set the error state to 4. Because a valid empty json string is '""' and not ''

Test script:
---------------
<?php
error_reporting(E_ALL);

json_decode('{"test":"test"}');
var_dump(json_last_error());

json_decode("");
var_dump(json_last_error());


json_decode("invalid json");
var_dump(json_last_error());


json_decode("");
var_dump(json_last_error());

?>

Expected result:
----------------
int(0)
int(0)
int(4)
int(0)


OR

int(0)
int(4)
int(4)
int(4)


Actual result:
--------------
int(0)
int(0)
int(4)
int(4)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-08 08:20 UTC] scottmac@php.net
a string of length 0 skips the decoder and returns null. I'm thinking it should 
set the error code to 4 so I'll fix it to do so.
 [2011-04-08 09:59 UTC] aharvey@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: scottmac
 [2011-06-01 11:44 UTC] iliaa@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=311708
Log: Fixed bug #54484 (Empty string in json_decode doesn't reset json_last_error()).
 [2011-06-01 11:44 UTC] iliaa@php.net
-Status: Assigned +Status: Closed -Assigned To: scottmac +Assigned To: iliaa
 [2011-06-01 11:44 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2012-04-18 09:50 UTC] laruence@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=850db4b315eb56d261098a6f42df6a8375cbd937
Log: Fixed bug #54484 (Empty string in json_decode doesn't reset json_last_error()).
 [2012-07-24 23:41 UTC] rasmus@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=850db4b315eb56d261098a6f42df6a8375cbd937
Log: Fixed bug #54484 (Empty string in json_decode doesn't reset json_last_error()).
 [2012-10-22 02:35 UTC] aharvey@php.net
I don't think the above commit fixed the entire issue: it ensures that the error state is reset to 0 when json_decode() is called, but doesn't deal with actually setting an error state when we return NULL due to an empty string (which I agree should result in a syntax error state).

Assigning to self.
 [2012-10-22 02:35 UTC] aharvey@php.net
-Status: Closed +Status: Re-Opened -Assigned To: iliaa +Assigned To: aharvey
 [2013-11-17 09:37 UTC] laruence@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=850db4b315eb56d261098a6f42df6a8375cbd937
Log: Fixed bug #54484 (Empty string in json_decode doesn't reset json_last_error()).
 [2013-11-17 09:37 UTC] laruence@php.net
-Status: Re-Opened +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC