From 985072a63b72332b1a012a5d686771da93342349 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Wed, 28 Jan 2026 10:52:36 +0000 Subject: fix: prompt segfault and strict string eq --- src/codegen/codegen.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/codegen/codegen.c') diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c index b090546..a66f179 100644 --- a/src/codegen/codegen.c +++ b/src/codegen/codegen.c @@ -240,6 +240,7 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) strcmp(t1, "const char*") == 0)) { // Check if comparing to NULL - don't use strcmp for NULL comparisons + char *t2 = infer_type(ctx, node->binary.right); int is_null_compare = 0; if (node->binary.right->type == NODE_EXPR_VAR && strcmp(node->binary.right->var_ref.name, "NULL") == 0) @@ -252,16 +253,8 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) is_null_compare = 1; } - if (is_null_compare) - { - // Direct pointer comparison for NULL - fprintf(out, "("); - codegen_expression(ctx, node->binary.left, out); - fprintf(out, " %s ", node->binary.op); - codegen_expression(ctx, node->binary.right, out); - fprintf(out, ")"); - } - else + if (!is_null_compare && strcmp(t1, "string") == 0 && t2 && + strcmp(t2, "string") == 0) { fprintf(out, "(strcmp("); codegen_expression(ctx, node->binary.left, out); @@ -276,6 +269,19 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) fprintf(out, ") != 0)"); } } + else + { + // Direct pointer comparison + fprintf(out, "("); + codegen_expression(ctx, node->binary.left, out); + fprintf(out, " %s ", node->binary.op); + codegen_expression(ctx, node->binary.right, out); + fprintf(out, ")"); + } + if (t2) + { + free(t2); + } } else { -- cgit v1.2.3