@@ -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