From 157e64e0d8d9a6124a689ff24fddae7c0bbbaf4e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 9 Jun 2024 19:13:26 +0800 Subject: [PATCH] fix a memory leak --- src/compiler/compiler.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 803690b6..6f24fada 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -317,15 +317,15 @@ Error* Compiler::exprTernary() noexcept{ } Error* Compiler::exprBinaryOp() noexcept{ - auto e = make_expr(prev().type); - e->lhs = ctx()->s_popx(); - Error* err = parse_expression(rules[e->op].precedence + 1); - if(err){ - delete_expr(e); - return err; - } + Error* err; + int line = prev().line; + TokenIndex op = prev().type; + check(parse_expression(rules[op].precedence + 1)); + BinaryExpr* e = make_expr(op); + e->line = line; e->rhs = ctx()->s_popx(); - ctx()->s_push(std::move(e)); + e->lhs = ctx()->s_popx(); + ctx()->s_push(e); return NULL; } @@ -883,7 +883,8 @@ Error* Compiler::try_compile_assignment(bool* is_assign) noexcept{ // a[x] += 1; a and x should be evaluated only once // a.x += 1; a should be evaluated only once // -1 to remove =; inplace=true - auto e = make_expr(prev().type - 1, true); + // TODO: memory leak on error here! + BinaryExpr* e = make_expr(prev().type - 1, true); e->lhs = ctx()->s_popx(); check(EXPR_TUPLE()); e->rhs = ctx()->s_popx(); @@ -905,9 +906,9 @@ Error* Compiler::try_compile_assignment(bool* is_assign) noexcept{ for(int j = 1; j < n; j++) ctx()->emit_(OP_DUP_TOP, BC_NOARG, BC_KEEPLINE); for(int j = 0; j < n; j++) { - auto e = ctx()->s_popx(); - if(e->is_starred()) return SyntaxError(); - bool ok = e->emit_store(ctx()); + if(ctx()->s_top()->is_starred()) return SyntaxError(); + bool ok = ctx()->s_top()->emit_store(ctx()); + ctx()->s_pop(); if(!ok) return SyntaxError(); } *is_assign = true;