Merge branch 'master' into errorstr

This commit is contained in:
Lee Thomason 2017-10-11 10:57:52 -07:00
commit d556e6df9d
5 changed files with 82 additions and 44 deletions

View File

@ -4,6 +4,13 @@ rebuild: clean all
xmltest: xmltest.cpp libtinyxml2.a xmltest: xmltest.cpp libtinyxml2.a
effc:
gcc -Werror -Wall -Wextra -Wshadow -Wpedantic -Wformat-nonliteral \
-Wformat-security -Wswitch-default -Wuninitialized -Wundef \
-Wpointer-arith -Woverloaded-virtual -Wctor-dtor-privacy \
-Wnon-virtual-dtor -Woverloaded-virtual -Wsign-promo \
-Wno-unused-parameter -Weffc++ xmltest.cpp tinyxml2.cpp -o xmltest
clean: clean:
$(RM) *.o xmltest libtinyxml2.a $(RM) *.o xmltest libtinyxml2.a

View File

@ -40,7 +40,7 @@ distribution.
*/ */
#include <tinyxml2.h> #include "../tinyxml2.h"
#include <iostream> #include <iostream>
#if defined (_MSC_VER) #if defined (_MSC_VER)

View File

@ -742,6 +742,7 @@ bool XMLDocument::Accept( XMLVisitor* visitor ) const
XMLNode::XMLNode( XMLDocument* doc ) : XMLNode::XMLNode( XMLDocument* doc ) :
_document( doc ), _document( doc ),
_parent( 0 ), _parent( 0 ),
_value(),
_parseLineNum( 0 ), _parseLineNum( 0 ),
_firstChild( 0 ), _lastChild( 0 ), _firstChild( 0 ), _lastChild( 0 ),
_prev( 0 ), _next( 0 ), _prev( 0 ), _next( 0 ),
@ -2001,9 +2002,16 @@ XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) :
_processEntities( processEntities ), _processEntities( processEntities ),
_errorID(XML_SUCCESS), _errorID(XML_SUCCESS),
_whitespaceMode( whitespaceMode ), _whitespaceMode( whitespaceMode ),
_errorStr1(),
_errorStr2(),
_errorLineNum( 0 ), _errorLineNum( 0 ),
_charBuffer( 0 ), _charBuffer( 0 ),
_parseCurLineNum( 0 ) _parseCurLineNum( 0 ),
_unlinked(),
_elementPool(),
_attributePool(),
_textPool(),
_commentPool()
{ {
// avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+) // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+)
_document = this; _document = this;
@ -2363,12 +2371,14 @@ void XMLDocument::Parse()
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
_elementJustOpened( false ), _elementJustOpened( false ),
_stack(),
_firstElement( true ), _firstElement( true ),
_fp( file ), _fp( file ),
_depth( depth ), _depth( depth ),
_textDepth( -1 ), _textDepth( -1 ),
_processEntities( true ), _processEntities( true ),
_compactMode( compact ) _compactMode( compact ),
_buffer()
{ {
for( int i=0; i<ENTITY_RANGE; ++i ) { for( int i=0; i<ENTITY_RANGE; ++i ) {
_entityFlag[i] = false; _entityFlag[i] = false;

View File

@ -192,10 +192,11 @@ template <class T, int INITIAL_SIZE>
class DynArray class DynArray
{ {
public: public:
DynArray() { DynArray() :
_mem = _pool; _mem( _pool ),
_allocated = INITIAL_SIZE; _allocated( INITIAL_SIZE ),
_size = 0; _size( 0 )
{
} }
~DynArray() { ~DynArray() {
@ -333,7 +334,7 @@ template< int ITEM_SIZE >
class MemPoolT : public MemPool class MemPoolT : public MemPool
{ {
public: public:
MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {} MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
~MemPoolT() { ~MemPoolT() {
Clear(); Clear();
} }
@ -1211,7 +1212,7 @@ public:
private: private:
enum { BUF_SIZE = 200 }; enum { BUF_SIZE = 200 };
XMLAttribute() : _parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {} XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
virtual ~XMLAttribute() {} virtual ~XMLAttribute() {}
XMLAttribute( const XMLAttribute& ); // not supported XMLAttribute( const XMLAttribute& ); // not supported
@ -1953,16 +1954,13 @@ class TINYXML2_LIB XMLHandle
{ {
public: public:
/// Create a handle from any node (at any depth of the tree.) This can be a null pointer. /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
XMLHandle( XMLNode* node ) { XMLHandle( XMLNode* node ) : _node( node ) {
_node = node;
} }
/// Create a handle from a node. /// Create a handle from a node.
XMLHandle( XMLNode& node ) { XMLHandle( XMLNode& node ) : _node( &node ) {
_node = &node;
} }
/// Copy constructor /// Copy constructor
XMLHandle( const XMLHandle& ref ) { XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
_node = ref._node;
} }
/// Assignment /// Assignment
XMLHandle& operator=( const XMLHandle& ref ) { XMLHandle& operator=( const XMLHandle& ref ) {
@ -2036,14 +2034,11 @@ private:
class TINYXML2_LIB XMLConstHandle class TINYXML2_LIB XMLConstHandle
{ {
public: public:
XMLConstHandle( const XMLNode* node ) { XMLConstHandle( const XMLNode* node ) : _node( node ) {
_node = node;
} }
XMLConstHandle( const XMLNode& node ) { XMLConstHandle( const XMLNode& node ) : _node( &node ) {
_node = &node;
} }
XMLConstHandle( const XMLConstHandle& ref ) { XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
_node = ref._node;
} }
XMLConstHandle& operator=( const XMLConstHandle& ref ) { XMLConstHandle& operator=( const XMLConstHandle& ref ) {
@ -2258,6 +2253,10 @@ private:
bool _restrictedEntityFlag[ENTITY_RANGE]; bool _restrictedEntityFlag[ENTITY_RANGE];
DynArray< char, 20 > _buffer; DynArray< char, 20 > _buffer;
// Prohibit cloning, intentionally not implemented
XMLPrinter( const XMLPrinter& );
XMLPrinter& operator=( const XMLPrinter& );
}; };

View File

@ -1035,8 +1035,9 @@ int main( int argc, const char ** argv )
if ( textfile ) if ( textfile )
{ {
XMLPrinter streamer( textfile ); XMLPrinter streamer( textfile );
psg->Accept( &streamer ); bool acceptResult = psg->Accept( &streamer );
fclose( textfile ); fclose( textfile );
XMLTest( "Entity transformation: Accept", true, acceptResult );
} }
textfile = fopen( textFilePath, "r" ); textfile = fopen( textFilePath, "r" );
@ -1332,7 +1333,8 @@ int main( int argc, const char ** argv )
XMLTest( "Parse before deep cloning sub element", false, doc.Error() ); XMLTest( "Parse before deep cloning sub element", false, doc.Error() );
const XMLElement* subElement = doc.FirstChildElement("root")->FirstChildElement("child2"); const XMLElement* subElement = doc.FirstChildElement("root")->FirstChildElement("child2");
subElement->Accept(&printer1); bool acceptResult = subElement->Accept(&printer1);
XMLTest( "Accept before deep cloning", true, acceptResult );
XMLNode* clonedSubElement = subElement->DeepClone(&doc2); XMLNode* clonedSubElement = subElement->DeepClone(&doc2);
doc2.InsertFirstChild(clonedSubElement); doc2.InsertFirstChild(clonedSubElement);
@ -1648,7 +1650,8 @@ int main( int argc, const char ** argv )
XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child"); XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child");
XMLPrinter printer; XMLPrinter printer;
ele->Accept( &printer ); bool acceptResult = ele->Accept( &printer );
XMLTest( "Accept of sub-element", true, acceptResult );
XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false ); XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false );
} }
@ -1730,7 +1733,8 @@ int main( int argc, const char ** argv )
XMLElement* two = doc.RootElement()->FirstChildElement("two"); XMLElement* two = doc.RootElement()->FirstChildElement("two");
two->InsertFirstChild(subtree); two->InsertFirstChild(subtree);
XMLPrinter printer1(0, true); XMLPrinter printer1(0, true);
doc.Accept(&printer1); bool acceptResult = doc.Accept(&printer1);
XMLTest("Move node from within <one> to <two> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr()); XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
doc.Parse(xml); doc.Parse(xml);
@ -1739,7 +1743,8 @@ int main( int argc, const char ** argv )
two = doc.RootElement()->FirstChildElement("two"); two = doc.RootElement()->FirstChildElement("two");
doc.RootElement()->InsertAfterChild(two, subtree); doc.RootElement()->InsertAfterChild(two, subtree);
XMLPrinter printer2(0, true); XMLPrinter printer2(0, true);
doc.Accept(&printer2); acceptResult = doc.Accept(&printer2);
XMLTest("Move node from within <one> after <two> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false); XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false);
doc.Parse(xml); doc.Parse(xml);
@ -1748,7 +1753,8 @@ int main( int argc, const char ** argv )
subtree = one->FirstChildElement("subtree"); subtree = one->FirstChildElement("subtree");
doc.RootElement()->InsertAfterChild(one, subtree); doc.RootElement()->InsertAfterChild(one, subtree);
XMLPrinter printer3(0, true); XMLPrinter printer3(0, true);
doc.Accept(&printer3); acceptResult = doc.Accept(&printer3);
XMLTest("Move node from within <one> after <one> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false); XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false);
doc.Parse(xml); doc.Parse(xml);
@ -1757,7 +1763,8 @@ int main( int argc, const char ** argv )
two = doc.RootElement()->FirstChildElement("two"); two = doc.RootElement()->FirstChildElement("two");
doc.RootElement()->InsertEndChild(subtree); doc.RootElement()->InsertEndChild(subtree);
XMLPrinter printer4(0, true); XMLPrinter printer4(0, true);
doc.Accept(&printer4); acceptResult = doc.Accept(&printer4);
XMLTest("Move node from within <one> after <two> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false); XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false);
} }
@ -1881,7 +1888,8 @@ int main( int argc, const char ** argv )
XMLDocument doc; XMLDocument doc;
XMLElement* newElement = doc.NewElement( "printme" ); XMLElement* newElement = doc.NewElement( "printme" );
XMLPrinter printer; XMLPrinter printer;
newElement->Accept( &printer ); bool acceptResult = newElement->Accept( &printer );
XMLTest( "printme - Accept()", true, acceptResult );
// Delete the node to avoid possible memory leak report in debug output // Delete the node to avoid possible memory leak report in debug output
doc.DeleteNode( newElement ); doc.DeleteNode( newElement );
} }
@ -2015,22 +2023,33 @@ int main( int argc, const char ** argv )
XMLElement* playlist = doc.FirstChildElement("playlist"); XMLElement* playlist = doc.FirstChildElement("playlist");
XMLTest("Crash bug parsing", true, playlist != 0); XMLTest("Crash bug parsing", true, playlist != 0);
tinyxml2::XMLElement* entry = playlist->FirstChildElement("entry"); {
const char* elementName = "entry";
XMLElement* entry = playlist->FirstChildElement(elementName);
XMLTest("Crash bug parsing", true, entry != 0); XMLTest("Crash bug parsing", true, entry != 0);
while (entry) { while (entry) {
tinyxml2::XMLElement* todelete = entry; XMLElement* todelete = entry;
entry = entry->NextSiblingElement("entry"); entry = entry->NextSiblingElement(elementName);
playlist->DeleteChild(todelete); playlist->DeleteChild(todelete);
}; }
tinyxml2::XMLElement* blank = playlist->FirstChildElement("blank"); entry = playlist->FirstChildElement(elementName);
XMLTest("Crash bug parsing", true, entry == 0);
}
{
const char* elementName = "blank";
XMLElement* blank = playlist->FirstChildElement(elementName);
XMLTest("Crash bug parsing", true, blank != 0);
while (blank) { while (blank) {
tinyxml2::XMLElement* todelete = blank; XMLElement* todelete = blank;
blank = blank->NextSiblingElement("blank"); blank = blank->NextSiblingElement(elementName);
playlist->DeleteChild(todelete); playlist->DeleteChild(todelete);
}; }
XMLTest("Crash bug parsing", true, blank == 0);
}
tinyxml2::XMLPrinter printer; tinyxml2::XMLPrinter printer;
playlist->Accept(&printer); const bool acceptResult = playlist->Accept(&printer);
XMLTest("Crash bug parsing - Accept()", true, acceptResult);
printf("%s\n", printer.CStr()); printf("%s\n", printer.CStr());
// No test; it only need to not crash. // No test; it only need to not crash.
@ -2046,6 +2065,8 @@ int main( int argc, const char ** argv )
{ {
struct TestUtil: XMLVisitor struct TestUtil: XMLVisitor
{ {
TestUtil() : str() {}
void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine) void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine)
{ {
XMLDocument doc; XMLDocument doc;
@ -2119,7 +2140,8 @@ int main( int argc, const char ** argv )
void TestDocLines(const char *testString, XMLDocument &doc, const char *expectedLines) void TestDocLines(const char *testString, XMLDocument &doc, const char *expectedLines)
{ {
str.Clear(); str.Clear();
doc.Accept(this); const bool acceptResult = doc.Accept(this);
XMLTest(testString, true, acceptResult);
str.Push(0); str.Push(0);
XMLTest(testString, expectedLines, str.Mem()); XMLTest(testString, expectedLines, str.Mem());
} }