From e8d25b0bc44335b4b35556c1f6db9cf1476f9ee5 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Wed, 26 Sep 2012 10:57:51 +0800 Subject: [PATCH] Fixed Bug #6 (bug with incorrect packing of mixed arrays) --- msgpack_pack.c | 22 +++++++++++++++++++++- package.xml | 22 +++++++++++++++++++--- tests/bug006.phpt | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 tests/bug006.phpt diff --git a/msgpack_pack.c b/msgpack_pack.c index 728f2c8..2ee0128 100644 --- a/msgpack_pack.c +++ b/msgpack_pack.c @@ -23,6 +23,26 @@ # define Z_ISREF_P(pz) PZVAL_IS_REF(pz) #endif +inline static int msgpack_check_ht_is_map(zval *array) +{ + int count = zend_hash_num_elements(Z_ARRVAL_P(array)); + + if (count != (Z_ARRVAL_P(array))->nNextFreeElement) { + return 1; + } else { + int i; + HashPosition pos = {0}; + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos); + for (i = 0; i < count; i++) { + if (zend_hash_get_current_key_type_ex(Z_ARRVAL_P(array), &pos) != HASH_KEY_IS_LONG) { + return 1; + } + zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos); + } + } + return 0; +} + inline static int msgpack_var_add( HashTable *var_hash, zval *var, void *var_old TSRMLS_DC) { @@ -296,7 +316,7 @@ inline static void msgpack_serialize_array( msgpack_pack_nil(buf); msgpack_pack_long(buf, MSGPACK_SERIALIZE_TYPE_REFERENCE); } - else if (ht->nNumOfElements == ht->nNextFreeElement) + else if (!msgpack_check_ht_is_map(val)) { hash = 0; msgpack_pack_array(buf, n); diff --git a/package.xml b/package.xml index 86c1b64..f4c6f1e 100644 --- a/package.xml +++ b/package.xml @@ -19,8 +19,8 @@ 2011-09-14 - 0.5.2 - 0.5.2 + 0.5.3 + 0.5.3 beta @@ -28,7 +28,7 @@ New BSD - - Initial release + - Fixed Bug #6 (bug with incorrect packing of mixed arrays) @@ -247,6 +247,7 @@ + @@ -263,6 +264,21 @@ msgpack + + 2012-09-14 + + 0.5.3 + 0.5.3 + + + beta + beta + + PHP License + + + + 2012-09-14 diff --git a/tests/bug006.phpt b/tests/bug006.phpt new file mode 100644 index 0000000..cda28fe --- /dev/null +++ b/tests/bug006.phpt @@ -0,0 +1,41 @@ +--TEST-- +Bug #6 (bug with incorrect packing of mixed arrays) +--SKIPIF-- + 2, 1 => 3); + +print_r(msgpack_unpack(msgpack_pack($data))); + +$var = array( 1=> "foo", 2 => "bar"); + +$var[0] = "dummy"; + +print_r(msgpack_unpack(msgpack_pack($var))); + +foreach ($var as $v) { + var_dump($v); +} +?> +--EXPECTF-- +Array +( + [key] => 2 + [1] => 3 +) +Array +( + [0] => dummy + [1] => foo + [2] => bar +) +string(3) "foo" +string(3) "bar" +string(5) "dummy"