Refactor memory allocation in hex_symbol_cat and hex_symbol_map to use calloc for zero-initialization; enhances safety and prevents uninitialized memory usage.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 18:00:26 +0200
2 files changed,
4 insertions(+),
4 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -5265,7 +5265,7 @@ // This avoids aliasing between the original quotations and the new one.
if (list->type == HEX_TYPE_QUOTATION && value->type == HEX_TYPE_QUOTATION) { size_t new_size = list->quotation_size + value->quotation_size; - hex_item_t **items = (hex_item_t **)malloc(new_size * sizeof(hex_item_t *)); + hex_item_t **items = (hex_item_t **)calloc(new_size, sizeof(hex_item_t *)); if (!items) { hex_error(ctx, "[symbol cat] Memory allocation failed");@@ -6533,7 +6533,7 @@ }
else { // Allocate result quotation (array of element pointers) - hex_item_t **quotation = (hex_item_t **)malloc(list->quotation_size * sizeof(hex_item_t *)); + hex_item_t **quotation = (hex_item_t **)calloc(list->quotation_size, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol map] Memory allocation failed");
M
src/symbols.c
→
src/symbols.c
@@ -1338,7 +1338,7 @@ // This avoids aliasing between the original quotations and the new one.
if (list->type == HEX_TYPE_QUOTATION && value->type == HEX_TYPE_QUOTATION) { size_t new_size = list->quotation_size + value->quotation_size; - hex_item_t **items = (hex_item_t **)malloc(new_size * sizeof(hex_item_t *)); + hex_item_t **items = (hex_item_t **)calloc(new_size, sizeof(hex_item_t *)); if (!items) { hex_error(ctx, "[symbol cat] Memory allocation failed");@@ -2606,7 +2606,7 @@ }
else { // Allocate result quotation (array of element pointers) - hex_item_t **quotation = (hex_item_t **)malloc(list->quotation_size * sizeof(hex_item_t *)); + hex_item_t **quotation = (hex_item_t **)calloc(list->quotation_size, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol map] Memory allocation failed");