Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,28 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
}
}

// A substitution rule must not turn a node into a structurally incompatible
// type. The XML was validated (children count, mandatory ID) against the
// original node type, so replacing a leaf with a SubTree, Decorator or
// Control leaves the builder with a node whose child or "ID" attribute the
// XML never supplied. That later dereferences a null child pointer or builds
// a std::string from a null Attribute("ID"). Allow same-type swaps and swaps
// to a leaf (the common "replace with a mock action" case, including the
// SubTree -> TestNode substitution used for mocking).
if(substituted)
{
const NodeType original_type = it_manifest->second.type;
const NodeType new_type = node->type();
if(new_type != original_type && new_type != NodeType::ACTION &&
new_type != NodeType::CONDITION)
{
throw RuntimeError("Substitution of node [", ID, "] of type [",
toStr(original_type), "] with a node of type [", toStr(new_type),
"] is not allowed: a substitution may only keep the same "
"type or replace the node with a leaf (Action/Condition)");
}
}

// No substitution rule applied: default behavior
if(!substituted)
{
Expand Down
43 changes: 43 additions & 0 deletions tests/gtest_substitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,46 @@ TEST(Substitution, StringSubstitutionRegistrationID_Issue930)
// The substituted node should still work correctly
ASSERT_EQ(tree.tickWhileRunning(), NodeStatus::SUCCESS);
}

// Regression test: a substitution rule must not turn a node into a
// structurally incompatible type. The XML for a compact leaf (<AlwaysSuccess/>)
// carries no "ID" attribute and no children, so replacing it with a SubTree
// (which reads element->Attribute("ID")) or a Decorator (which needs a child)
// used to segfault at tree construction / first tick. It must now throw.
TEST(Substitution, IncompatibleTypeSubstitutionThrows)
{
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<AlwaysSuccess name="action_A"/>
</BehaviorTree>
</root>
)";

// leaf -> SubTree: element has no ID attribute
{
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(xml_text);
factory.addSubstitutionRule("action_A", "SubTree");
EXPECT_THROW(factory.createTree("MainTree"), RuntimeError);
}

// leaf -> Decorator: element has no child
{
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(xml_text);
factory.addSubstitutionRule("action_A", "Inverter");
EXPECT_THROW(factory.createTree("MainTree"), RuntimeError);
}

// leaf -> leaf stays valid (the common mock case)
{
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(xml_text);
factory.registerSimpleAction("MyMock", [](TreeNode&) { return NodeStatus::SUCCESS; });
factory.addSubstitutionRule("action_A", "MyMock");
Tree tree;
ASSERT_NO_THROW(tree = factory.createTree("MainTree"));
EXPECT_EQ(tree.tickWhileRunning(), NodeStatus::SUCCESS);
}
}
Loading