Merge pull request #624 from Dmitry-Me/fixCrashWhenInsertingAfterItself

Fix crash when element is being inserted "after itself"
This commit is contained in:
Lee Thomason 2017-09-18 09:39:50 -07:00 committed by GitHub
commit 620cb1c55a
2 changed files with 35 additions and 0 deletions

View File

@ -911,6 +911,13 @@ XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
TIXMLASSERT( false );
return 0;
}
if ( afterThis == addThis ) {
// Current state: BeforeThis -> AddThis -> OneAfterAddThis
// Now AddThis must disappear from it's location and then
// reappear between BeforeThis and OneAfterAddThis.
// So just leave it where it is.
return addThis;
}
if ( afterThis->_next == 0 ) {
// The last node or the only node.

View File

@ -385,6 +385,34 @@ int main( int argc, const char ** argv )
doc.Print();
}
{
// This test is pre-test for the next one
// (where Element1 is inserted "after itself".
// This code didn't use to crash.
XMLDocument doc;
XMLElement* element1 = doc.NewElement("Element1");
XMLElement* element2 = doc.NewElement("Element2");
doc.InsertEndChild(element1);
doc.InsertEndChild(element2);
doc.InsertAfterChild(element2, element2);
doc.InsertAfterChild(element2, element2);
}
{
XMLDocument doc;
XMLElement* element1 = doc.NewElement("Element1");
XMLElement* element2 = doc.NewElement("Element2");
doc.InsertEndChild(element1);
doc.InsertEndChild(element2);
// This insertion "after itself"
// used to cause invalid memory access and crash
doc.InsertAfterChild(element1, element1);
doc.InsertAfterChild(element1, element1);
doc.InsertAfterChild(element2, element2);
doc.InsertAfterChild(element2, element2);
}
{
static const char* test = "<element>Text before.</element>";
XMLDocument doc;