Skip to content

Commit 0d7a4d0

Browse files
authored
Merge pull request #388 from scratchcpp/fix_vm_stopping_clearing_arguments
Fix #387: Do not clear procedure arguments when stopping the VM
2 parents b5ee58f + 1590f63 commit 0d7a4d0

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,6 @@ do_exec : {
731731
}
732732
if (stop) {
733733
stop = false;
734-
callTree.clear();
735-
procedureArgTree.clear();
736-
procedureArgs = nullptr;
737-
nextProcedureArgs = nullptr;
738734
if (goBack) {
739735
goBack = false;
740736
pos -= instruction_arg_count[OP_EXEC] + 1;

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,3 +1687,31 @@ TEST(VirtualMachineTest, NoCrashInNestedLoopsInIfElseStatements)
16871687
vm.run();
16881688
ASSERT_EQ(vm.registerCount(), 0);
16891689
}
1690+
1691+
TEST(VirtualMachineTest, NoCrashWhenReadingProcedureArgsAfterStopping)
1692+
{
1693+
// Regtest for #387
1694+
static unsigned int bytecode[] = { OP_START, OP_INIT_PROCEDURE, OP_CONST, 0, OP_ADD_ARG, OP_CALL_PROCEDURE, 0, OP_HALT };
1695+
static unsigned int procedure[] = { OP_START, OP_NULL, OP_EXEC, 0, OP_READ_ARG, 0, OP_PRINT, OP_HALT };
1696+
static unsigned int *procedures[] = { procedure };
1697+
static BlockFunc functions[] = { &testFunction3 };
1698+
static Value constValues[] = { "test" };
1699+
1700+
VirtualMachine vm(nullptr, nullptr, nullptr);
1701+
vm.setBytecode(bytecode);
1702+
vm.setProcedures(procedures);
1703+
vm.setFunctions(functions);
1704+
vm.setConstValues(constValues);
1705+
1706+
testing::internal::CaptureStdout();
1707+
vm.run();
1708+
ASSERT_TRUE(testing::internal::GetCapturedStdout().empty());
1709+
ASSERT_EQ(vm.registerCount(), 0);
1710+
ASSERT_FALSE(vm.atEnd());
1711+
1712+
testing::internal::CaptureStdout();
1713+
vm.run();
1714+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\n");
1715+
ASSERT_EQ(vm.registerCount(), 0);
1716+
ASSERT_TRUE(vm.atEnd());
1717+
}

0 commit comments

Comments
 (0)