Skip to content

Commit 0ca8a51

Browse files
livecodealimontegoulding
authored andcommitted
[[ Bug 11039 ]] Throw error when changing behavior in behavior
This patch prevents behavior scripts from changing their child's behavior. (cherry picked from commit 24e482a)
1 parent 49522c2 commit 0ca8a51

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

docs/notes/bugfix-11039.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Throw error when changing behavior from behavior script

engine/src/exec-interface-object.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,10 +1698,20 @@ void MCObject::SetParentScript(MCExecContext& ctxt, MCStringRef new_parent_scrip
16981698
bool t_success;
16991699
t_success = true;
17001700

1701+
// If this object's parent script is on the script stack, it's an
1702+
// execution error
1703+
if (parent_script != nil &&
1704+
parent_script -> GetParent() -> GetObject() != nil &&
1705+
parent_script -> GetParent() -> GetObject() -> getscriptdepth() > 0)
1706+
{
1707+
ctxt . LegacyThrow(EE_PARENTSCRIPT_EXECUTING);
1708+
return;
1709+
}
1710+
17011711
// MW-2008-11-02: [[ Bug ]] Setting the parentScript of an object to
17021712
// empty should unset the parent script property and not throw an
17031713
// error.
1704-
if (new_parent_script == nil || MCStringGetLength(new_parent_script) == 0)
1714+
if (MCStringIsEmpty(new_parent_script))
17051715
{
17061716
if (parent_script != NULL)
17071717
parent_script -> Release();

engine/src/executionerrors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,9 @@ enum Exec_errors
27372737

27382738
// {EE-0896} graphic : too many points
27392739
EE_GRAPHIC_TOOMANYPOINTS,
2740+
2741+
// {EE-0897} parentScript: can't change parent while parent script is executing
2742+
EE_PARENTSCRIPT_EXECUTING,
27402743
};
27412744

27422745
extern const char *MCexecutionerrors;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
script "CoreEngineBehavior"
2+
/*
3+
Copyright (C) 2016 LiveCode Ltd.
4+
5+
This file is part of LiveCode.
6+
7+
LiveCode is free software; you can redistribute it and/or modify it under
8+
the terms of the GNU General Public License v3 as published by the Free
9+
Software Foundation.
10+
11+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
12+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
18+
19+
on _TestCyclicBehavior pStack
20+
set the behavior of (the behavior of pStack) to pStack
21+
end _TestCyclicBehavior
22+
23+
on TestCyclicBehavior
24+
create stack "Behavior"
25+
create stack
26+
set the behavior of it to the long id of stack "Behavior"
27+
TestAssertThrow "cycle in behavior script hierarchy throws", \
28+
"_TestCyclicBehavior", the long id of me, 678, it
29+
end TestCyclicBehavior
30+
31+
on TestInvalidBehaviorChange
32+
local tScript
33+
put "on changeBehavior" & return into tScript
34+
put "set the behavior of me to the long id of stack" && quote & "NewBehavior" & quote & return after tScript
35+
put "end changeBehavior" after tScript
36+
37+
create stack "NewBehavior"
38+
create stack "Behavior"
39+
set the script of it to tScript
40+
41+
create stack
42+
set the behavior of it to the long id of stack "Behavior"
43+
TestAssertThrow "behavior change while parent script is executing throws", \
44+
"changeBehavior", it, 895
45+
end TestInvalidBehaviorChange

0 commit comments

Comments
 (0)