From 3602632c8f3e3cfe6e38a4ba5379fe01ce2bc43b Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 5 Nov 2024 14:48:55 +0900 Subject: [PATCH] Fix memory leaks and handler initialization in PHP_RINIT 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. --- rayaop.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/rayaop.c b/rayaop.c index d684b8d..d71b1fa 100644 --- a/rayaop.c +++ b/rayaop.c @@ -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; }