all repos — hex @ a1f5b97ccce00f4f12cbb65b03601ddcae395858

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

Refactor memory management in symbol functions; ensures proper ownership transfer and prevents memory leaks.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 18:22:32 +0200
commit

a1f5b97ccce00f4f12cbb65b03601ddcae395858

parent

8341f3711a859aee82fff1153942a777f92ca394

2 files changed, 24 insertions(+), 4 deletions(-)

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

@@ -4009,6 +4009,8 @@ HEX_FREE(ctx, name);

HEX_FREE(ctx, value); return 1; } + // Success: registry takes ownership of 'value'. Free name only. + HEX_FREE(ctx, name); return 0; }

@@ -4050,7 +4052,8 @@ HEX_FREE(ctx, name);

HEX_FREE(ctx, value); return 1; } - + // Success: registry now owns 'value'; free only the name item. + HEX_FREE(ctx, name); return 0; }

@@ -4081,6 +4084,8 @@ hex_error(ctx, "[symbol #] Symbol not found: %s", item->data.str_value);

HEX_FREE(ctx, item); return 1; } + // Success: free the popped symbol name item + HEX_FREE(ctx, item); return 0; }

@@ -4243,6 +4248,7 @@ if (item->type == HEX_TYPE_STRING)

{ int result = hex_interpret(ctx, item->data.str_value, file->data.str_value, 1, 1); HEX_FREE(ctx, item); + HEX_FREE(ctx, file); return result; } else if (item->type == HEX_TYPE_QUOTATION)

@@ -4253,6 +4259,7 @@ if (item->data.quotation_value[i]->type != HEX_TYPE_INTEGER)

{ hex_error(ctx, "[symbol !] Quotation must contain only integers"); HEX_FREE(ctx, item); + HEX_FREE(ctx, file); return 1; } }

@@ -4273,7 +4280,10 @@ hex_symbol_table_t *symbol_table_copy = hex_symboltable_copy(ctx);

int result = hex_interpret_bytecode(ctx, bytecode, item->quotation_size, file->data.str_value); // Restore the original symbol table ctx->symbol_table = symbol_table_copy; - return result; + free(bytecode); + HEX_FREE(ctx, item); + HEX_FREE(ctx, file); + return result; // NOTE: potential leak of mutated symbol table not restored (pre-existing design) } else {
M src/symbols.csrc/symbols.c

@@ -44,6 +44,8 @@ HEX_FREE(ctx, name);

HEX_FREE(ctx, value); return 1; } + // Success: registry takes ownership of 'value'. Free name only. + HEX_FREE(ctx, name); return 0; }

@@ -85,7 +87,8 @@ HEX_FREE(ctx, name);

HEX_FREE(ctx, value); return 1; } - + // Success: registry now owns 'value'; free only the name item. + HEX_FREE(ctx, name); return 0; }

@@ -116,6 +119,8 @@ hex_error(ctx, "[symbol #] Symbol not found: %s", item->data.str_value);

HEX_FREE(ctx, item); return 1; } + // Success: free the popped symbol name item + HEX_FREE(ctx, item); return 0; }

@@ -278,6 +283,7 @@ if (item->type == HEX_TYPE_STRING)

{ int result = hex_interpret(ctx, item->data.str_value, file->data.str_value, 1, 1); HEX_FREE(ctx, item); + HEX_FREE(ctx, file); return result; } else if (item->type == HEX_TYPE_QUOTATION)

@@ -288,6 +294,7 @@ if (item->data.quotation_value[i]->type != HEX_TYPE_INTEGER)

{ hex_error(ctx, "[symbol !] Quotation must contain only integers"); HEX_FREE(ctx, item); + HEX_FREE(ctx, file); return 1; } }

@@ -308,7 +315,10 @@ hex_symbol_table_t *symbol_table_copy = hex_symboltable_copy(ctx);

int result = hex_interpret_bytecode(ctx, bytecode, item->quotation_size, file->data.str_value); // Restore the original symbol table ctx->symbol_table = symbol_table_copy; - return result; + free(bytecode); + HEX_FREE(ctx, item); + HEX_FREE(ctx, file); + return result; // NOTE: potential leak of mutated symbol table not restored (pre-existing design) } else {