From 321184da5e7dfa8cb043886934cbe714e7d3447b Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Thu, 20 Jul 2006 08:23:45 +0000 Subject: [PATCH] fix segfault caused by empty keys is JSON data add test --- ext/json/JSON_parser.c | 12 +++---- ext/json/tests/001.phpt | 69 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 ext/json/tests/001.phpt diff --git a/ext/json/JSON_parser.c b/ext/json/JSON_parser.c index adb974346e..f4ee177548 100644 --- a/ext/json/JSON_parser.c +++ b/ext/json/JSON_parser.c @@ -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 index 0000000000..89a9528d9a --- /dev/null +++ b/ext/json/tests/001.phpt @@ -0,0 +1,69 @@ +--TEST-- +json_decode() tests +--FILE-- +")); +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 -- 2.40.0