From fcdebd7d041c43e5c2227a0d19b59efa4b3dff9f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 4 Dec 2024 22:38:31 +0100 Subject: [PATCH] Fix break statement in presence of labels In this snippet... for (;;) label: break ...the break statement jumped back to the start of the loop instead of *out* of the loop. Fixes: https://github.com/quickjs-ng/quickjs/issues/741 --- quickjs.c | 4 +--- tests/bug741.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/bug741.js diff --git a/quickjs.c b/quickjs.c index 2082fbf0..ccb840ef 100644 --- a/quickjs.c +++ b/quickjs.c @@ -24917,9 +24917,7 @@ static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont) emit_goto(s, OP_goto, top->label_cont); return 0; } - if (!is_cont && - top->label_break != -1 && - (name == JS_ATOM_NULL || top->label_name == name)) { + if (!is_cont && top->label_break != -1 && top->label_name == name) { emit_goto(s, OP_goto, top->label_break); return 0; } diff --git a/tests/bug741.js b/tests/bug741.js new file mode 100644 index 00000000..3947f719 --- /dev/null +++ b/tests/bug741.js @@ -0,0 +1,19 @@ +import {assert} from "./assert.js" + +while (1) label: break + +var i = 0 +while (i < 3) label: { + if (i > 0) + break + i++ +} +assert(i, 1) + +for (;;) label: break + +for (i = 0; i < 3; i++) label: { + if (i > 0) + break +} +assert(i, 1)