From 7e744771adc3b6ce51668434923d1deeb053a120 Mon Sep 17 00:00:00 2001 From: Ant Mitchell Date: Tue, 24 Mar 2015 14:33:28 +0000 Subject: [PATCH 1/3] Issue299 - Allow Accept() to work with element trees that are not linked in to the owning document. --- tinyxml2.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 57a4840..a22f251 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -2297,8 +2297,12 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc ) bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) { - const XMLElement* parentElem = element.Parent()->ToElement(); - bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode; + const XMLElement* parentElem = NULL; + if ( element.Parent() ) + { + parentElem = element.Parent()->ToElement(); + } + bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode; OpenElement( element.Name(), compactMode ); while ( attribute ) { PushAttribute( attribute->Name(), attribute->Value() ); From 148cc1a0a57c8ab906fc8dbf987a4096f1d09e76 Mon Sep 17 00:00:00 2001 From: Ant Mitchell Date: Tue, 24 Mar 2015 15:12:35 +0000 Subject: [PATCH 2/3] Issue 299 - test for code changes --- xmltest.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/xmltest.cpp b/xmltest.cpp index 243628c..06ac9fc 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1425,6 +1425,14 @@ int main( int argc, const char ** argv ) XMLPrinter printer; doc.Print( &printer ); } + { + // Issue 299. Can print elements that are not linked in. + // Will crash if issue not fixed. + XMLDocument doc; + XMLElement* newElement = doc.NewElement( "printme" ); + XMLPrinter printer; + newElement->Accept( &printer ); + } // ----------- Performance tracking -------------- { From 3ccb1cec29b5b745d48d36d5415e6ba6e8d4c6ec Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Tue, 24 Mar 2015 11:17:44 -0700 Subject: [PATCH 3/3] minor formatting cleanup --- tinyxml2.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index a22f251..6a7ca07 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -2297,12 +2297,11 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc ) bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) { - const XMLElement* parentElem = NULL; - if ( element.Parent() ) - { - parentElem = element.Parent()->ToElement(); + const XMLElement* parentElem = NULL; + if ( element.Parent() ) { + parentElem = element.Parent()->ToElement(); } - bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode; + bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode; OpenElement( element.Name(), compactMode ); while ( attribute ) { PushAttribute( attribute->Name(), attribute->Value() );