Skip to content

Commit 7bdfe4b

Browse files
committed
Cleaning up memory leaks in msgpack_convert.c
1 parent 94ad5fd commit 7bdfe4b

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

msgpack.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ PS_SERIALIZER_DECODE_FUNC(msgpack)
180180
if (key_str) {
181181
php_set_session_var(key_str, value, NULL);
182182
php_add_session_var(key_str);
183-
} else {
184-
//unhandled non-string key
185183
}
186184
} ZEND_HASH_FOREACH_END();
187185
zval_ptr_dtor(&tmp);

msgpack_class.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static ZEND_METHOD(msgpack, unpack)
251251
MSGPACK_G(php_only) = base->php_only;
252252

253253
if (object == NULL) {
254-
php_msgpack_unserialize(return_value, str->val, str->len TSRMLS_CC);
254+
php_msgpack_unserialize(return_value, str->val, str->len);
255255
} else {
256256
zval zv, *zv_p;
257257
zv_p = &zv;

msgpack_convert.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval **value)
128128
zend_string *key;
129129
int key_type;
130130
ulong key_index;
131-
zval *data, *arydata;
131+
zval *data, *arydata, nv, *nv_p;
132132
HashPosition pos, valpos;
133133
HashTable *ht, *htval;
134134

@@ -188,22 +188,24 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval **value)
188188
zval_ptr_dtor(*value);
189189
return FAILURE;
190190
}
191+
nv = *dataval;
192+
nv_p = &nv;
193+
zval_copy_ctor(&nv);
191194

192195
if (convert_function) {
193196
zval rv;
194-
if (convert_function(&rv, data, &dataval) != SUCCESS) {
195-
//zval_ptr_dtor(&val);
197+
if (convert_function(&rv, data, &nv_p) != SUCCESS) {
198+
zval_ptr_dtor(*value);
196199
return FAILURE;
197200
}
198201
add_assoc_zval_ex(return_value, key->val, key->len, &rv);
199202
} else {
200-
add_assoc_zval_ex(return_value, key->val, key->len, dataval);
203+
add_assoc_zval_ex(return_value, key->val, key->len, &nv);
201204
}
202205
}
203206
}
204207

205-
//zval_ptr_dtor(*value);
206-
208+
zval_ptr_dtor(*value);
207209
return SUCCESS;
208210
} else {
209211
/* index */
@@ -264,12 +266,15 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval **value)
264266
MSGPACK_WARNING( "[msgpack] (%s) can't get next data in indexed array", __FUNCTION__);
265267
continue;
266268
}
269+
nv = *arydata;
270+
nv_p = &nv;
271+
zval_copy_ctor(&nv);
267272

268273
switch (key_type) {
269274
case HASH_KEY_IS_LONG: {
270275
zval rv;
271276
if (convert_function) {
272-
if (convert_function(&rv, data, &arydata) != SUCCESS) {
277+
if (convert_function(&rv, data, &nv_p) != SUCCESS) {
273278
MSGPACK_WARNING(
274279
"[msgpack] (%s) "
275280
"convert failure in HASH_KEY_IS_LONG "
@@ -280,7 +285,7 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval **value)
280285
}
281286
add_next_index_zval(return_value, &rv);
282287
} else {
283-
add_next_index_zval(return_value, arydata);
288+
add_next_index_zval(return_value, &nv);
284289
}
285290
break;
286291
}
@@ -295,7 +300,7 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval **value)
295300
}
296301
}
297302

298-
//zval_ptr_dtor(*value);
303+
zval_ptr_dtor(*value);
299304
return SUCCESS;
300305
}
301306

@@ -307,10 +312,14 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval **value)
307312

308313
int msgpack_convert_object(zval *return_value, zval *tpl, zval **value) {
309314
zend_class_entry *ce;
315+
zend_string *tpl_zstring;
310316

311317
switch (Z_TYPE_P(tpl)) {
312318
case IS_STRING:
313-
if ((ce = zend_lookup_class(zval_get_string(tpl))) == NULL) {
319+
tpl_zstring = zval_get_string(tpl);
320+
ce = zend_lookup_class(tpl_zstring);
321+
zend_string_release(tpl_zstring);
322+
if (ce == NULL) {
314323
MSGPACK_ERROR("[msgpack] (%s) Class '%s' not found",
315324
__FUNCTION__, Z_STRVAL_P(tpl));
316325
return FAILURE;
@@ -475,7 +484,7 @@ int msgpack_convert_object(zval *return_value, zval *tpl, zval **value) {
475484
num_key++;
476485
} ZEND_HASH_FOREACH_END();
477486
}
478-
//zval_ptr_dtor(*value);
487+
zval_ptr_dtor(*value);
479488
break;
480489
}
481490
default:
@@ -490,7 +499,7 @@ int msgpack_convert_object(zval *return_value, zval *tpl, zval **value) {
490499
MSGPACK_WARNING("[msgpack] (%s) illegal offset type, skip this decoding",
491500
__FUNCTION__);
492501
}
493-
break;
502+
zval_ptr_dtor(*value);
494503
}
495504
}
496505

msgpack_unpack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ int msgpack_unserialize_map_item(
546546

547547
zend_string_release(key_zstring);
548548
zval_ptr_dtor(key);
549+
zval_ptr_dtor(val);
549550
} else {
550551
switch (Z_TYPE_P(key)) {
551552
case IS_LONG:

0 commit comments

Comments
 (0)