Skip to content

Commit b4137a1

Browse files
validate substituted node type in instantiateTreeNode (#1193)
* validate substituted node type in instantiateTreeNode * silence nodiscard warnings in the substitution type test
1 parent bfcc743 commit b4137a1

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎src/bt_factory.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,28 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
365365
}
366366
}
367367

368+
// A substitution rule must not turn a node into a structurally incompatible
369+
// type. The XML was validated (children count, mandatory ID) against the
370+
// original node type, so replacing a leaf with a SubTree, Decorator or
371+
// Control leaves the builder with a node whose child or "ID" attribute the
372+
// XML never supplied. That later dereferences a null child pointer or builds
373+
// a std::string from a null Attribute("ID"). Allow same-type swaps and swaps
374+
// to a leaf (the common "replace with a mock action" case, including the
375+
// SubTree -> TestNode substitution used for mocking).
376+
if(substituted)
377+
{
378+
const NodeType original_type = it_manifest->second.type;
379+
const NodeType new_type = node->type();
380+
if(new_type != original_type && new_type != NodeType::ACTION &&
381+
new_type != NodeType::CONDITION)
382+
{
383+
throw RuntimeError("Substitution of node [", ID, "] of type [",
384+
toStr(original_type), "] with a node of type [", toStr(new_type),
385+
"] is not allowed: a substitution may only keep the same "
386+
"type or replace the node with a leaf (Action/Condition)");
387+
}
388+
}
389+
368390
// No substitution rule applied: default behavior
369391
if(!substituted)
370392
{

‎tests/gtest_substitution.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,46 @@ TEST(Substitution, StringSubstitutionRegistrationID_Issue930)
546546
// The substituted node should still work correctly
547547
ASSERT_EQ(tree.tickWhileRunning(), NodeStatus::SUCCESS);
548548
}
549+
550+
// Regression test: a substitution rule must not turn a node into a
551+
// structurally incompatible type. The XML for a compact leaf (<AlwaysSuccess/>)
552+
// carries no "ID" attribute and no children, so replacing it with a SubTree
553+
// (which reads element->Attribute("ID")) or a Decorator (which needs a child)
554+
// used to segfault at tree construction / first tick. It must now throw.
555+
TEST(Substitution, IncompatibleTypeSubstitutionThrows)
556+
{
557+
static const char* xml_text = R"(
558+
<root BTCPP_format="4">
559+
<BehaviorTree ID="MainTree">
560+
<AlwaysSuccess name="action_A"/>
561+
</BehaviorTree>
562+
</root>
563+
)";
564+
565+
// leaf -> SubTree: element has no ID attribute
566+
{
567+
BehaviorTreeFactory factory;
568+
factory.registerBehaviorTreeFromText(xml_text);
569+
factory.addSubstitutionRule("action_A", "SubTree");
570+
EXPECT_THROW((void)factory.createTree("MainTree"), RuntimeError);
571+
}
572+
573+
// leaf -> Decorator: element has no child
574+
{
575+
BehaviorTreeFactory factory;
576+
factory.registerBehaviorTreeFromText(xml_text);
577+
factory.addSubstitutionRule("action_A", "Inverter");
578+
EXPECT_THROW((void)factory.createTree("MainTree"), RuntimeError);
579+
}
580+
581+
// leaf -> leaf stays valid (the common mock case)
582+
{
583+
BehaviorTreeFactory factory;
584+
factory.registerBehaviorTreeFromText(xml_text);
585+
factory.registerSimpleAction("MyMock", [](TreeNode&) { return NodeStatus::SUCCESS; });
586+
factory.addSubstitutionRule("action_A", "MyMock");
587+
Tree tree;
588+
ASSERT_NO_THROW(tree = factory.createTree("MainTree"));
589+
EXPECT_EQ(tree.tickWhileRunning(), NodeStatus::SUCCESS);
590+
}
591+
}

0 commit comments

Comments
 (0)