Skip to content

Commit 9a92228

Browse files
committed
fix #364: Fix skipping of if statements evaluated to "false"
1 parent 2e979ab commit 9a92228

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,15 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
238238

239239
do_if:
240240
if (!READ_LAST_REG()->toBool()) {
241-
while (*pos != OP_ELSE && *pos != OP_ENDIF)
241+
unsigned int ifCounter = 1;
242+
while ((*pos != OP_ELSE && *pos != OP_ENDIF) || (ifCounter > 0)) {
242243
pos += instruction_arg_count[*pos++];
244+
245+
if (*pos == OP_IF)
246+
ifCounter++;
247+
else if ((*pos == OP_ELSE) || (*pos == OP_ENDIF))
248+
ifCounter--;
249+
}
243250
}
244251
FREE_REGS(1);
245252
DISPATCH();

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,10 +1609,20 @@ TEST(VirtualMachineTest, NoCrashWhenRepeatingZeroTimes)
16091609
static unsigned int bytecode[] = { OP_START, OP_CONST, 0, OP_REPEAT_LOOP, OP_UNTIL_LOOP, OP_NULL, OP_BEGIN_UNTIL_LOOP, OP_LOOP_END, OP_LOOP_END, OP_HALT };
16101610
static Value constValues[] = { 0 };
16111611

1612-
EngineMock engineMock;
1613-
VirtualMachine vm(nullptr, &engineMock, nullptr);
1612+
VirtualMachine vm(nullptr, nullptr, nullptr);
16141613
vm.setBytecode(bytecode);
16151614
vm.setConstValues(constValues);
16161615
vm.run();
16171616
ASSERT_EQ(vm.registerCount(), 0);
16181617
}
1618+
1619+
TEST(VirtualMachineTest, NoCrashInNestedIfStatementsWithLoop)
1620+
{
1621+
// Regtest for #364
1622+
static unsigned int bytecode[] = { OP_START, OP_NULL, OP_IF, OP_NULL, OP_REPEAT_LOOP, OP_NULL, OP_IF, OP_ENDIF, OP_LOOP_END, OP_ENDIF, OP_HALT };
1623+
1624+
VirtualMachine vm(nullptr, nullptr, nullptr);
1625+
vm.setBytecode(bytecode);
1626+
vm.run();
1627+
ASSERT_EQ(vm.registerCount(), 0);
1628+
}

0 commit comments

Comments
 (0)