Skip to content

Commit

Permalink
fix: Crash when overwriting builtin with walrus operator (fixes #416) (
Browse files Browse the repository at this point in the history
…#418)

It appears that `check_newvar` was called with `&e1`,  but then `new_var` was mistakenly called with `e`, which caused the parser to generate garbage opcodes, leading to crashes.

Added test case to verify this behaviour.
  • Loading branch information
alufers authored Apr 14, 2024
1 parent b4f37db commit 1ca4383
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/be_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ static void walrus_expr(bparser *parser, bexpdesc *e)
expr(parser, e);
check_var(parser, e);
if (check_newvar(parser, &e1)) { /* new variable */
new_var(parser, e1.v.s, e);
new_var(parser, e1.v.s, &e1);
}
if (be_code_setvar(parser->finfo, &e1, e, btrue /* do not release register */ )) {
parser->lexer.linenumber = line;
Expand Down
9 changes: 9 additions & 0 deletions tests/walrus.be
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ class confused_walrus
end
var ins = confused_walrus()
assert(ins.f() == ins)

# Check overwriting a builtin (https://github.com/berry-lang/berry/issues/416)

def check_overwrite_builtin()
print := 1
assert(print == 1)
end

check_overwrite_builtin()

0 comments on commit 1ca4383

Please sign in to comment.