Refactor memory allocation in hex_registry_create and hex_set_symbol to use calloc for zero-initialization; enhances safety and prevents uninitialized memory usage.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 17:59:30 +0200
3 files changed,
16 insertions(+),
6 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -1010,7 +1010,7 @@ }
hex_registry_t *hex_registry_create() { - hex_registry_t *registry = malloc(sizeof(hex_registry_t)); + hex_registry_t *registry = calloc(1, sizeof(hex_registry_t)); if (registry == NULL) { return NULL;@@ -1179,7 +1179,7 @@ entry = entry->next;
} // Add a new entry to the bucket - hex_registry_entry_t *new_entry = malloc(sizeof(hex_registry_entry_t)); + hex_registry_entry_t *new_entry = calloc(1, sizeof(hex_registry_entry_t)); if (new_entry == NULL) { return 1; // Memory allocation failed@@ -5635,7 +5635,12 @@ if (size >= capacity)
{ capacity *= 2; hex_item_t **tmp = (hex_item_t **)realloc(quotation, capacity * sizeof(hex_item_t *)); - if (!tmp) { hex_error(ctx, "[symbol split] Memory allocation failed"); result = 1; break; } + if (!tmp) + { + hex_error(ctx, "[symbol split] Memory allocation failed"); + result = 1; + break; + } quotation = tmp; if (!quotation) {
M
src/registry.c
→
src/registry.c
@@ -32,7 +32,7 @@ }
hex_registry_t *hex_registry_create() { - hex_registry_t *registry = malloc(sizeof(hex_registry_t)); + hex_registry_t *registry = calloc(1, sizeof(hex_registry_t)); if (registry == NULL) { return NULL;@@ -201,7 +201,7 @@ entry = entry->next;
} // Add a new entry to the bucket - hex_registry_entry_t *new_entry = malloc(sizeof(hex_registry_entry_t)); + hex_registry_entry_t *new_entry = calloc(1, sizeof(hex_registry_entry_t)); if (new_entry == NULL) { return 1; // Memory allocation failed
M
src/symbols.c
→
src/symbols.c
@@ -1708,7 +1708,12 @@ if (size >= capacity)
{ capacity *= 2; hex_item_t **tmp = (hex_item_t **)realloc(quotation, capacity * sizeof(hex_item_t *)); - if (!tmp) { hex_error(ctx, "[symbol split] Memory allocation failed"); result = 1; break; } + if (!tmp) + { + hex_error(ctx, "[symbol split] Memory allocation failed"); + result = 1; + break; + } quotation = tmp; if (!quotation) {