Skip to content

Fix skipping of if statements and loops #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/engine/virtualmachine_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,25 +236,40 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
checkpoint = pos - 1;
DISPATCH();

do_if:
do_if : {
if (!READ_LAST_REG()->toBool()) {
unsigned int ifCounter = 1;
while ((*pos != OP_ELSE && *pos != OP_ENDIF) || (ifCounter > 0)) {
while (!((*pos == OP_ELSE && ifCounter == 1) || (*pos == OP_ENDIF && ifCounter == 0))) {
pos += instruction_arg_count[*pos++];

if (*pos == OP_IF)
if ((*pos == OP_IF) || (*pos == OP_FOREVER_LOOP) || (*pos == OP_REPEAT_LOOP) || (*pos == OP_UNTIL_LOOP))
ifCounter++;
else if ((*pos == OP_ELSE) || (*pos == OP_ENDIF))
else if ((*pos == OP_ENDIF) || (*pos == OP_LOOP_END)) {
assert(ifCounter > 0);
ifCounter--;
}
}
}
FREE_REGS(1);
DISPATCH();
}

do_else:
while (*pos != OP_ENDIF)
do_else : {
unsigned int ifCounter = 1;
while (!(*pos == OP_ENDIF && ifCounter == 0)) {
pos += instruction_arg_count[*pos++];

if ((*pos == OP_IF) || (*pos == OP_FOREVER_LOOP) || (*pos == OP_REPEAT_LOOP) || (*pos == OP_UNTIL_LOOP))
ifCounter++;
else if ((*pos == OP_ENDIF) || (*pos == OP_LOOP_END)) {
assert(ifCounter > 0);
ifCounter--;
}

assert(!(*pos == OP_ELSE && ifCounter == 1));
}
}

do_endif:
DISPATCH();

Expand All @@ -275,10 +290,12 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
while ((*loopEnd != OP_LOOP_END) || (loopCounter > 0)) {
loopEnd += instruction_arg_count[*loopEnd++];

if ((*loopEnd == OP_FOREVER_LOOP) || (*loopEnd == OP_REPEAT_LOOP) || (*loopEnd == OP_UNTIL_LOOP))
if ((*loopEnd == OP_IF) || (*loopEnd == OP_FOREVER_LOOP) || (*loopEnd == OP_REPEAT_LOOP) || (*loopEnd == OP_UNTIL_LOOP))
loopCounter++;
else if (*loopEnd == OP_LOOP_END)
else if ((*loopEnd == OP_ENDIF) || (*loopEnd == OP_LOOP_END)) {
assert(loopCounter > 0);
loopCounter--;
}
}
pos = loopEnd;
} else {
Expand Down Expand Up @@ -317,8 +334,17 @@ do_repeat_loop_index1 : {
pos = loopStart;
} else {
pos = loopStart;
while (*pos != OP_LOOP_END)
unsigned int loopCounter = 1;
while ((*pos != OP_LOOP_END) || (loopCounter > 0)) {
pos += instruction_arg_count[*pos++];

if ((*pos == OP_IF) || (*pos == OP_FOREVER_LOOP) || (*pos == OP_REPEAT_LOOP) || (*pos == OP_UNTIL_LOOP))
loopCounter++;
else if ((*pos == OP_ENDIF) || (*pos == OP_LOOP_END)) {
assert(loopCounter > 0);
loopCounter--;
}
}
}
FREE_REGS(1);
DISPATCH();
Expand Down
33 changes: 33 additions & 0 deletions test/virtual_machine/virtual_machine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,3 +1626,36 @@ TEST(VirtualMachineTest, NoCrashInNestedIfStatementsWithLoop)
vm.run();
ASSERT_EQ(vm.registerCount(), 0);
}

TEST(VirtualMachineTest, NoCrashInNestedIfStatementsWithLoopAndIfElse)
{
// Regtest for #378
static unsigned int bytecode[] = { OP_START, OP_NULL, OP_IF, OP_NULL, OP_REPEAT_LOOP, OP_NULL, OP_IF, OP_ELSE, OP_ENDIF, OP_LOOP_END, OP_ENDIF, OP_HALT };

VirtualMachine vm(nullptr, nullptr, nullptr);
vm.setBytecode(bytecode);
vm.run();
ASSERT_EQ(vm.registerCount(), 0);
}

TEST(VirtualMachineTest, NoCrashInNestedLoopsInRepeatUntilLoop)
{
// Regtest for #379
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 };

VirtualMachine vm(nullptr, nullptr, nullptr);
vm.setBytecode(bytecode);
vm.run();
ASSERT_EQ(vm.registerCount(), 0);
}

TEST(VirtualMachineTest, NoCrashInNestedLoopsInIfElseStatements)
{
// Regtest for #380
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 };

VirtualMachine vm(nullptr, nullptr, nullptr);
vm.setBytecode(bytecode);
vm.run();
ASSERT_EQ(vm.registerCount(), 0);
}