Skip to content

Commit

Permalink
Merge pull request #33 from alex-vlasov/BR-4992
Browse files Browse the repository at this point in the history
BR-4992: Shadow memory leaks on PHP 7
  • Loading branch information
yellowandy authored Apr 18, 2017
2 parents 327520b + 8901ef9 commit 49575e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ const zend_function_entry shadow_functions[] = {
static PHP_GINIT_FUNCTION(shadow)
{
memset(shadow_globals, 0, sizeof(zend_shadow_globals));
zend_hash_init(&shadow_globals->cache, 10, NULL, NULL, 1); // persistent!
zend_hash_init(&shadow_globals->replaced_function_table, 10, NULL, NULL, 1);
zend_hash_init(&shadow_globals->cache, 10, NULL, ZVAL_PTR_DTOR, 1); // persistent!
zend_hash_init(&shadow_globals->replaced_function_table, 10, NULL, ZVAL_PTR_DTOR, 1);
// initial size 10 here is a common sense - look at the number of overriden functions
}
/* }}} */
Expand All @@ -123,6 +123,8 @@ static PHP_GINIT_FUNCTION(shadow)
*/
static PHP_GSHUTDOWN_FUNCTION(shadow)
{
shadow_cache_clean(TSRMLS_C);
zend_hash_clean(&shadow_globals->replaced_function_table);
zend_hash_destroy(&shadow_globals->replaced_function_table);
zend_hash_destroy(&shadow_globals->cache);
}
Expand Down Expand Up @@ -544,6 +546,9 @@ static int is_instance_only(const char *filename TSRMLS_DC)
}
return result;
}
if (realpath) {
efree(realpath);
}
return 0;
}

Expand Down Expand Up @@ -754,6 +759,7 @@ zend_string *shadow_resolve_path(const char *filename, int filename_len)
// in any case we have to call original resolver because that can be reimplemented by opcache for example
if (shadow_result) {
result = original_zend_resolve_path(shadow_result, strlen(shadow_result));
efree(shadow_result);
} else {
result = original_zend_resolve_path(filename, filename_len);
}
Expand Down Expand Up @@ -1440,6 +1446,7 @@ static void shadow_glob(INTERNAL_FUNCTION_PARAMETERS)
efree(mergepath);
} ZEND_HASH_FOREACH_END();
}
zval_dtor(return_value);
return_value = &templdata;
/* convert mergedata to return */
zend_hash_clean(Z_ARRVAL_P(return_value));
Expand All @@ -1448,6 +1455,7 @@ static void shadow_glob(INTERNAL_FUNCTION_PARAMETERS)
add_next_index_str(return_value, zend_string_copy(filename_zs));
} ZEND_HASH_FOREACH_END();
/* cleanup */
zend_hash_clean(mergedata);
zend_hash_destroy(mergedata);
efree(mergedata);
efree(path);
Expand Down
6 changes: 3 additions & 3 deletions shadow_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ int shadow_cache_get(const char *name, char **entry TSRMLS_DC)
namelen = shadow_cache_segmented_name(&segname, name TSRMLS_CC);
zend_string *segname_zs = zend_string_init(segname, namelen, 0);
if ((centry = zend_hash_find(&SHADOW_G(cache), segname_zs)) != NULL) {
zend_string_release(segname_zs);
efree(segname);
if(Z_STRLEN_P(centry) == 0){
*entry = NULL;
return SUCCESS;
}
*entry = estrdup(Z_STR_P(centry)->val);
efree(segname);
zend_string_release(segname_zs);
return SUCCESS;
} else {
*entry = NULL;
zend_string_release(segname_zs);
efree(segname);
zend_string_release(segname_zs);
return FAILURE;
}
}
Expand Down

0 comments on commit 49575e7

Please sign in to comment.