all repos — hex @ b4de8aabfb2174b4bad4d1d0f563a2c5b89995ff

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

Refactor hex_get_symbol to use hex_copy_item_into for improved memory management; enhances safety by preventing aliasing and ensuring proper ownership transfer.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 17:48:02 +0200
commit

b4de8aabfb2174b4bad4d1d0f563a2c5b89995ff

parent

a48690e3a68a3dff333dce00fe5d4169edcfc086

2 files changed, 28 insertions(+), 10 deletions(-)

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

@@ -984,6 +984,19 @@ ////////////////////////////////////////

// Registry Implementation // //////////////////////////////////////// +// Helper: deep-copy an item into an existing struct (wrapper move) +static int hex_copy_item_into(hex_context_t *ctx, const hex_item_t *src, hex_item_t *dst) +{ + hex_item_t *tmp = hex_copy_item(ctx, src); + if (!tmp) + { + return 0; + } + *dst = *tmp; // move struct fields + free(tmp); // free only the wrapper + return 1; +} + static size_t hash_function(const char *key, size_t bucket_count) { size_t hash = 5381;

@@ -1216,15 +1229,11 @@ while (entry != NULL)

{ if (strcmp(entry->key, key) == 0) { - // Deep copy registry value into caller-provided storage (no aliasing) - hex_item_t *copy = hex_copy_item(ctx, entry->value); - if (!copy) + if (!hex_copy_item_into(ctx, entry->value, result)) { hex_error(ctx, "[get symbol] Failed to copy item for key: %s", key); return 0; } - *result = *copy; // Move struct contents - free(copy); // Free wrapper; inner allocations now owned by result hex_debug_item(ctx, "DONE", result); return 1; }
M src/registry.csrc/registry.c

@@ -6,6 +6,19 @@ ////////////////////////////////////////

// Registry Implementation // //////////////////////////////////////// +// Helper: deep-copy an item into an existing struct (wrapper move) +static int hex_copy_item_into(hex_context_t *ctx, const hex_item_t *src, hex_item_t *dst) +{ + hex_item_t *tmp = hex_copy_item(ctx, src); + if (!tmp) + { + return 0; + } + *dst = *tmp; // move struct fields + free(tmp); // free only the wrapper + return 1; +} + static size_t hash_function(const char *key, size_t bucket_count) { size_t hash = 5381;

@@ -238,15 +251,11 @@ while (entry != NULL)

{ if (strcmp(entry->key, key) == 0) { - // Deep copy registry value into caller-provided storage (no aliasing) - hex_item_t *copy = hex_copy_item(ctx, entry->value); - if (!copy) + if (!hex_copy_item_into(ctx, entry->value, result)) { hex_error(ctx, "[get symbol] Failed to copy item for key: %s", key); return 0; } - *result = *copy; // Move struct contents - free(copy); // Free wrapper; inner allocations now owned by result hex_debug_item(ctx, "DONE", result); return 1; }