Skip to content

Commit 5ca367f

Browse files
XMLParser: detect cyclic <include> instead of overflowing the stack (#1250)
A tree file that includes itself, directly or through other files, made loadDocImpl() recurse until the process crashed with a stack overflow (SIGSEGV), because the include loop never checked whether a file was already being loaded. Track the normalized paths of the files in the active include chain and throw a RuntimeError naming the repeated file. The chain is a stack (pushed before, popped after each include), so a file included twice from different branches is not reported as a cycle; it is reset by the top-level loadFromFile()/loadFromText(), so a failed load leaves no stale state in the factory's parser. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 90ce0a2 commit 5ca367f

5 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎src/xml_parsing.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "behaviortree_cpp/basic_types.h"
1414

15+
#include <algorithm>
1516
#include <charconv>
1617
#include <cstdio>
1718
#include <cstring>
@@ -103,6 +104,15 @@ auto StrEqual = [](const char* str1, const char* str2) -> bool {
103104
return strcmp(str1, str2) == 0;
104105
};
105106

107+
// Normalized form of an included file path, so that different spellings of
108+
// the same file ("a.xml", "./a.xml", "dir/../a.xml") compare equal.
109+
std::filesystem::path IncludeKey(const std::filesystem::path& absolute_path)
110+
{
111+
std::error_code err;
112+
auto canonical = std::filesystem::weakly_canonical(absolute_path, err);
113+
return err ? absolute_path.lexically_normal() : canonical;
114+
}
115+
106116
void validateModelName(const std::string& name, int line_number)
107117
{
108118
const auto line_str = std::to_string(line_number);
@@ -242,6 +252,8 @@ struct XMLParser::PImpl
242252
const BehaviorTreeFactory* factory = nullptr;
243253

244254
std::filesystem::path current_path;
255+
// files currently being loaded through <include>, used to detect cycles
256+
std::vector<std::filesystem::path> include_stack;
245257
std::map<std::string, SubtreeModel> subtree_models;
246258

247259
int suffix_count = 0;
@@ -289,6 +301,7 @@ void XMLParser::loadFromFile(const std::filesystem::path& filepath, bool add_inc
289301
doc->LoadFile(filepath.string().c_str());
290302

291303
_p->current_path = std::filesystem::absolute(filepath.parent_path());
304+
_p->include_stack = { IncludeKey(std::filesystem::absolute(filepath)) };
292305

293306
_p->loadDocImpl(doc, add_includes);
294307
}
@@ -300,6 +313,7 @@ void XMLParser::loadFromText(const std::string& xml_text, bool add_includes)
300313
XMLDocument* doc = _p->opened_documents.back().get();
301314
doc->Parse(xml_text.c_str(), xml_text.size());
302315

316+
_p->include_stack.clear();
303317
_p->loadDocImpl(doc, add_includes);
304318
}
305319

@@ -414,15 +428,27 @@ void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
414428
file_path = current_path / file_path;
415429
}
416430

431+
// A file that includes itself, directly or through other files, would
432+
// otherwise recurse until the stack overflows.
433+
auto include_key = IncludeKey(file_path);
434+
if(std::find(include_stack.begin(), include_stack.end(), include_key) !=
435+
include_stack.end())
436+
{
437+
throw RuntimeError("Cyclic <include> detected: the file [", include_key.string(),
438+
"] includes itself, directly or through other files");
439+
}
440+
417441
opened_documents.push_back(std::make_unique<XMLDocument>());
418442
XMLDocument* next_doc = opened_documents.back().get();
419443

420444
// change current path to the included file for handling additional relative paths
421445
const auto previous_path = current_path;
422446
current_path = std::filesystem::absolute(file_path.parent_path());
423447

448+
include_stack.push_back(std::move(include_key));
424449
next_doc->LoadFile(file_path.string().c_str());
425450
loadDocImpl(next_doc, add_includes);
451+
include_stack.pop_back();
426452

427453
// reset current path to the previous value
428454
current_path = previous_path;

‎tests/gtest_factory.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,24 @@ TEST(
361361
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickWhileRunning());
362362
}
363363

364+
TEST(BehaviorTreeFactory, CreateTreeFromFileWhichIncludesItself)
365+
{
366+
BehaviorTreeFactory factory;
367+
std::string path = FilePath("trees/self_include.xml");
368+
ASSERT_THROW(factory.createTreeFromFile(path), RuntimeError);
369+
}
370+
371+
TEST(BehaviorTreeFactory, CreateTreeFromFileWithCyclicIncludes)
372+
{
373+
BehaviorTreeFactory factory;
374+
std::string path = FilePath("trees/parent_include_child_cycle.xml");
375+
ASSERT_THROW(factory.createTreeFromFile(path), RuntimeError);
376+
377+
// a failed load must not leave stale state behind in the factory's parser
378+
Tree tree = factory.createTreeFromFile(FilePath("trees/parent_include_child.xml"));
379+
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickWhileRunning());
380+
}
381+
364382
TEST(BehaviorTreeFactory, WrongTreeName)
365383
{
366384
const char* xmlA = R"(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<root BTCPP_format="4" main_tree_to_execute = "ChildIncludeParentCycle">
2+
<include path="../parent_include_child_cycle.xml" />
3+
4+
<BehaviorTree ID="ChildIncludeParentCycle">
5+
<AlwaysSuccess />
6+
</BehaviorTree>
7+
</root>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<root BTCPP_format="4" main_tree_to_execute = "ParentIncludeChildCycle">
2+
<include path="child/child_include_parent_cycle.xml" />
3+
4+
<BehaviorTree ID="ParentIncludeChildCycle">
5+
<AlwaysSuccess />
6+
</BehaviorTree>
7+
</root>

‎tests/trees/self_include.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<root BTCPP_format="4" main_tree_to_execute = "SelfInclude">
2+
<include path="self_include.xml" />
3+
4+
<BehaviorTree ID="SelfInclude">
5+
<AlwaysSuccess />
6+
</BehaviorTree>
7+
</root>

0 commit comments

Comments
 (0)