Skip to content

Commit

Permalink
Fix memory leaks and handler initialization in PHP_RINIT
Browse files Browse the repository at this point in the history
Refactored PHP_RINIT_FUNCTION to ensure proper initialization of the execution handler and error handling. Added checks to prevent memory leaks in case of hash table initialization failure and correctly set the execute handler when method intercepting is enabled.
  • Loading branch information
koriym committed Nov 5, 2024
1 parent fff2983 commit 3602632
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions rayaop.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,37 @@ PHP_MSHUTDOWN_FUNCTION(rayaop) {
return SUCCESS;
}

/* Request initialization function fix */
PHP_RINIT_FUNCTION(rayaop) {
// Checking and saving handler initialization
if (!php_rayaop_original_execute_ex) {
php_rayaop_original_execute_ex = zend_execute_ex;
}

RAYAOP_G_LOCK();
if (!RAYAOP_G(intercept_ht)) {
ALLOC_HASHTABLE(RAYAOP_G(intercept_ht));
if (!RAYAOP_G(intercept_ht)) {
RAYAOP_G_UNLOCK();
php_rayaop_handle_error(RAYAOP_E_MEMORY_ALLOCATION, "Failed to allocate intercept hash table");
return FAILURE;
}
// Fix to prevent memory leaks when zend_hash_init fails
if (zend_hash_init(RAYAOP_G(intercept_ht), 8, NULL, (dtor_func_t)php_rayaop_free_intercept_info, 0) == FAILURE) {
FREE_HASHTABLE(RAYAOP_G(intercept_ht));
RAYAOP_G(intercept_ht) = NULL;
RAYAOP_G_UNLOCK();
php_rayaop_handle_error(RAYAOP_E_MEMORY_ALLOCATION, "Failed to initialize intercept hash table");
return FAILURE;
}
zend_hash_init(RAYAOP_G(intercept_ht), 8, NULL, (dtor_func_t)php_rayaop_free_intercept_info, 0);
}
RAYAOP_G(is_intercepting) = 0;
RAYAOP_G(execution_depth) = 0;

// Setting the execute handler
if (RAYAOP_G(method_intercept_enabled)) {
zend_execute_ex = rayaop_execute_ex;
}

RAYAOP_G_UNLOCK();
return SUCCESS;
}
Expand Down

0 comments on commit 3602632

Please sign in to comment.