Skip to content

Commit be86c7d

Browse files
committed
fix #380: Fix skipping of nested if statements and loops in else branch
1 parent 8f76eb3 commit be86c7d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,20 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
254254
DISPATCH();
255255

256256
do_else:
257-
while (*pos != OP_ENDIF)
257+
unsigned int ifCounter = 1;
258+
while (!(*pos == OP_ENDIF && ifCounter == 0)) {
258259
pos += instruction_arg_count[*pos++];
259260

261+
if ((*pos == OP_IF) || (*pos == OP_FOREVER_LOOP) || (*pos == OP_REPEAT_LOOP) || (*pos == OP_UNTIL_LOOP))
262+
ifCounter++;
263+
else if ((*pos == OP_ENDIF) || (*pos == OP_LOOP_END)) {
264+
assert(ifCounter > 0);
265+
ifCounter--;
266+
}
267+
268+
assert(!(*pos == OP_ELSE && ifCounter == 1));
269+
}
270+
260271
do_endif:
261272
DISPATCH();
262273

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,17 @@ TEST(VirtualMachineTest, NoCrashInNestedIfStatementsWithLoopAndIfElse)
16411641
TEST(VirtualMachineTest, NoCrashInNestedLoopsInRepeatUntilLoop)
16421642
{
16431643
// Regtest for #379
1644+
static unsigned int bytecode[] = { OP_START, OP_NULL, OP_NOT, OP_IF, OP_ELSE, OP_NULL, OP_REPEAT_LOOP, OP_NULL, OP_IF, OP_ENDIF, OP_LOOP_END, OP_ENDIF, OP_HALT };
1645+
1646+
VirtualMachine vm(nullptr, nullptr, nullptr);
1647+
vm.setBytecode(bytecode);
1648+
vm.run();
1649+
ASSERT_EQ(vm.registerCount(), 0);
1650+
}
1651+
1652+
TEST(VirtualMachineTest, NoCrashInNestedLoopsInIfElseStatements)
1653+
{
1654+
// Regtest for #380
16441655
static unsigned int bytecode[] = { OP_START, OP_UNTIL_LOOP, OP_NULL, OP_NOT, OP_BEGIN_UNTIL_LOOP, OP_NULL, OP_REPEAT_LOOP, OP_LOOP_END, OP_LOOP_END, OP_HALT };
16451656

16461657
VirtualMachine vm(nullptr, nullptr, nullptr);

0 commit comments

Comments
 (0)