From c8d05935d4311d818f75814f0b9dd3905c23f29b Mon Sep 17 00:00:00 2001 From: CiroZDP Date: Sat, 27 Dec 2025 19:07:40 +0100 Subject: [PATCH 1/2] refactor: Refactor function `_glfw_calloc` A simple refactor for the `_glfw_calloc` function. It does not modify the original function behavior, it just makes it more human readable and reduces the indentation. Also, I created a constant called `total_size`, so the program doesn't need to multiply the same two variables three times. --- src/init.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/init.c b/src/init.c index dbd5a900c..fd18da517 100644 --- a/src/init.c +++ b/src/init.c @@ -248,27 +248,24 @@ int _glfw_max(int a, int b) void* _glfw_calloc(size_t count, size_t size) { - if (count && size) - { - void* block; - - if (count > SIZE_MAX / size) - { - _glfwInputError(GLFW_INVALID_VALUE, "Allocation size overflow"); - return NULL; - } - - block = _glfw.allocator.allocate(count * size, _glfw.allocator.user); - if (block) - return memset(block, 0, count * size); - else - { - _glfwInputError(GLFW_OUT_OF_MEMORY, NULL); - return NULL; - } - } - else + if (!count || !size) return NULL; + + const size_t total_size = count * size; + if (total_size > SIZE_MAX) + { + _glfwInputError(GLFW_INVALID_VALUE, "Allocation size overflow"); + return NULL; + } + + void* block = _glfw.allocator.allocate(total_size, _glfw.allocator.user); + if (!block) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, NULL); + return NULL; + } + + return memset(block, 0, total_size); } void* _glfw_realloc(void* block, size_t size) From 0c70f22410baec0ace3c900871a969bdbb0d6ff1 Mon Sep 17 00:00:00 2001 From: CiroZDP Date: Mon, 29 Dec 2025 22:32:24 +0100 Subject: [PATCH 2/2] Fix allocation size overflow check --- src/init.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/init.c b/src/init.c index fd18da517..560524483 100644 --- a/src/init.c +++ b/src/init.c @@ -251,13 +251,13 @@ void* _glfw_calloc(size_t count, size_t size) if (!count || !size) return NULL; - const size_t total_size = count * size; - if (total_size > SIZE_MAX) + if (count > SIZE_MAX / size) { _glfwInputError(GLFW_INVALID_VALUE, "Allocation size overflow"); return NULL; } + const size_t total_size = count * size; void* block = _glfw.allocator.allocate(total_size, _glfw.allocator.user); if (!block) {