Skip to content

Commit

Permalink
Fixed Bug #6 (bug with incorrect packing of mixed arrays)
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Sep 26, 2012
1 parent 21b22bf commit e8d25b0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
22 changes: 21 additions & 1 deletion msgpack_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 19 additions & 3 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
<date>2011-09-14</date>
<time>22:39:08</time>
<version>
<release>0.5.2</release>
<api>0.5.2</api>
<release>0.5.3</release>
<api>0.5.3</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license filesource="LICENSE">New BSD</license>
<notes>
- Initial release
- Fixed Bug #6 (bug with incorrect packing of mixed arrays)
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -247,6 +247,7 @@
<file name="135b.phpt" role="test" />
<file name="136.phpt" role="test" />
<file name="136b.phpt" role="test" />
<file name="bug006.phpt" role="test" />
</dir>
</dir>
</contents>
Expand All @@ -263,6 +264,21 @@
<providesextension>msgpack</providesextension>
<extsrcrelease />
<changelog>
<release>
<date>2012-09-14</date>
<version>
<release>0.5.3</release>
<api>0.5.3</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
</notes>
</release>

<release>
<date>2012-09-14</date>
<version>
Expand Down
41 changes: 41 additions & 0 deletions tests/bug006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Bug #6 (bug with incorrect packing of mixed arrays)
--SKIPIF--
<?php
if (version_compare(PHP_VERSION, '5.2.0') < 0) {
echo "skip tests in PHP 5.2 or newer";
}
if (!extension_loaded("msgpack")) {
echo "skip";
}
--FILE--
<?php
$data = array('key' => 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"

0 comments on commit e8d25b0

Please sign in to comment.