Skip to content

forget a rejected XML document in XMLParser - #1266

Merged
facontidavide merged 1 commit into
BehaviorTree:masterfrom
aysha-afrah26:xml-parser-rejected-document
Nov 7, 2026
Merged

facontidavide merged 1 commit into
BehaviorTree:masterfrom
aysha-afrah26:xml-parser-rejected-document

Conversation

@aysha-afrah26

Copy link
Copy Markdown
Contributor

UBSan, createTreeFromText on a factory that had rejected one malformed document before:

src/xml_parsing.cpp:750:51: runtime error: member call on null pointer of type 'tinyxml2::XMLElement'
    #0 BT::XMLParser::instantiateTree(...) xml_parsing.cpp:750
    #1 BT::BehaviorTreeFactory::createTreeFromText(...) bt_factory.cpp:460

loadFromText and loadFromFile append the new XMLDocument to opened_documents before loading it, and a document that loadDocImpl rejects (XML that doesn't parse, a missing file, a tree that fails VerifyXML) stays in the list even though it registered no tree. instantiateTree with an empty main_tree_ID then reads main_tree_to_execute from the root element of the first document in that list, which is null for a document that never parsed. A release build dereferences it.

The factory keeps one parser for its whole life, so a single registerBehaviorTreeFromText/File call that threw on a bad file (an application loading user-provided trees and carrying on after the error) is enough: every later createTreeFromText/FromFile whose main tree can't be resolved from the new document (two trees and no main_tree_to_execute, an empty ID) crashes instead of reporting the missing attribute, and a rejected document that did parse makes instantiateTree look for its main_tree_to_execute instead of the valid document's tree. With the low-level XMLParser API the same happens on instantiateTree(blackboard) right after one failed loadFromText.

The fix lives in the parser: a small PImpl helper loads the document just appended and erases it again if loadDocImpl throws, at the three places that push a document (loadFromFile, loadFromText, and the <include> loop, since a failed include would become the front once the top-level document is dropped). instantiateTree also throws a RuntimeError instead of calling front() on an empty list. Private PImpl plus one function body, no API or ABI change; included documents that did load keep their registered trees, so nothing points at a removed document.

BehaviorTreeFactory.RejectedDocumentIsForgotten aborts under UBSan on master and passes with the fix; the full suite is green under ASan+UBSan and TSan.

@facontidavide
facontidavide merged commit cf22aa5 into BehaviorTree:master Nov 7, 2026
17 checks passed
facontidavide pushed a commit to Quchaosheng/BehaviorTree.CPP that referenced this pull request Nov 12, 2026
BehaviorTree#1266 dropped a document that failed to load, but the rollback stopped at the
document boundary. If main.xml includes sub.xml and then fails, sub.xml has
already registered its trees and they stay behind, and sub.xml becomes the
first document, so instantiateTree() reads main_tree_to_execute from it.

Take a snapshot before the call and restore it on failure. That also puts back
a tree this call redefined, whose element lives in the document about to be
erased.

Fixes BehaviorTree#1282
facontidavide added a commit that referenced this pull request Nov 12, 2026
…from throwing (#1289)

* Roll back a rejected XML load as a whole, not per document

#1266 dropped a document that failed to load, but the rollback stopped at the
document boundary. If main.xml includes sub.xml and then fails, sub.xml has
already registered its trees and they stay behind, and sub.xml becomes the
first document, so instantiateTree() reads main_tree_to_execute from it.

Take a snapshot before the call and restore it on failure. That also puts back
a tree this call redefined, whose element lives in the document about to be
erased.

Fixes #1282

* Don't let an exception escape Tree's destructor

Tree::~Tree() calls haltTree(), and halting may throw: a TryCatch with
catch_on_halt="true" ticks its catch child, and a tick is allowed to throw.
An exception leaving a destructor calls std::terminate, so destroying such a
tree aborted the process.

Mark the destructor noexcept and swallow the exception. There is no caller
left to report to.

Fixes #1281

* Apply pre-commit (clang-format)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Roll back a rejected load once, at the top-level call; simplify tests

- loadLastDocImpl is now only the top-level rollback point: <include> calls
  loadDocImpl directly, and on failure the call drops every document it
  opened (erase(doc_it, end())), so a valid included document no longer stays
  the "first document" whose main_tree_to_execute instantiateTree() reads.
  The snapshot also covers subtree_models and current_path, as listed in
  #1282, and is restored by move.
- ~Tree(): drop the redundant noexcept (destructors are implicitly noexcept)
  and report a throwing halt on stderr, as FinallyNode does, instead of
  swallowing it silently (also fixes bugprone-empty-catch).
- Tests: share TempSubdir/writeFile through test_helper.hpp (no getpid, which
  broke MSVC); move the destructor test next to the other catch_on_halt tests
  using lambda nodes and assert the catch child ran; add a first-document
  test; make the redefinition test go through a valid include of a rejected
  file (the old one passed on master); drop HaltStillRuns and
  RollbackKeepsEarlierTrees, which asserted nothing new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Cover a non-std exception thrown while halting in Tree::~Tree()

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Davide Faconti <davide.faconti@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants