all repos — hex @ 0c8622cff8de5b10a8d6ca8401da3e8d0e4747d4

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Refactor memory allocation in hex_symbol functions to use calloc for zero-initialization; enhances safety and prevents uninitialized memory usage.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 17:56:21 +0200
commit

0c8622cff8de5b10a8d6ca8401da3e8d0e4747d4

parent

20d6cf101b32165cdf5b16f66dd6f116272db0a1

2 files changed, 42 insertions(+), 38 deletions(-)

jump to
M src/hex.csrc/hex.c

@@ -4049,7 +4049,7 @@

int hex_symbol_symbols(hex_context_t *ctx) { // Allocate memory for the quotation - hex_item_t **quotation = (hex_item_t **)malloc(ctx->registry->size * sizeof(hex_item_t *)); + hex_item_t **quotation = (hex_item_t **)calloc(ctx->registry->size, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol symbols] Memory allocation failed for quotation");

@@ -5392,7 +5392,7 @@ HEX_FREE(ctx, index);

return 1; } int result = 0; - hex_item_t *copy = malloc(sizeof(hex_item_t)); + hex_item_t *copy = calloc(1, sizeof(hex_item_t)); if (list->type == HEX_TYPE_QUOTATION) { if (index->type != HEX_TYPE_INTEGER)

@@ -5581,7 +5581,7 @@ if (strlen(separator->data.str_value) == 0)

{ // Separator is an empty string: split into individual characters size_t size = strlen(str->data.str_value); - hex_item_t **quotation = (hex_item_t **)malloc(size * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(size, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -5591,7 +5591,7 @@ else

{ for (size_t i = 0; i < size; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); if (!quotation[i]) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -5621,7 +5621,7 @@ // Separator is not empty: split as usual

char *token = strtok(str->data.str_value, separator->data.str_value); size_t capacity = 2; size_t size = 0; - hex_item_t **quotation = (hex_item_t **)malloc(capacity * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(capacity, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -5634,7 +5634,9 @@ {

if (size >= capacity) { capacity *= 2; - quotation = (hex_item_t **)realloc(quotation, capacity * sizeof(hex_item_t)); + 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; } + quotation = tmp; if (!quotation) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -5642,7 +5644,7 @@ result = 1;

break; } } - quotation[size] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[size] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[size]->type = HEX_TYPE_STRING; quotation[size]->data.str_value = strdup(token); size++;

@@ -5773,7 +5775,7 @@ {

size_t bytesRead = fread(buffer, 1, length, file); if (hex_is_binary(buffer, bytesRead)) { - hex_item_t **quotation = (hex_item_t **)malloc(bytesRead * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(bytesRead, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol read] Memory allocation failed");

@@ -5783,7 +5785,7 @@ else

{ for (size_t i = 0; i < bytesRead; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[i]->type = HEX_TYPE_INTEGER; quotation[i]->data.int_value = buffer[i]; }

@@ -5800,7 +5802,7 @@ result = 1;

} else { - hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); + hex_item_t *item = (hex_item_t *)calloc(1, sizeof(hex_item_t)); item->type = HEX_TYPE_STRING; item->data.str_value = hex_process_string(str); result = HEX_PUSH(ctx, item);

@@ -5980,7 +5982,7 @@ // Shell symbols

int hex_symbol_args(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc(ctx->argc * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(ctx->argc, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol args] Memory allocation failed");

@@ -5990,7 +5992,7 @@ else

{ for (size_t i = 0; i < (size_t)ctx->argc; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[i]->type = HEX_TYPE_STRING; quotation[i]->data.str_value = ctx->argv[i]; }

@@ -6189,16 +6191,16 @@ }

#endif // Push the return code, output, and error as a quotation - hex_item_t **quotation = (hex_item_t **)malloc(3 * sizeof(hex_item_t)); - quotation[0] = (hex_item_t *)malloc(sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(3, sizeof(hex_item_t *)); + quotation[0] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[0]->type = HEX_TYPE_INTEGER; quotation[0]->data.int_value = return_code; - quotation[1] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[1] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[1]->type = HEX_TYPE_STRING; quotation[1]->data.str_value = strdup(output); - quotation[2] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[2] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[2]->type = HEX_TYPE_STRING; quotation[2]->data.str_value = strdup(error);

@@ -6478,7 +6480,7 @@ return 1;

} result->type = HEX_TYPE_QUOTATION; - result->data.quotation_value = (hex_item_t **)malloc(sizeof(hex_item_t)); + result->data.quotation_value = (hex_item_t **)calloc(1, sizeof(hex_item_t *)); if (!result->data.quotation_value) { hex_error(ctx, "[symbol '] Memory allocation failed");

@@ -6653,7 +6655,7 @@

int hex_symbol_stack(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc((ctx->stack->top + 2) * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc((ctx->stack->top + 2), sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol stack] Memory allocation failed");

@@ -6704,7 +6706,7 @@ int hex_symbol_timestamp(hex_context_t *ctx)

{ static int32_t timestamp[2]; get_unix_timestamp_sec_usec(timestamp); - hex_item_t **quotation = (hex_item_t **)malloc(2 * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(2, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol timestamp] Memory allocation failed");
M src/symbols.csrc/symbols.c

@@ -122,7 +122,7 @@

int hex_symbol_symbols(hex_context_t *ctx) { // Allocate memory for the quotation - hex_item_t **quotation = (hex_item_t **)malloc(ctx->registry->size * sizeof(hex_item_t *)); + hex_item_t **quotation = (hex_item_t **)calloc(ctx->registry->size, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol symbols] Memory allocation failed for quotation");

@@ -1465,7 +1465,7 @@ HEX_FREE(ctx, index);

return 1; } int result = 0; - hex_item_t *copy = malloc(sizeof(hex_item_t)); + hex_item_t *copy = calloc(1, sizeof(hex_item_t)); if (list->type == HEX_TYPE_QUOTATION) { if (index->type != HEX_TYPE_INTEGER)

@@ -1654,7 +1654,7 @@ if (strlen(separator->data.str_value) == 0)

{ // Separator is an empty string: split into individual characters size_t size = strlen(str->data.str_value); - hex_item_t **quotation = (hex_item_t **)malloc(size * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(size, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -1664,7 +1664,7 @@ else

{ for (size_t i = 0; i < size; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); if (!quotation[i]) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -1694,7 +1694,7 @@ // Separator is not empty: split as usual

char *token = strtok(str->data.str_value, separator->data.str_value); size_t capacity = 2; size_t size = 0; - hex_item_t **quotation = (hex_item_t **)malloc(capacity * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(capacity, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -1707,7 +1707,9 @@ {

if (size >= capacity) { capacity *= 2; - quotation = (hex_item_t **)realloc(quotation, capacity * sizeof(hex_item_t)); + 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; } + quotation = tmp; if (!quotation) { hex_error(ctx, "[symbol split] Memory allocation failed");

@@ -1715,7 +1717,7 @@ result = 1;

break; } } - quotation[size] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[size] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[size]->type = HEX_TYPE_STRING; quotation[size]->data.str_value = strdup(token); size++;

@@ -1846,7 +1848,7 @@ {

size_t bytesRead = fread(buffer, 1, length, file); if (hex_is_binary(buffer, bytesRead)) { - hex_item_t **quotation = (hex_item_t **)malloc(bytesRead * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(bytesRead, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol read] Memory allocation failed");

@@ -1856,7 +1858,7 @@ else

{ for (size_t i = 0; i < bytesRead; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[i]->type = HEX_TYPE_INTEGER; quotation[i]->data.int_value = buffer[i]; }

@@ -1873,7 +1875,7 @@ result = 1;

} else { - hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); + hex_item_t *item = (hex_item_t *)calloc(1, sizeof(hex_item_t)); item->type = HEX_TYPE_STRING; item->data.str_value = hex_process_string(str); result = HEX_PUSH(ctx, item);

@@ -2053,7 +2055,7 @@ // Shell symbols

int hex_symbol_args(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc(ctx->argc * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(ctx->argc, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol args] Memory allocation failed");

@@ -2063,7 +2065,7 @@ else

{ for (size_t i = 0; i < (size_t)ctx->argc; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[i]->type = HEX_TYPE_STRING; quotation[i]->data.str_value = ctx->argv[i]; }

@@ -2262,16 +2264,16 @@ }

#endif // Push the return code, output, and error as a quotation - hex_item_t **quotation = (hex_item_t **)malloc(3 * sizeof(hex_item_t)); - quotation[0] = (hex_item_t *)malloc(sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(3, sizeof(hex_item_t *)); + quotation[0] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[0]->type = HEX_TYPE_INTEGER; quotation[0]->data.int_value = return_code; - quotation[1] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[1] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[1]->type = HEX_TYPE_STRING; quotation[1]->data.str_value = strdup(output); - quotation[2] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[2] = (hex_item_t *)calloc(1, sizeof(hex_item_t)); quotation[2]->type = HEX_TYPE_STRING; quotation[2]->data.str_value = strdup(error);

@@ -2551,7 +2553,7 @@ return 1;

} result->type = HEX_TYPE_QUOTATION; - result->data.quotation_value = (hex_item_t **)malloc(sizeof(hex_item_t)); + result->data.quotation_value = (hex_item_t **)calloc(1, sizeof(hex_item_t *)); if (!result->data.quotation_value) { hex_error(ctx, "[symbol '] Memory allocation failed");

@@ -2726,7 +2728,7 @@

int hex_symbol_stack(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc((ctx->stack->top + 2) * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc((ctx->stack->top + 2), sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol stack] Memory allocation failed");

@@ -2777,7 +2779,7 @@ int hex_symbol_timestamp(hex_context_t *ctx)

{ static int32_t timestamp[2]; get_unix_timestamp_sec_usec(timestamp); - hex_item_t **quotation = (hex_item_t **)malloc(2 * sizeof(hex_item_t)); + hex_item_t **quotation = (hex_item_t **)calloc(2, sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol timestamp] Memory allocation failed");