Fixes.
h3rald h3rald@h3rald.com
Fri, 27 Dec 2024 16:26:08 +0100
3 files changed,
11 insertions(+),
17 deletions(-)
M
src/hex.h
→
src/hex.h
@@ -277,7 +277,7 @@ int hex_push_string(hex_context_t *ctx, const char *value);
int hex_push_quotation(hex_context_t *ctx, hex_item_t **quotation, size_t size); int hex_push_symbol(hex_context_t *ctx, hex_token_t *token); hex_item_t *hex_pop(hex_context_t *ctx); -hex_item_t *hex_copy_item(const hex_item_t *item); +hex_item_t *hex_copy_item(hex_context_t *ctx, const hex_item_t *item); // Parser and interpreter hex_token_t *hex_next_token(hex_context_t *ctx, const char **input, hex_file_position_t *position);
M
src/stack.c
→
src/stack.c
@@ -44,7 +44,6 @@ {
if (hex_push(ctx, value->data.quotation_value[i]) != 0) { HEX_FREE(ctx, value); - free(value); hex_debug_item(ctx, "FAIL", item); return 1; }@@ -61,7 +60,6 @@ hex_error(ctx, "[push] Undefined user symbol: %s", item->token->value);
HEX_FREE(ctx, value); result = 1; } - free(value); } else if (item->type == HEX_TYPE_NATIVE_SYMBOL) {@@ -83,7 +81,6 @@ hex_error(ctx, "[push] Undefined native symbol: %s", item->token->value);
HEX_FREE(ctx, value); result = 1; } - free(value); } else {@@ -170,7 +167,6 @@ {
return 1; } int result = HEX_PUSH(ctx, item); - free(item); return result; }@@ -182,7 +178,6 @@ {
return 1; } int result = HEX_PUSH(ctx, item); - free(item); return result; }@@ -194,7 +189,6 @@ {
return 1; } int result = HEX_PUSH(ctx, item); - free(item); return result; }@@ -206,7 +200,6 @@ {
return 1; } int result = HEX_PUSH(ctx, item); - free(item); return result; }@@ -218,14 +211,15 @@ {
hex_error(ctx, "[pop] Insufficient items on the stack"); return NULL; } - hex_debug_item(ctx, " POP", ctx->stack->entries[ctx->stack->top]); hex_item_t *item = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[pop] Failed to allocate memory for item"); return NULL; } - *item = *ctx->stack->entries[ctx->stack->top--]; + *item = *ctx->stack->entries[ctx->stack->top]; + hex_debug_item(ctx, " POP", item); + ctx->stack->top--; return item; }@@ -287,7 +281,7 @@
free(item); // Free the item itself } -hex_item_t *hex_copy_item(const hex_item_t *item) +hex_item_t *hex_copy_item(hex_context_t *ctx, const hex_item_t *item) { hex_item_t *copy = malloc(sizeof(hex_item_t)); if (copy == NULL)@@ -305,7 +299,7 @@ {
copy->data.str_value = strdup(item->data.str_value); // Duplicate the string if (copy->data.str_value == NULL) { - free(copy); + HEX_FREE(ctx, copy); return NULL; } }@@ -322,7 +316,7 @@ return NULL;
} for (size_t i = 0; i < item->quotation_size; i++) { - copy->data.quotation_value[i] = hex_copy_item(item->data.quotation_value[i]); // Deep copy each item + copy->data.quotation_value[i] = hex_copy_item(ctx, item->data.quotation_value[i]); // Deep copy each item if (copy->data.quotation_value[i] == NULL) { hex_free_list(NULL, copy->data.quotation_value, i);
M
src/symbols.c
→
src/symbols.c
@@ -1650,7 +1650,7 @@ result = 1;
} else { - copy = *hex_copy_item(list->data.quotation_value[index->data.int_value]); + copy = *hex_copy_item(ctx, list->data.quotation_value[index->data.int_value]); result = hex_push(ctx, ©); } }@@ -3061,7 +3061,7 @@ HEX_FREE(ctx, list);
hex_free_list(ctx, quotation, i); return 1; } - *quotation[i] = *hex_copy_item(hex_pop(ctx)); + *quotation[i] = *hex_copy_item(ctx, hex_pop(ctx)); } if (hex_push_quotation(ctx, quotation, list->quotation_size) != 0) {@@ -3138,7 +3138,7 @@ {
HEX_FREE(ctx, item); return 1; } - hex_item_t *copy = hex_copy_item(item); + hex_item_t *copy = hex_copy_item(ctx, item); if (!copy) { hex_error(ctx, "[symbol dup] Memory allocation failed");@@ -3166,7 +3166,7 @@ }
int count = 0; for (size_t i = 0; i <= (size_t)ctx->stack->top; i++) { - quotation[i] = hex_copy_item(ctx->stack->entries[i]); + quotation[i] = hex_copy_item(ctx, ctx->stack->entries[i]); if (!quotation[i]) { hex_error(ctx, "[symbol stack] Memory allocation failed");