Skip to content

Commit

Permalink
Fixed bug #12 (msgpack_seriallize interfere with php serialize)
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Feb 18, 2013
1 parent 21a99f2 commit 48e2148
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
8 changes: 4 additions & 4 deletions msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ PS_SERIALIZER_ENCODE_FUNC(msgpack)
smart_str buf = {0};
msgpack_serialize_data_t var_hash;

PHP_VAR_SERIALIZE_INIT(var_hash);
msgpack_serialize_var_init(&var_hash);

#if PHP_API_VERSION < 20100412
msgpack_serialize_zval(&buf, PS(http_session_vars), &var_hash TSRMLS_CC);
Expand All @@ -161,7 +161,7 @@ PS_SERIALIZER_ENCODE_FUNC(msgpack)
smart_str_0(&buf);
*newstr = buf.c;

PHP_VAR_SERIALIZE_DESTROY(var_hash);
msgpack_serialize_var_destroy(&var_hash);

return SUCCESS;
}
Expand Down Expand Up @@ -233,15 +233,15 @@ PHP_MSGPACK_API void php_msgpack_serialize(smart_str *buf, zval *val TSRMLS_DC)
{
msgpack_serialize_data_t var_hash;

PHP_VAR_SERIALIZE_INIT(var_hash);
msgpack_serialize_var_init(&var_hash);

#if PHP_API_VERSION < 20100412
msgpack_serialize_zval(buf, val, &var_hash TSRMLS_CC);
#else
msgpack_serialize_zval(buf, val, var_hash TSRMLS_CC);
#endif

PHP_VAR_SERIALIZE_DESTROY(var_hash);
msgpack_serialize_var_destroy(&var_hash);
}

PHP_MSGPACK_API void php_msgpack_unserialize(
Expand Down
28 changes: 28 additions & 0 deletions msgpack_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,34 @@ inline static zend_class_entry* msgpack_unserialize_class(
return ce;
}

void msgpack_serialize_var_init(msgpack_serialize_data_t *var_hash)
{

#if PHP_API_VERSION < 20100412
HashTable *var_hash_ptr = (HashTable *)var_hash;
zend_hash_init(var_hash_ptr, 10, NULL, NULL, 0);
#else
HashTable **var_hash_ptr = (HashTable **)var_hash;
ALLOC_HASHTABLE(*var_hash_ptr);
zend_hash_init(*var_hash_ptr, 10, NULL, NULL, 0);
#endif

}

void msgpack_serialize_var_destroy(msgpack_serialize_data_t *var_hash)
{

#if PHP_API_VERSION < 20100412
HashTable *var_hash_ptr = (HashTable *)var_hash;
zend_hash_destroy(var_hash_ptr);
#else
HashTable **var_hash_ptr = (HashTable **)var_hash;
zend_hash_destroy(*var_hash_ptr);
FREE_HASHTABLE(*var_hash_ptr);
#endif

}

void msgpack_unserialize_var_init(msgpack_unserialize_data_t *var_hashx)
{
var_hashx->first = 0;
Expand Down
25 changes: 21 additions & 4 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
<email>laruence@php.net</email>
<active>yes</active>
</lead>
<date>2012-10-12</date>
<date>2013-02-18</date>
<time>15:58:08</time>
<version>
<release>0.5.3</release>
<api>0.5.3</api>
<release>0.5.4</release>
<api>0.5.4</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license filesource="LICENSE">New BSD</license>
<notes>
- Fixed Bug #12 (msgpack_seriallize interfere with php serialize)
- Fixed Bug #6 (bug with incorrect packing of mixed arrays)
</notes>
<contents>
Expand Down Expand Up @@ -248,6 +249,7 @@
<file name="136.phpt" role="test" />
<file name="136b.phpt" role="test" />
<file name="bug006.phpt" role="test" />
<file name="bug012.phpt" role="test" />
</dir>
</dir>
</contents>
Expand All @@ -264,6 +266,22 @@
<providesextension>msgpack</providesextension>
<extsrcrelease />
<changelog>
<release>
<date>2013-02-18</date>
<version>
<release>0.5.4</release>
<api>0.5.4</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed Bug #12 (msgpack_seriallize interfere with php serialize)
</notes>
</release>

<release>
<date>2012-10-12</date>
<version>
Expand All @@ -279,7 +297,6 @@
- Fixed Bug #6 (bug with incorrect packing of mixed arrays)
</notes>
</release>

<release>
<date>2012-09-14</date>
<version>
Expand Down
60 changes: 60 additions & 0 deletions tests/bug012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
Bug #12 (msgpack_seriallize interfere with php serialize)
--SKIPIF--
<?php
if (!extension_loaded("msgpack")) {
echo "skip";
}
if (version_compare(PHP_VERSION, '5.4.0') < 0) {
echo "skip tests before PHP 5.4";
}
--FILE--
<?php

class Demo extends ArrayObject {

}

$obj = new StdClass();

$demo = new Demo;

$demo[] = $obj;
$demo[] = $obj;

$data = array(
$demo,
$obj,
$obj,
);

print_r(msgpack_unserialize(msgpack_serialize($data)));
?>
--EXPECTF--
Array
(
[0] => Demo Object
(
[storage:ArrayObject:private] => Array
(
[0] => stdClass Object
(
)

[1] => stdClass Object
(
)

)

)

[1] => stdClass Object
(
)

[2] => stdClass Object
(
)

)

0 comments on commit 48e2148

Please sign in to comment.