From 79df3d0c3886a5bf2b0bed27b0d6159630611c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 6 Jan 2023 20:25:33 +0100 Subject: [PATCH 1/2] random: Rely on `free(NULL)` being safe for random status freeing --- ext/random/random.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index e35b3bbba47e..0aba1b30245e 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -234,7 +234,7 @@ static zend_object *php_random_randomizer_new(zend_class_entry *ce) static void randomizer_free_obj(zend_object *object) { php_random_randomizer *randomizer = php_random_randomizer_from_obj(object); - if (randomizer->is_userland_algo && randomizer->status) { + if (randomizer->is_userland_algo) { php_random_status_free(randomizer->status, false); } @@ -261,9 +261,11 @@ PHPAPI php_random_status *php_random_status_copy(const php_random_algo *algo, ph PHPAPI void php_random_status_free(php_random_status *status, const bool persistent) { - if (status->state) { - pefree(status->state, persistent); + if (status == NULL) { + return; } + + pefree(status->state, persistent); pefree(status, persistent); } @@ -285,10 +287,7 @@ PHPAPI void php_random_engine_common_free_object(zend_object *object) { php_random_engine *engine = php_random_engine_from_obj(object); - if (engine->status) { - php_random_status_free(engine->status, false); - } - + php_random_status_free(engine->status, false); zend_object_std_dtor(object); } From 54ff5a4308bd9e5cd203d86cc5f215a5c2528ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 7 Jan 2023 14:09:34 +0100 Subject: [PATCH 2/2] random: Restructure `php_random_status_free()` to not early-return --- ext/random/random.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index 0aba1b30245e..d4e21beba6d5 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -261,11 +261,10 @@ PHPAPI php_random_status *php_random_status_copy(const php_random_algo *algo, ph PHPAPI void php_random_status_free(php_random_status *status, const bool persistent) { - if (status == NULL) { - return; + if (status != NULL) { + pefree(status->state, persistent); } - pefree(status->state, persistent); pefree(status, persistent); }