mirror of https://github.com/AxioDL/tinyxml2.git
Merge pull request #624 from Dmitry-Me/fixCrashWhenInsertingAfterItself
Fix crash when element is being inserted "after itself"
This commit is contained in:
commit
620cb1c55a
|
@ -911,6 +911,13 @@ XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
|
||||||
TIXMLASSERT( false );
|
TIXMLASSERT( false );
|
||||||
return 0;
|
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 ) {
|
if ( afterThis->_next == 0 ) {
|
||||||
// The last node or the only node.
|
// The last node or the only node.
|
||||||
|
|
28
xmltest.cpp
28
xmltest.cpp
|
@ -385,6 +385,34 @@ int main( int argc, const char ** argv )
|
||||||
doc.Print();
|
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>";
|
static const char* test = "<element>Text before.</element>";
|
||||||
XMLDocument doc;
|
XMLDocument doc;
|
||||||
|
|
Loading…
Reference in New Issue