Skip to content

Commit 495ad07

Browse files
committed
fix #132
check if we actually got a usable zval type after attempting to deserialize the session
1 parent c587568 commit 495ad07

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

msgpack.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ PS_SERIALIZER_DECODE_FUNC(msgpack) /* {{{ */ {
164164

165165
msgpack_unserialize_var_init(&var_hash);
166166

167+
ZVAL_UNDEF(&tmp);
167168
mp.user.retval = &tmp;
168169
mp.user.var_hash = &var_hash;
169170

@@ -175,13 +176,18 @@ PS_SERIALIZER_DECODE_FUNC(msgpack) /* {{{ */ {
175176
if (ret == MSGPACK_UNPACK_EXTRA_BYTES || ret == MSGPACK_UNPACK_SUCCESS) {
176177
msgpack_unserialize_var_destroy(&var_hash, 0);
177178

178-
ZEND_HASH_FOREACH_STR_KEY_VAL(HASH_OF(mp.user.retval), key_str, value) {
179-
if (key_str) {
180-
php_set_session_var(key_str, value, NULL);
181-
php_add_session_var(key_str);
182-
ZVAL_UNDEF(value);
183-
}
184-
} ZEND_HASH_FOREACH_END();
179+
switch(Z_TYPE_P(mp.user.retval)) {
180+
case IS_ARRAY:
181+
case IS_OBJECT:
182+
ZEND_HASH_FOREACH_STR_KEY_VAL(HASH_OF(mp.user.retval), key_str, value) {
183+
if (key_str) {
184+
php_set_session_var(key_str, value, NULL);
185+
php_add_session_var(key_str);
186+
ZVAL_UNDEF(value);
187+
}
188+
} ZEND_HASH_FOREACH_END();
189+
break;
190+
}
185191

186192
zval_ptr_dtor(&tmp);
187193
} else {

tests/issue132.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Issue #132 (Segmentation fault when using cloned unpacker)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("msgpack")) {
6+
echo "skip";
7+
}
8+
if (!extension_loaded("session")) {
9+
echo "skip - need ext/session";
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
session_start();
15+
$_SESSION['a'] = 1;
16+
session_write_close();
17+
18+
ini_set('session.serialize_handler', 'msgpack');
19+
session_start();
20+
?>
21+
OK
22+
--EXPECT--
23+
OK

0 commit comments

Comments
 (0)