Skip to content

Commit a7b3abe

Browse files
committed
json_decode() should generate a syntax error when given "".
Fixes bug #68938 (json_decode() decodes empty string without error). Patch by jeremy at bat-country dot us.
1 parent fb803ff commit a7b3abe

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ PHP NEWS
2424
. Fixed bug #68571 (core dump when webserver close the socket).
2525
(redfoxli069 at gmail dot com, Laruence)
2626

27+
- JSON:
28+
. Fixed bug #68938 (json_decode() decodes empty string without error).
29+
(jeremy at bat-country dot us)
30+
2731
- Libxml:
2832
. Fixed bug #64938 (libxml_disable_entity_loader setting is shared
2933
between threads). (Martin Jansen)

ext/json/json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ static PHP_FUNCTION(json_decode)
818818
JSON_G(error_code) = 0;
819819

820820
if (!str_len) {
821+
JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
821822
RETURN_NULL();
822823
}
823824

ext/json/tests/bug54484.phpt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ json_decode("invalid json");
1515
var_dump(json_last_error());
1616

1717

18+
json_decode("\001 invalid json");
19+
var_dump(json_last_error());
20+
21+
1822
json_decode("");
1923
var_dump(json_last_error());
2024
?>
2125
--EXPECT--
2226
int(0)
23-
int(0)
2427
int(4)
25-
int(0)
28+
int(4)
29+
int(3)
30+
int(4)

ext/json/tests/bug68938.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Bug #68938 (json_decode() decodes empty string without indicating error)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
json_decode("");
8+
var_dump(json_last_error());
9+
?>
10+
--EXPECT--
11+
int(4)

0 commit comments

Comments
 (0)