Skip to content

Commit dc851c4

Browse files
committed
fix #503: Use signed integer for loop count
1 parent df198ff commit dc851c4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
206206
assert(pos);
207207
unsigned int *loopStart;
208208
unsigned int *loopEnd;
209-
size_t loopCount;
209+
long loopCount;
210210
if (reset) {
211211
atEnd = false;
212212
noBreak = true;

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,3 +1814,20 @@ TEST(VirtualMachineTest, NoCrashAfterCallingProcedureFromLoop)
18141814
ASSERT_EQ(vm.registerCount(), 0);
18151815
ASSERT_EQ(::testing::internal::GetCapturedStdout(), "test\ntest\n");
18161816
}
1817+
1818+
TEST(VirtualMachineTest, NegativeLoopCount)
1819+
{
1820+
// Regtest for #503
1821+
static unsigned int bytecode[] = { OP_START, OP_CONST, 0, OP_REPEAT_LOOP, OP_CONST, 1, OP_PRINT, OP_BREAK_FRAME, OP_LOOP_END, OP_HALT };
1822+
static Value constValues[] = { -1, "test" };
1823+
1824+
VirtualMachine vm;
1825+
vm.setBytecode(bytecode);
1826+
vm.setConstValues(constValues);
1827+
1828+
::testing::internal::CaptureStdout();
1829+
vm.run();
1830+
ASSERT_TRUE(::testing::internal::GetCapturedStdout().empty());
1831+
ASSERT_TRUE(vm.atEnd());
1832+
ASSERT_EQ(vm.registerCount(), 0);
1833+
}

0 commit comments

Comments
 (0)