Skip to content

Commit

Permalink
Merge pull request #860 from sjinks/issue-858
Browse files Browse the repository at this point in the history
Fix memory leaks and memory corruption in cache backends
  • Loading branch information
Phalcon committed Jul 17, 2013
2 parents 0461f9c + 55fde67 commit d551b09
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
37 changes: 18 additions & 19 deletions ext/cache/backend/apc.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, save){

if (!lifetime) {
PHALCON_INIT_VAR(lifetime);
} else {
PHALCON_SEPARATE_PARAM(lifetime);
}

if (!stop_buffer) {
Expand Down Expand Up @@ -192,14 +190,12 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, save){
* Take the lifetime from the frontend or read it from the set in start()
*/
if (Z_TYPE_P(lifetime) == IS_NULL) {
PHALCON_OBS_NVAR(lifetime);
phalcon_read_property_this(&lifetime, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
if (Z_TYPE_P(lifetime) == IS_NULL) {
PHALCON_INIT_VAR(ttl);

PHALCON_OBS_VAR(ttl);
phalcon_read_property_this(&ttl, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
if (Z_TYPE_P(ttl) == IS_NULL) {
PHALCON_INIT_NVAR(ttl);
phalcon_call_method(ttl, frontend, "getlifetime");
} else {
PHALCON_CPY_WRT(ttl, lifetime);
}
} else {
PHALCON_CPY_WRT(ttl, lifetime);
Expand Down Expand Up @@ -234,7 +230,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, save){
*/
PHP_METHOD(Phalcon_Cache_Backend_Apc, delete){

zval *key_name, *prefix, *key, *success;
zval *key_name, *prefix, *key;

PHALCON_MM_GROW();

Expand All @@ -246,9 +242,8 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, delete){
PHALCON_INIT_VAR(key);
PHALCON_CONCAT_SVV(key, "_PHCA", prefix, key_name);

PHALCON_INIT_VAR(success);
phalcon_call_func_p1(success, "apc_delete", key);
RETURN_CCTOR(success);
phalcon_call_func_p1(return_value, "apc_delete", key);
PHALCON_MM_RESTORE();
}

/**
Expand Down Expand Up @@ -276,9 +271,12 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, queryKeys){

phalcon_fetch_params(1, 0, 1, &prefix);

PHALCON_INIT_VAR(prefix_pattern);
if (!prefix) {
PHALCON_INIT_VAR(prefix);
ZVAL_STRING(prefix, "", 1);
ZVAL_STRING(prefix_pattern, "/^_PHCA/", 1);
}
else {
PHALCON_CONCAT_SVS(prefix_pattern, "/^_PHCA", prefix, "/");
}

PHALCON_INIT_VAR(keys);
Expand All @@ -287,8 +285,6 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, queryKeys){
PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "user", 1);

PHALCON_INIT_VAR(prefix_pattern);
PHALCON_CONCAT_SVS(prefix_pattern, "/^_PHCA", prefix, "/");
ce0 = zend_fetch_class(SL("APCIterator"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

PHALCON_INIT_VAR(iterator);
Expand Down Expand Up @@ -327,21 +323,24 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, queryKeys){
* Remove the _PHCA prefix.
*/
ZVAL_STRINGL(key, str_key + 5, str_key_len - 5 - 1, 1);
efree(str_key);

phalcon_array_append(&keys, key, PH_SEPARATE);
phalcon_array_append(&keys, key, PH_COPY);
}
#else
PHALCON_INIT_NVAR(itkey);
it->funcs->get_current_key(it, itkey TSRMLS_CC);
if (likely(Z_TYPE_P(itkey) == IS_STRING)) {
ZVAL_STRINGL(key, Z_STRVAL_P(itkey) + 5, Z_STRLEN_P(itkey) - 5, 1);
phalcon_array_append(&keys, key, PH_SEPARATE);
phalcon_array_append(&keys, key, PH_COPY);
}
#endif

it->funcs->move_forward(it TSRMLS_CC);
}

it->funcs->dtor(it TSRMLS_CC);

RETURN_CTOR(keys);
}

Expand Down
6 changes: 3 additions & 3 deletions ext/cache/backend/memcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Memcache, __construct){
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect){

zval *options, *memcache, *host, *port, *persistent;
zval *success = NULL;
zval *success;
zend_class_entry *ce0;

PHALCON_MM_GROW();
Expand All @@ -160,11 +160,11 @@ PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect){

PHALCON_OBS_VAR(persistent);
phalcon_array_fetch_string(&persistent, options, SL("persistent"), PH_NOISY);

PHALCON_INIT_VAR(success);
if (zend_is_true(persistent)) {
PHALCON_INIT_VAR(success);
phalcon_call_method_p2(success, memcache, "pconnect", host, port);
} else {
PHALCON_INIT_NVAR(success);
phalcon_call_method_p2(success, memcache, "connect", host, port);
}

Expand Down
26 changes: 11 additions & 15 deletions ext/cache/backend/xcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ PHP_METHOD(Phalcon_Cache_Backend_Xcache, save){

if (!lifetime) {
PHALCON_INIT_VAR(lifetime);
} else {
PHALCON_SEPARATE_PARAM(lifetime);
}

if (!stop_buffer) {
Expand Down Expand Up @@ -222,13 +220,11 @@ PHP_METHOD(Phalcon_Cache_Backend_Xcache, save){
*/
if (Z_TYPE_P(lifetime) == IS_NULL) {

PHALCON_OBS_NVAR(lifetime);
phalcon_read_property_this(&lifetime, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
if (Z_TYPE_P(lifetime) == IS_NULL) {
PHALCON_INIT_VAR(ttl);
PHALCON_OBS_NVAR(ttl);
phalcon_read_property_this(&ttl, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
if (Z_TYPE_P(ttl) == IS_NULL) {
PHALCON_INIT_NVAR(ttl);
phalcon_call_method(ttl, frontend, "getlifetime");
} else {
PHALCON_CPY_WRT(ttl, lifetime);
}
} else {
PHALCON_CPY_WRT(ttl, lifetime);
Expand Down Expand Up @@ -350,14 +346,14 @@ PHP_METHOD(Phalcon_Cache_Backend_Xcache, queryKeys){
PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 1, &prefix);


PHALCON_INIT_VAR(prefixed);
if (!prefix) {
PHALCON_INIT_VAR(prefix);
ZVAL_STRING(prefix, "", 1);
ZVAL_STRING(prefixed, "_PHCX", 1);
}
else {
PHALCON_CONCAT_SV(prefixed, "_PHCX", prefix);
}

PHALCON_INIT_VAR(prefixed);
PHALCON_CONCAT_SV(prefixed, "_PHCX", prefix);

PHALCON_OBS_VAR(options);
phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC);
Expand All @@ -383,7 +379,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Xcache, queryKeys){
PHALCON_GET_HKEY(key, ah0, hp0);
PHALCON_GET_HVALUE(ttl);

if (!phalcon_memnstr(key, prefix)) {
if (!phalcon_memnstr(key, prefixed)) {
zend_hash_move_forward_ex(ah0, &hp0);
continue;
}
Expand Down

0 comments on commit d551b09

Please sign in to comment.