Refactor hex_pop to copy item before returning, ensuring proper ownership and preventing memory issues.
h3rald h3rald@h3rald.com
Thu, 11 Sep 2025 13:37:51 +0200
2 files changed,
5 insertions(+),
2 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -704,10 +704,11 @@ }
return item; } - hex_item_t *item = ctx->stack->entries[ctx->stack->top]; + hex_item_t *orig_item = ctx->stack->entries[ctx->stack->top]; ctx->stack->entries[ctx->stack->top] = NULL; // Clear the stack reference ctx->stack->top--; + hex_item_t *item = hex_copy_item(ctx, orig_item); hex_debug_item(ctx, " POP", item); return item; }
M
src/stack.c
→
src/stack.c
@@ -271,10 +271,12 @@ }
return item; } - hex_item_t *item = ctx->stack->entries[ctx->stack->top]; + hex_item_t *orig_item = ctx->stack->entries[ctx->stack->top]; ctx->stack->entries[ctx->stack->top] = NULL; // Clear the stack reference ctx->stack->top--; + // Copying the item to avoid ownership issues when freeing + hex_item_t *item = hex_copy_item(ctx, orig_item); hex_debug_item(ctx, " POP", item); return item; }