Fixes.
@@ -149,8 +149,11 @@
// Tokenize and process the input hex_interpret(ctx, line, "<repl>", 1, 1); // Print the top item of the stack - hex_print_item(stdout, ctx->stack->entries[ctx->stack->top]); - printf("\n"); + if (ctx->stack->top >= 0) + { + hex_print_item(stdout, ctx->stack->entries[ctx->stack->top]); + printf("\n"); + } return 0; }
@@ -260,7 +260,7 @@ case HEX_TYPE_QUOTATION:
if (item->data.quotation_value) { hex_free_list(ctx, item->data.quotation_value, item->quotation_size); - free(item->data.quotation_value); + // free(item->data.quotation_value); item->data.quotation_value = NULL; // Prevent double free } break;@@ -286,81 +286,71 @@ {
hex_item_t *copy = malloc(sizeof(hex_item_t)); if (copy == NULL) { + hex_error(ctx, "[copy item] Failed to allocate memory for item"); return NULL; } - *copy = *item; // Shallow copy the structure - + copy->type = item->type; switch (item->type) { case HEX_TYPE_STRING: - if (item->data.str_value) + copy->data.str_value = strdup(item->data.str_value); + if (copy->data.str_value == NULL) { - copy->data.str_value = strdup(item->data.str_value); // Duplicate the string - if (copy->data.str_value == NULL) - { - HEX_FREE(ctx, copy); - return NULL; - } + hex_error(ctx, "[copy item] Failed to allocate memory for string"); + free(copy); + return NULL; } break; + case HEX_TYPE_INTEGER: + copy->data.int_value = item->data.int_value; + break; + case HEX_TYPE_QUOTATION: - if (item->data.quotation_value) + copy->quotation_size = item->quotation_size; + copy->data.quotation_value = malloc(copy->quotation_size * sizeof(hex_item_t *)); + if (copy->data.quotation_value == NULL) { - copy->data.quotation_value = malloc(item->quotation_size * sizeof(hex_item_t *)); - if (copy->data.quotation_value == NULL) + hex_error(ctx, "[copy item] Failed to allocate memory for quotation"); + free(copy); + return NULL; + } + for (size_t i = 0; i < copy->quotation_size; i++) + { + copy->data.quotation_value[i] = hex_copy_item(ctx, item->data.quotation_value[i]); + if (copy->data.quotation_value[i] == NULL) { - HEX_FREE(ctx, copy); + hex_free_list(ctx, copy->data.quotation_value, i); + free(copy->data.quotation_value); + free(copy); return NULL; } - for (size_t i = 0; i < item->quotation_size; i++) - { - if (item->data.quotation_value[i] != NULL) - { - 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(ctx, copy->data.quotation_value, i); - HEX_FREE(ctx, copy); - return NULL; - } - } - else - { - copy->data.quotation_value[i] = NULL; - } - } } - copy->quotation_size = item->quotation_size; break; case HEX_TYPE_NATIVE_SYMBOL: case HEX_TYPE_USER_SYMBOL: - if (item->token) + copy->token = malloc(sizeof(hex_token_t)); + if (copy->token == NULL) { - copy->token = malloc(sizeof(hex_token_t)); - if (copy->token == NULL) - { - HEX_FREE(ctx, copy); - return NULL; - } - *copy->token = *item->token; // Shallow copy the token structure - if (item->token->value) - { - copy->token->value = strdup(item->token->value); // Deep copy the token's value - if (copy->token->value == NULL) - { - hex_free_token(copy->token); - HEX_FREE(ctx, copy); - return NULL; - } - } + hex_error(ctx, "[copy item] Failed to allocate memory for token"); + free(copy); + return NULL; + } + copy->token->value = strdup(item->token->value); + if (copy->token->value == NULL) + { + hex_error(ctx, "[copy item] Failed to allocate memory for token value"); + free(copy->token); + free(copy); + return NULL; } break; default: - break; + free(copy); + return NULL; } return copy;
@@ -211,7 +211,7 @@ HEX_FREE(ctx, item);
return 1; } int result = hex_push_string(ctx, hex_type(item->type)); - HEX_FREE(ctx, item); + // HEX_FREE(ctx, item); return result; }@@ -246,7 +246,6 @@ HEX_FREE(ctx, item);
return 1; } } - HEX_FREE(ctx, item); return 0; }@@ -269,7 +268,7 @@ }
if (item->type == HEX_TYPE_STRING) { int result = hex_interpret(ctx, item->data.str_value, "<!>", 1, 1); - HEX_FREE(ctx, item); + // HEX_FREE(ctx, item); return result; } else if (item->type == HEX_TYPE_QUOTATION)@@ -295,7 +294,6 @@ {
bytecode[i] = (uint8_t)item->data.quotation_value[i]->data.int_value; } int result = hex_interpret_bytecode(ctx, bytecode, item->quotation_size); - HEX_FREE(ctx, item); return result; } else@@ -325,7 +323,6 @@ return 1;
} hex_raw_print_item(stdout, *item); printf("\n"); - HEX_FREE(ctx, item); return 0; }@@ -346,7 +343,6 @@ return 1;
} hex_raw_print_item(stderr, *item); printf("\n"); - HEX_FREE(ctx, item); return 0; }@@ -367,7 +363,6 @@ return 1;
} hex_raw_print_item(stdout, *item); fflush(stdout); - HEX_FREE(ctx, item); return 0; }@@ -1635,7 +1630,7 @@ HEX_FREE(ctx, index);
return 1; } int result = 0; - hex_item_t copy; + hex_item_t *copy = malloc(sizeof(hex_item_t)); if (list->type == HEX_TYPE_QUOTATION) { if (index->type != HEX_TYPE_INTEGER)@@ -1650,8 +1645,8 @@ result = 1;
} else { - copy = *hex_copy_item(ctx, list->data.quotation_value[index->data.int_value]); - result = hex_push(ctx, ©); + copy = hex_copy_item(ctx, list->data.quotation_value[index->data.int_value]); + result = hex_push(ctx, copy); } } else if (list->type == HEX_TYPE_STRING)@@ -1669,8 +1664,8 @@ }
else { char str[2] = {list->data.str_value[index->data.int_value], '\0'}; - copy = *hex_string_item(ctx, str); - result = hex_push(ctx, ©); + copy = hex_string_item(ctx, str); + result = hex_push(ctx, copy); } } else@@ -2328,7 +2323,6 @@ HEX_FREE(ctx, item);
return 1; } int exit_status = item->data.int_value; - HEX_FREE(ctx, item); exit(exit_status); return 0; // This line will never be reached, but it's here to satisfy the return type }@@ -2359,7 +2353,6 @@ {
hex_error(ctx, "[symbol exec] String required"); result = 1; } - HEX_FREE(ctx, command); return result; }@@ -2527,8 +2520,6 @@ quotation[2] = (hex_item_t *)malloc(sizeof(hex_item_t));
quotation[2]->type = HEX_TYPE_STRING; quotation[2]->data.str_value = strdup(error); - HEX_FREE(ctx, command); - return hex_push_quotation(ctx, quotation, 3); }@@ -2645,9 +2636,6 @@ }
} HEX_FREE(ctx, evalResult); } - HEX_FREE(ctx, condition); - HEX_FREE(ctx, thenBlock); - HEX_FREE(ctx, elseBlock); return 0; }@@ -2720,7 +2708,6 @@ break;
} } } - HEX_FREE(ctx, evalResult); } if (result != 0) {@@ -2812,8 +2799,6 @@ }
} } - HEX_FREE(ctx, action); - HEX_FREE(ctx, condition); return 0; }@@ -2895,9 +2880,6 @@ }
strncpy(ctx->error, prevError, sizeof(ctx->error)); } - - HEX_FREE(ctx, catch_block); - HEX_FREE(ctx, try_block); return 0; }@@ -2923,7 +2905,6 @@ HEX_FREE(ctx, message);
return 1; } hex_error(ctx, message->data.str_value); - HEX_FREE(ctx, message); return 1; }@@ -3072,8 +3053,6 @@ return 1;
} } - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); return 0; }@@ -3147,7 +3126,6 @@ return 1;
} if (HEX_PUSH(ctx, copy) == 0 && HEX_PUSH(ctx, item) == 0) { - HEX_FREE(ctx, item); return 0; } HEX_FREE(ctx, item);@@ -3194,7 +3172,10 @@ hex_error(ctx, "[symbol pop] Memory allocation failed");
return 1; } HEX_POP(ctx, item); - HEX_FREE(ctx, item); + if (item != NULL) + { + HEX_FREE(ctx, item); + } return 0; }