Conversation
…eethomason#1031) XMLPrinter::PushText(text, cdata) forwarded text straight to Write(text) (which is Write(text, strlen(text))) and PrintString(text, true), neither of which guarded a null pointer. A caller passing nullptr segfaulted in PrintString when it dereferenced p = text. Reject a null text pointer early -- TIXMLASSERT matches the precondition style used at PushAttribute:2773, and the explicit return protects release builds where assertions are stripped. Verified under clang + -fsanitize=address,undefined against master 8224e42: without the fix, the reporter's PoC hits 'tinyxml2.cpp:2681:17: runtime error: load of null pointer of type ''const char''' and 'AddressSanitizer: SEGV on unknown address 0x000000000000' with a stack of PrintString <- PushText <- main. With the fix the same PoC returns cleanly and the printer emits '<root/>' as expected. Multi-element regression (PushText + CDATA) round-trips correctly. Fixes: leethomason#1031 Reported-by: wangziqi520 (github.com/wangziqi520) Signed-off-by: Brandon Barrante <aetherai@aethersystems.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1031.
Small one.
XMLPrinter::PushText(const char* text, bool cdata)(tinyxml2.cpp:2868) passestextstraight through toWrite( text )(which isWrite(text, strlen(text))pertinyxml2.h:2342) and toPrintString( text, true )(which dereferencesp = textattinyxml2.cpp:2681). Neither guards against a nulltext; a caller passingnullptr(fuzz-shaped or otherwise malformed input) segfaults insidePrintString.The reporter's ASan run in #1031 shows the OOB read; a minimal repro under clang +
-fsanitize=address,undefinedreproduces cleanly against master8224e427:Baseline (RED):
tinyxml2.cpp:2681:17: runtime error: load of null pointer of type 'const char'
AddressSanitizer: SEGV on unknown address 0x000000000000
#0 tinyxml2::XMLPrinter::PrintString(char const*, bool) tinyxml2.cpp:2681:17
#1 tinyxml2::XMLPrinter::PushText(char const*, bool) tinyxml2.cpp:2879:9
#2 main repro.cpp:9:7
Post-fix (GREEN): same repro runs cleanly,
PushTextbecomes a no-op onnullptr,<root/>is emitted correctly.Fix
Reject a null
textearly —TIXMLASSERTfires in debug builds (matches the project's precondition style atPushAttribute:2773) and an explicitreturnprotects release builds:Verification
<root/>as expected.PushText("hello world")and CDATA text round-trips correctly to the expected 102-byte serialization.Diff
+6/-0 in one file, one function.
Base
Applied on top of master
8224e427b655b83dae5e2298f1e6919523a78737.Scope note
PushComment,PushDeclaration,PushUnknown, and the two-argPushAttribute(name, value)have the same missing-guard shape (they all forward toWrite(const char*)which callsstrlen). Those aren't touched here to keep the diff scoped to the reported bug, but they're worth a follow-up — happy to send a wider patch if that fits maintainer taste.Credit
Reported by @wangziqi520 in #1031 with an AddressSanitizer trace. The report pins the exact function and root cause; the fix is the minimal guard that resolves it.
Note on AI assistance
I used an AI assistant to help trace the crash and draft this write-up. The code change and every claim above I verified myself against the built binary.