Refactor hex_symbol_item and hex_push_symbol to improve token ownership management; now copies token to ensure proper ownership and prevents memory issues.
h3rald h3rald@h3rald.com
Thu, 11 Sep 2025 10:06:16 +0200
2 files changed,
38 insertions(+),
2 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -648,7 +648,16 @@ hex_error(ctx, "[create symbol] Failed to allocate memory for item");
return NULL; } item->type = hex_valid_native_symbol(ctx, token->value) ? HEX_TYPE_NATIVE_SYMBOL : HEX_TYPE_USER_SYMBOL; - item->token = token; + + // Make a copy of the token for this item to ensure clear ownership + item->token = hex_copy_token(ctx, token); + if (item->token == NULL) + { + hex_error(ctx, "[create symbol] Failed to copy token"); + free(item); + return NULL; + } + item->is_operator = 0; item->quotation_size = 0; return item;@@ -700,6 +709,15 @@ {
return 1; } int result = HEX_PUSH(ctx, item); + + // Since hex_symbol_item now copies the token, we should free the original + // token here to maintain clear ownership + if (result != 0) + { + // If push failed, we need to clean up the item we created + hex_free_item(ctx, item); + } + return result; }
M
src/stack.c
→
src/stack.c
@@ -197,7 +197,16 @@ hex_error(ctx, "[create symbol] Failed to allocate memory for item");
return NULL; } item->type = hex_valid_native_symbol(ctx, token->value) ? HEX_TYPE_NATIVE_SYMBOL : HEX_TYPE_USER_SYMBOL; - item->token = token; + + // Make a copy of the token for this item to ensure clear ownership + item->token = hex_copy_token(ctx, token); + if (item->token == NULL) + { + hex_error(ctx, "[create symbol] Failed to copy token"); + free(item); + return NULL; + } + item->is_operator = 0; item->quotation_size = 0; return item;@@ -249,6 +258,15 @@ {
return 1; } int result = HEX_PUSH(ctx, item); + + // Since hex_symbol_item now copies the token, we should free the original + // token here to maintain clear ownership + if (result != 0) + { + // If push failed, we need to clean up the item we created + hex_free_item(ctx, item); + } + return result; }