Refactor memory allocation in hex_parse_quotation to use calloc for zero-initialization; enhances safety and prevents uninitialized memory usage.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 17:50:06 +0200
2 files changed,
12 insertions(+),
14 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -1845,17 +1845,16 @@ else if (token->type == HEX_TOKEN_SYMBOL)
{ if (hex_valid_native_symbol(ctx, token->value)) { - item = malloc(sizeof(hex_item_t)); + item = calloc(1, sizeof(hex_item_t)); if (item) { - item->type = HEX_TYPE_NATIVE_SYMBOL; - hex_item_t *value = malloc(sizeof(hex_item_t)); + hex_item_t *value = calloc(1, sizeof(hex_item_t)); if (hex_get_symbol(ctx, token->value, value)) { - item->token = token; item->type = HEX_TYPE_NATIVE_SYMBOL; item->data.fn_value = value->data.fn_value; - free(value); // Free the temporary value holder + item->token = token; + free(value); // wrapper only } else {@@ -1869,7 +1868,7 @@ }
} else { - item = malloc(sizeof(hex_item_t)); + item = calloc(1, sizeof(hex_item_t)); if (item) { item->type = HEX_TYPE_USER_SYMBOL;@@ -1887,7 +1886,7 @@ }
} else if (token->type == HEX_TOKEN_QUOTATION_START) { - item = malloc(sizeof(hex_item_t)); + item = calloc(1, sizeof(hex_item_t)); if (item) { item->type = HEX_TYPE_QUOTATION;
M
src/parser.c
→
src/parser.c
@@ -311,17 +311,16 @@ else if (token->type == HEX_TOKEN_SYMBOL)
{ if (hex_valid_native_symbol(ctx, token->value)) { - item = malloc(sizeof(hex_item_t)); + item = calloc(1, sizeof(hex_item_t)); if (item) { - item->type = HEX_TYPE_NATIVE_SYMBOL; - hex_item_t *value = malloc(sizeof(hex_item_t)); + hex_item_t *value = calloc(1, sizeof(hex_item_t)); if (hex_get_symbol(ctx, token->value, value)) { - item->token = token; item->type = HEX_TYPE_NATIVE_SYMBOL; item->data.fn_value = value->data.fn_value; - free(value); // Free the temporary value holder + item->token = token; + free(value); // wrapper only } else {@@ -335,7 +334,7 @@ }
} else { - item = malloc(sizeof(hex_item_t)); + item = calloc(1, sizeof(hex_item_t)); if (item) { item->type = HEX_TYPE_USER_SYMBOL;@@ -353,7 +352,7 @@ }
} else if (token->type == HEX_TOKEN_QUOTATION_START) { - item = malloc(sizeof(hex_item_t)); + item = calloc(1, sizeof(hex_item_t)); if (item) { item->type = HEX_TYPE_QUOTATION;