]> granicus.if.org Git - php/commitdiff
fix segfault caused by empty keys is JSON data
authorAntony Dovgal <tony2001@php.net>
Thu, 20 Jul 2006 08:23:45 +0000 (08:23 +0000)
committerAntony Dovgal <tony2001@php.net>
Thu, 20 Jul 2006 08:23:45 +0000 (08:23 +0000)
add test

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

index adb974346ec4c6a79b1f37e552ae7766f1dff8bd..f4ee177548b4d77b1ef0503d018551e48f1bf80d 100644 (file)
@@ -338,14 +338,14 @@ static void attach_zval(json_parser *json, int up, int cur, smart_str *key, int
     {
         if (!assoc)
         {
-            add_property_zval(root, key->c, child);
+            add_property_zval_ex(root, (key->len ? key->c : "_empty_"), (key->len ? (key->len + 1) : sizeof("_empty_")), child);
 #if PHP_MAJOR_VERSION >= 5
             ZVAL_DELREF(child);
 #endif
         }
         else
         {
-            add_assoc_zval(root, key->c, child);
+            add_assoc_zval_ex(root, (key->len ? key->c : "_empty_"), (key->len ? (key->len + 1) : sizeof("_empty_")), child);
         }
         key->len = 0;
     }
@@ -481,14 +481,14 @@ JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
 
                     if (!assoc)
                     {
-                        add_property_zval(JSON(the_zstack)[JSON(the_top)], key.c, mval);
+                        add_property_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
 #if PHP_MAJOR_VERSION >= 5
                         ZVAL_DELREF(mval);
 #endif
                     }
                     else
                     {
-                        add_assoc_zval(JSON(the_zstack)[JSON(the_top)], key.c, mval);
+                        add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
                     }
                     key.len = 0;
                     buf.len = 0;
@@ -604,14 +604,14 @@ JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
                             {
                                 if (!assoc)
                                 {
-                                    add_property_zval(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), mval);
+                                    add_property_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
 #if PHP_MAJOR_VERSION >= 5
                                     ZVAL_DELREF(mval);
 #endif
                                 }
                                 else
                                 {
-                                    add_assoc_zval(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), mval);
+                                    add_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
                                 }
                                 key.len = 0;
                             }
diff --git a/ext/json/tests/001.phpt b/ext/json/tests/001.phpt
new file mode 100644 (file)
index 0000000..89a9528
--- /dev/null
@@ -0,0 +1,69 @@
+--TEST--
+json_decode() tests
+--FILE--
+<?php
+
+var_dump(json_decode());
+var_dump(json_decode(""));
+var_dump(json_decode("", 1));
+var_dump(json_decode("", 0));
+var_dump(json_decode(".", 1));
+var_dump(json_decode(".", 0));
+var_dump(json_decode("<?>"));
+var_dump(json_decode(";"));
+var_dump(json_decode("руссиш"));
+var_dump(json_decode("blah"));
+var_dump(json_decode(NULL));
+var_dump(json_decode('{ "test": { "foo": "bar" } }'));
+var_dump(json_decode('{ "test": { "foo": "" } }'));
+var_dump(json_decode('{ "": { "foo": "" } }'));
+var_dump(json_decode('{ "": { "": "" } }'));
+var_dump(json_decode('{ "": { "": "" }'));
+var_dump(json_decode('{ "": "": "" } }'));
+
+echo "Done\n";
+?>
+--EXPECTF--
+Warning: json_decode() expects at least 1 parameter, 0 given in %s on line %d
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+object(stdClass)#1 (1) {
+  ["test"]=>
+  object(stdClass)#2 (1) {
+    ["foo"]=>
+    string(3) "bar"
+  }
+}
+object(stdClass)#1 (1) {
+  ["test"]=>
+  object(stdClass)#2 (1) {
+    ["foo"]=>
+    string(0) ""
+  }
+}
+object(stdClass)#1 (1) {
+  ["_empty_"]=>
+  object(stdClass)#2 (1) {
+    ["foo"]=>
+    string(0) ""
+  }
+}
+object(stdClass)#1 (1) {
+  ["_empty_"]=>
+  object(stdClass)#2 (1) {
+    ["_empty_"]=>
+    string(0) ""
+  }
+}
+NULL
+NULL
+Done