1
0
mirror of https://github.com/leethomason/tinyxml2.git synced 2026-07-29 23:22:59 +04:00

Compare commits

..

7 Commits

Author SHA1 Message Date
Lee Thomason 5ce9f9a3a7 fix assert logic 2026-05-23 16:10:29 -07:00
Lee Thomason 682fda289f add comment 2026-05-23 16:04:26 -07:00
Lee Thomason e6a464942f Merge branch 'master' of github.com:IMBIGFISH4/xml2 into IMBIGFISH4-master 2026-05-23 16:02:58 -07:00
Lee Thomason 98137e6936 Merge branch 'master' of github.com:leethomason/tinyxml2 2026-03-09 17:53:04 -07:00
zhthhh aba13c50bf Fix issue #1048: Add null pointer check in XMLDocument::DeleteNode (#1057)
- Add null pointer check in XMLDocument::DeleteNode() method
- Add test case to verify null pointer handling
- Prevents segmentation fault when calling DeleteNode(nullptr)
2026-03-09 17:32:22 -07:00
sbaluja e48a1310fe Add null check for format in TIXML_VSCPRINTF (#1064) 2026-03-09 17:31:36 -07:00
Michael Mishin fc99428a82 Improved XMLNode::~XMLNode perfomance
XMLNode::~XMLNode uses interface method DeleteChildren(), which
iteratively removes the first child until the list is empty. Each single
removal performs additional checks and keeps _firstChild and _lastChild
links in a consistent state. This leads to a performance penalty.

The proposed patch solves this problem by iterating the list of the
children and directly destroying them without keeping links in a
consistent state.

The performance boost we gained on xmltest is:
- 3% for my laptop (x86_64)
- 9% for bananapi f3 board (riscv64)
2025-07-29 16:30:58 +03:00
2 changed files with 25 additions and 5 deletions
+17 -3
View File
@@ -114,6 +114,9 @@ distribution.
#define TIXML_VSNPRINTF vsnprintf #define TIXML_VSNPRINTF vsnprintf
static inline int TIXML_VSCPRINTF( const char* format, va_list va ) static inline int TIXML_VSCPRINTF( const char* format, va_list va )
{ {
if (!format) {
return 0;
}
int len = vsnprintf( 0, 0, format, va ); int len = vsnprintf( 0, 0, format, va );
TIXMLASSERT( len >= 0 ); TIXMLASSERT( len >= 0 );
return len; return len;
@@ -831,7 +834,17 @@ XMLNode::XMLNode( XMLDocument* doc ) :
XMLNode::~XMLNode() XMLNode::~XMLNode()
{ {
DeleteChildren(); // Fast path: this node is dying, so maintaining _firstChild/_lastChild and
// sibling _prev/_next links is unnecessary. Only _parent must be zeroed to
// satisfy the MarkInUse assertion inside DeleteNode.
XMLNode *currentChild = _firstChild;
while (currentChild != NULL) {
XMLNode *next = currentChild->_next;
currentChild->_parent = 0;
DeleteNode(currentChild);
currentChild = next;
}
if ( _parent ) { if ( _parent ) {
_parent->Unlink( this ); _parent->Unlink( this );
} }
@@ -2345,8 +2358,9 @@ static FILE* callfopen( const char* filepath, const char* mode )
} }
void XMLDocument::DeleteNode( XMLNode* node ) { void XMLDocument::DeleteNode( XMLNode* node ) {
TIXMLASSERT( node ); if(node == 0) {
TIXMLASSERT(node->_document == this ); return; // check for null pointer
}
if (node->_parent) { if (node->_parent) {
node->_parent->DeleteChild( node ); node->_parent->DeleteChild( node );
} }
+6
View File
@@ -2025,6 +2025,12 @@ int main( int argc, const char ** argv )
XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error()); XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", true, 0 == doc.RootElement()->FirstChildElement()->GetText()); XMLTest("Pedantic whitespace", true, 0 == doc.RootElement()->FirstChildElement()->GetText());
} }
//Check the robustness of the DeleteNode function in handling null pointers.
{
XMLDocument doc;
doc.DeleteNode(nullptr);
XMLTest("DeleteNode with null pointer", true, doc.Error() == XML_SUCCESS);
}
// Check sample xml can be parsed with pedantic mode // Check sample xml can be parsed with pedantic mode
{ {