]> granicus.if.org Git - php/commitdiff
Fixed bug #41504 (json_decode() incorrectly decodes JSON arrays with empty
authorIlia Alshanetsky <iliaa@php.net>
Sun, 27 May 2007 16:31:35 +0000 (16:31 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Sun, 27 May 2007 16:31:35 +0000 (16:31 +0000)
string keys).

NEWS
ext/json/JSON_parser.c
ext/json/tests/bug41504.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index b57915ffd640c80c8639bbd9ce4de737065996f5..123c773a3fb0b4c523bd052f8457a00b54ae8442 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@ PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? Jun 2007, PHP 5.2.3
 - Fixed bug #41511 (Compile failure under IRIX 6.5.30 building md5.c). (Jani)
+- Fixed bug #41504 (json_decode() incorrectly decodes JSON arrays with empty
+  string keys). (Ilia)
 
 24 May 2007, PHP 5.2.3RC1
 - Changed CGI install target to php-cgi and 'make install' to install CLI
index 91f565e03383b28f457e6a91798ad37f6a77d2cf..2f2d26812562bbbccaea4c46fe973ae96300ba13 100644 (file)
@@ -364,7 +364,7 @@ static void attach_zval(json_parser *json, int up, int cur, smart_str *key, int
         }
         else
         {
-            add_assoc_zval_ex(root, (key->len ? key->c : "_empty_"), (key->len ? (key->len + 1) : sizeof("_empty_")), child);
+            add_assoc_zval_ex(root, (key->len ? key->c : ""), (key->len ? (key->len + 1) : sizeof("")), child);
         }
         key->len = 0;
     }
@@ -507,7 +507,7 @@ JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
                     }
                     else
                     {
-                        add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
+                        add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : ""), (key.len ? (key.len + 1) : sizeof("")), mval);
                     }
                     key.len = 0;
                     buf.len = 0;
@@ -638,7 +638,7 @@ JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
                                 }
                                 else
                                 {
-                                    add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
+                                    add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : ""), (key.len ? (key.len + 1) : sizeof("")), mval);
                                 }
                                 key.len = 0;
                             }
diff --git a/ext/json/tests/bug41504.phpt b/ext/json/tests/bug41504.phpt
new file mode 100644 (file)
index 0000000..6e51bb9
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Bug #41504 (json_decode() converts empty array keys to "_empty_")
+--FILE--
+<?php
+
+var_dump(json_decode('{"":"value"}', true));
+var_dump(json_decode('{"":"value", "key":"value"}', true));
+var_dump(json_decode('{"key":"value", "":"value"}', true));
+
+echo "Done\n";
+?>
+--EXPECT--     
+array(1) {
+  [""]=>
+  string(5) "value"
+}
+array(2) {
+  [""]=>
+  string(5) "value"
+  ["key"]=>
+  string(5) "value"
+}
+array(2) {
+  ["key"]=>
+  string(5) "value"
+  [""]=>
+  string(5) "value"
+}
+Done
\ No newline at end of file