Skip to content

Commit

Permalink
Fixed memory leaks in __sleep and __wakeup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Jul 30, 2013
1 parent 25782bc commit 7b5a96b
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 33 deletions.
19 changes: 10 additions & 9 deletions mustache_ast.cpp
Expand Up @@ -146,6 +146,8 @@ static void MustacheAST_obj_free(void *object TSRMLS_DC)
if( payload->node != NULL ) {
delete payload->node;
}

zend_object_std_dtor((zend_object *)object);

efree(object);

Expand Down Expand Up @@ -204,6 +206,8 @@ PHP_MINIT_FUNCTION(mustache_ast)
MustacheAST_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC);
MustacheAST_ce_ptr->create_object = MustacheAST_obj_create;

zend_declare_property_null(MustacheAST_ce_ptr, ZEND_STRL("binaryString"), ZEND_ACC_PROTECTED TSRMLS_CC);

return SUCCESS;
} catch(...) {
mustache_exception_handler(TSRMLS_C);
Expand Down Expand Up @@ -277,16 +281,12 @@ PHP_METHOD(MustacheAST, __sleep)
char * str = NULL;
int len = 0;
mustache_node_to_binary_string(payload->node, &str, &len);
if( str != NULL && len > 0 ) {
add_property_stringl_ex(_this_zval,
"binaryString",
sizeof("binaryString"),
str,
len,
0 TSRMLS_CC);
if( str != NULL ) {
zend_update_property_stringl(MustacheAST_ce_ptr, _this_zval,
ZEND_STRL("binaryString"), str, len TSRMLS_CC);
add_next_index_string(return_value, "binaryString", 1);
efree(str);
}

add_next_index_string(return_value, "binaryString", 1);
}

} catch(...) {
Expand Down Expand Up @@ -385,6 +385,7 @@ PHP_METHOD(MustacheAST, __wakeup)
(php_obj_MustacheAST *) zend_object_store_get_object(_this_zval TSRMLS_CC);

// Get object properties
// @todo should be able to convert this to use zend_hash_find
int key_type = 0;
char * key_str = NULL;
uint key_len = 0;
Expand Down
24 changes: 11 additions & 13 deletions mustache_code.cpp
Expand Up @@ -21,6 +21,8 @@ static void MustacheCode_obj_free(void *object TSRMLS_DC)
if( payload->codes != NULL ) {
free(payload->codes);
}

zend_object_std_dtor((zend_object *)object);

efree(object);

Expand Down Expand Up @@ -126,20 +128,13 @@ PHP_METHOD(MustacheCode, __sleep)
// Check payload
if( payload->codes != NULL ) {
// Serialize and store
char * str = NULL;
int len = payload->length;
str = (char *) estrndup((const char *) payload->codes, payload->length);

if( str != NULL && len > 0 ) {
add_property_stringl_ex(_this_zval,
"binaryString",
sizeof("binaryString"),
str,
len,
0 TSRMLS_CC);
if( payload->codes != NULL ) {
zend_update_property_stringl(MustacheCode_ce_ptr, _this_zval,
ZEND_STRL("binaryString"), (const char *) payload->codes,
payload->length TSRMLS_CC);

add_next_index_string(return_value, "binaryString", 1);
}

add_next_index_string(return_value, "binaryString", 1);
}

} catch(...) {
Expand Down Expand Up @@ -243,6 +238,7 @@ PHP_METHOD(MustacheCode, __wakeup)
(php_obj_MustacheCode *) zend_object_store_get_object(_this_zval TSRMLS_CC);

// Get object properties
// @todo should be able to convert this to use zend_hash_find
int key_type = 0;
char * key_str = NULL;
uint key_len = 0;
Expand Down Expand Up @@ -337,6 +333,8 @@ PHP_MINIT_FUNCTION(mustache_code)
MustacheCode_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC);
MustacheCode_ce_ptr->create_object = MustacheCode_obj_create;

zend_declare_property_null(MustacheCode_ce_ptr, ZEND_STRL("binaryString"), ZEND_ACC_PROTECTED TSRMLS_CC);

return SUCCESS;
} catch(...) {
mustache_exception_handler(TSRMLS_C);
Expand Down
1 change: 1 addition & 0 deletions mustache_mustache.cpp
Expand Up @@ -218,6 +218,7 @@ bool mustache_parse_template_param(zval * tmpl, mustache::Mustache * mustache,
return false;
}
*node = maPayload->node;
return true;
} else {
php_error(E_WARNING, "Object not an instance of MustacheTemplate or MustacheAST");
return false;
Expand Down
17 changes: 10 additions & 7 deletions mustache_template.cpp
Expand Up @@ -59,6 +59,8 @@ static void MustacheTemplate_obj_free(void *object TSRMLS_DC)
if( payload->tmpl != NULL ) {
delete payload->tmpl;
}

zend_object_std_dtor((zend_object *)object);

efree(object);

Expand Down Expand Up @@ -117,6 +119,8 @@ PHP_MINIT_FUNCTION(mustache_template)
MustacheTemplate_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC);
MustacheTemplate_ce_ptr->create_object = MustacheTemplate_obj_create;

zend_declare_property_null(MustacheTemplate_ce_ptr, ZEND_STRL("template"), ZEND_ACC_PROTECTED TSRMLS_CC);

return SUCCESS;
} catch(...) {
mustache_exception_handler(TSRMLS_C);
Expand Down Expand Up @@ -184,13 +188,11 @@ PHP_METHOD(MustacheTemplate, __sleep)

if( payload->tmpl != NULL ) {
// Store
add_property_stringl_ex(_this_zval,
"template",
sizeof("template"),
(char *) payload->tmpl->c_str(),
payload->tmpl->length(),
1 TSRMLS_CC);

zend_update_property_stringl(MustacheTemplate_ce_ptr, _this_zval,
ZEND_STRL("template"),
(char *) payload->tmpl->c_str(),
payload->tmpl->length() TSRMLS_CC);

add_next_index_string(return_value, "template", 1);
}

Expand Down Expand Up @@ -252,6 +254,7 @@ PHP_METHOD(MustacheTemplate, __wakeup)
(php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);

// Get object properties
// @todo should be able to convert this to use zend_hash_find
int key_type = 0;
char * key_str = NULL;
uint key_len = 0;
Expand Down
Binary file modified tests/MustacheAST____sleep.phpt
Binary file not shown.
Binary file modified tests/MustacheAST____wakeup.phpt
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/MustacheCode____sleep.phpt
Expand Up @@ -11,4 +11,4 @@ $serial = serialize($tmpl);
var_dump(addslashes($serial));
?>
--EXPECT--
string(97) "O:12:\"MustacheCode\":1:{s:12:\"binaryString\";s:27:\"\0\0\0\0\0\0\0\0\0\0\0test\0\";}"
string(102) "O:12:\"MustacheCode\":1:{s:15:\"\0*\0binaryString\";s:27:\"\0\0\0\0\0\0\0\0\0\0\0test\0\";}"
2 changes: 0 additions & 2 deletions tests/MustacheCode____wakeup.phpt
Expand Up @@ -8,9 +8,7 @@ if( !extension_loaded('mustache') ) die('skip ');
<?php
$tmpl = new MustacheCode(pack("H*" , "000000020000000c00000014020011011810080003017465737400"));
$serial = unserialize(serialize($tmpl));
var_dump(bin2hex($serial->binaryString));
var_dump(bin2hex((string) $serial));
?>
--EXPECT--
string(54) "000000020000000c00000014020011011810080003017465737400"
string(54) "000000020000000c00000014020011011810080003017465737400"
Binary file modified tests/MustacheTemplate____sleep.phpt
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/MustacheTemplate____wakeup.phpt
Expand Up @@ -15,7 +15,7 @@ var_dump($m->render($tmpl, array('test' => 'baz')));
?>
--EXPECT--
object(MustacheTemplate)#3 (1) {
["template"]=>
["template":protected]=>
string(8) "{{test}}"
}
string(8) "{{test}}"
Expand Down
2 changes: 2 additions & 0 deletions tests/Mustache__compile-accepts-MustacheAST.phpt
Expand Up @@ -9,9 +9,11 @@ if( !extension_loaded('mustache') ) die('skip ');
$m = new Mustache();
$ast = $m->parse('{{test}}');
$tmpl = $m->compile($ast);
var_dump(get_class($ast));
var_dump(get_class($tmpl));
var_dump(bin2hex((string) $tmpl));
?>
--EXPECT--
string(11) "MustacheAST"
string(12) "MustacheCode"
string(54) "000000020000000c00000014020011011810080003017465737400"

0 comments on commit 7b5a96b

Please sign in to comment.