summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuhaitz <zuhaitz.zechhub@gmail.com>2026-01-28 10:55:19 +0000
committerGitHub <noreply@github.com>2026-01-28 10:55:19 +0000
commit11dbed3dc6e3c4ec154aab302088a2695c2c4052 (patch)
treeaedec24a350d0cb4056742b44367ef33609e2a42
parent279bc13582cb681867bc4ebadb4287449a2c7383 (diff)
parent985072a63b72332b1a012a5d686771da93342349 (diff)
Merge pull request #147 from z-libs/fix/segfault-and-string-refactor
fix: prompt segfault and strict string eq
-rw-r--r--src/codegen/codegen.c26
-rw-r--r--tests/std/test_env.zc2
2 files changed, 17 insertions, 11 deletions
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
{
diff --git a/tests/std/test_env.zc b/tests/std/test_env.zc
index 25d5bc1..4b68712 100644
--- a/tests/std/test_env.zc
+++ b/tests/std/test_env.zc
@@ -30,7 +30,7 @@ test "test_std_env_get_dup" {
assert(env_var.is_some(), "env_var should have a value");
let value = env_var.unwrap();
- assert(value.c_str() == "ok3", "value should be ok3");
+ assert(strcmp(value.c_str(), "ok3") == 0, "value should be ok3");
value.free();