Skip to content

Commit faae3e9

Browse files
author
Ilia Alshanetsky
committed
Fixed bug #41504 (json_decode() incorrectly decodes JSON arrays with empty string keys).
1 parent 2c5368c commit faae3e9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? Jun 2007, PHP 5.2.3
44
- Fixed bug #41511 (Compile failure under IRIX 6.5.30 building md5.c). (Jani)
5+
- Fixed bug #41504 (json_decode() incorrectly decodes JSON arrays with empty
6+
string keys). (Ilia)
57

68
24 May 2007, PHP 5.2.3RC1
79
- Changed CGI install target to php-cgi and 'make install' to install CLI

ext/json/JSON_parser.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static void attach_zval(json_parser *json, int up, int cur, smart_str *key, int
364364
}
365365
else
366366
{
367-
add_assoc_zval_ex(root, (key->len ? key->c : "_empty_"), (key->len ? (key->len + 1) : sizeof("_empty_")), child);
367+
add_assoc_zval_ex(root, (key->len ? key->c : ""), (key->len ? (key->len + 1) : sizeof("")), child);
368368
}
369369
key->len = 0;
370370
}
@@ -507,7 +507,7 @@ JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
507507
}
508508
else
509509
{
510-
add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
510+
add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : ""), (key.len ? (key.len + 1) : sizeof("")), mval);
511511
}
512512
key.len = 0;
513513
buf.len = 0;
@@ -638,7 +638,7 @@ JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
638638
}
639639
else
640640
{
641-
add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
641+
add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : ""), (key.len ? (key.len + 1) : sizeof("")), mval);
642642
}
643643
key.len = 0;
644644
}

ext/json/tests/bug41504.phpt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #41504 (json_decode() converts empty array keys to "_empty_")
3+
--FILE--
4+
<?php
5+
6+
var_dump(json_decode('{"":"value"}', true));
7+
var_dump(json_decode('{"":"value", "key":"value"}', true));
8+
var_dump(json_decode('{"key":"value", "":"value"}', true));
9+
10+
echo "Done\n";
11+
?>
12+
--EXPECT--
13+
array(1) {
14+
[""]=>
15+
string(5) "value"
16+
}
17+
array(2) {
18+
[""]=>
19+
string(5) "value"
20+
["key"]=>
21+
string(5) "value"
22+
}
23+
array(2) {
24+
["key"]=>
25+
string(5) "value"
26+
[""]=>
27+
string(5) "value"
28+
}
29+
Done

0 commit comments

Comments
 (0)