Skip to content

Commit 7b8ef76

Browse files
committed
fix #384: Ignore undefined procedures
1 parent c04807b commit 7b8ef76

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,19 @@ do_exec : {
759759
nextProcedureArgs = &procedureArgTree.back();
760760
DISPATCH();
761761

762-
do_call_procedure:
763-
callTree.push_back(++pos);
764-
procedureArgs = nextProcedureArgs;
765-
nextProcedureArgs = nullptr;
766-
pos = procedures[*pos];
762+
do_call_procedure : {
763+
unsigned int *procedurePos = procedures[pos[1]];
764+
765+
if (procedurePos) {
766+
callTree.push_back(++pos);
767+
procedureArgs = nextProcedureArgs;
768+
nextProcedureArgs = nullptr;
769+
pos = procedurePos;
770+
} else
771+
pos++;
772+
767773
DISPATCH();
774+
}
768775

769776
do_add_arg:
770777
nextProcedureArgs->push_back(*READ_LAST_REG());

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,34 @@ TEST(VirtualMachineTest, RunProcedures)
14661466
ASSERT_EQ(vm.registerCount(), 0);
14671467
}
14681468

1469+
TEST(VirtualMachineTest, RunUndefinedProcedure)
1470+
{
1471+
static unsigned int bytecode[] = {
1472+
OP_START, OP_INIT_PROCEDURE, OP_CONST, 0, OP_ADD_ARG, OP_CONST, 1, OP_ADD_ARG, OP_CALL_PROCEDURE, 0, OP_NULL, OP_EXEC, 0, OP_INIT_PROCEDURE, OP_CALL_PROCEDURE, 1, OP_HALT
1473+
};
1474+
static unsigned int procedure2[] = { OP_START, OP_CONST, 2, OP_PRINT, OP_HALT };
1475+
static unsigned int *procedures[] = { nullptr, procedure2 };
1476+
static BlockFunc functions[] = { &testFunction3 };
1477+
static Value constValues[] = { "hello", "world", "test" };
1478+
1479+
VirtualMachine vm;
1480+
vm.setBytecode(bytecode);
1481+
vm.setProcedures(procedures);
1482+
vm.setFunctions(functions);
1483+
vm.setConstValues(constValues);
1484+
testing::internal::CaptureStdout();
1485+
vm.run();
1486+
ASSERT_TRUE(testing::internal::GetCapturedStdout().empty());
1487+
ASSERT_FALSE(vm.atEnd());
1488+
ASSERT_EQ(vm.registerCount(), 0);
1489+
1490+
testing::internal::CaptureStdout();
1491+
vm.run();
1492+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\n");
1493+
ASSERT_TRUE(vm.atEnd());
1494+
ASSERT_EQ(vm.registerCount(), 0);
1495+
}
1496+
14691497
TEST(VirtualMachineTest, OP_BREAK_FRAME)
14701498
{
14711499
static unsigned int bytecode1[] = { OP_START, OP_FOREVER_LOOP, OP_BREAK_FRAME, OP_LOOP_END, OP_HALT };

0 commit comments

Comments
 (0)