Skip to content

Commit 8a94d4f

Browse files
committed
Fix memory leaks during expression folding
1 parent d2a963b commit 8a94d4f

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

src/compiler/optimizer.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static Expr* foldBinary(Expr* expr) {
3939
else folded = false;
4040

4141
if (folded) {
42+
free(expr->as.binary.operator);
4243
expr->type = EXPR_LITERAL;
4344
expr->as.literal.value = res;
4445
freeExpr(l);
@@ -60,14 +61,18 @@ static Expr* foldUnary(Expr* expr) {
6061
const char* op = expr->as.unary.operator;
6162

6263
if (strcmp(op, "-") == 0 && IS_NUMBER(rv)) {
64+
free(expr->as.unary.operator);
6365
expr->type = EXPR_LITERAL;
6466
expr->as.literal.value = NUMBER_VAL(-AS_NUMBER(rv));
67+
freeExpr(r);
6568
return expr;
6669
} else if (strcmp(op, "!") == 0) {
6770
// isFalsey logic
6871
bool res = IS_NIL(rv) || (IS_BOOL(rv) && !AS_BOOL(rv));
72+
free(expr->as.unary.operator);
6973
expr->type = EXPR_LITERAL;
7074
expr->as.literal.value = BOOL_VAL(res);
75+
freeExpr(r);
7176
return expr;
7277
}
7378
}

0 commit comments

Comments
 (0)