Refactor memory allocation in hex_next_token and 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:52:14 +0200
2 files changed,
12 insertions(+),
14 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -1565,14 +1565,13 @@ {
return NULL; // End of input } - hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); + hex_token_t *token = (hex_token_t *)calloc(1, sizeof(hex_token_t)); if (!token) + { return NULL; - - token->value = NULL; - token->type = HEX_TOKEN_INVALID; - token->quotation_size = 0; - token->position = (hex_file_position_t *)malloc(sizeof(hex_file_position_t)); + } + token->type = HEX_TOKEN_INVALID; // explicit for clarity + token->position = (hex_file_position_t *)calloc(1, sizeof(hex_file_position_t)); if (!token->position) { free(token);@@ -1799,7 +1798,7 @@ size_t capacity = 2;
size_t size = 0; int balanced = 1; - quotation = (hex_item_t **)malloc(capacity * sizeof(hex_item_t *)); + quotation = (hex_item_t **)calloc(capacity, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[parse quotation] Memory allocation failed");
M
src/parser.c
→
src/parser.c
@@ -31,14 +31,13 @@ {
return NULL; // End of input } - hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); + hex_token_t *token = (hex_token_t *)calloc(1, sizeof(hex_token_t)); if (!token) + { return NULL; - - token->value = NULL; - token->type = HEX_TOKEN_INVALID; - token->quotation_size = 0; - token->position = (hex_file_position_t *)malloc(sizeof(hex_file_position_t)); + } + token->type = HEX_TOKEN_INVALID; // explicit for clarity + token->position = (hex_file_position_t *)calloc(1, sizeof(hex_file_position_t)); if (!token->position) { free(token);@@ -265,7 +264,7 @@ size_t capacity = 2;
size_t size = 0; int balanced = 1; - quotation = (hex_item_t **)malloc(capacity * sizeof(hex_item_t *)); + quotation = (hex_item_t **)calloc(capacity, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[parse quotation] Memory allocation failed");