Skip to content

Commit

Permalink
fix error string memory errors
Browse files Browse the repository at this point in the history
  • Loading branch information
leethomason committed Sep 5, 2016
1 parent 2e14517 commit 584af57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
24 changes: 14 additions & 10 deletions tinyxml2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1829,8 +1829,6 @@ XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
_processEntities( processEntities ),
_errorID(XML_SUCCESS),
_whitespace( whitespace ),
_errorStr1( 0 ),
_errorStr2( 0 ),
_charBuffer( 0 )
{
// avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+)
Expand All @@ -1852,8 +1850,8 @@ void XMLDocument::Clear()
const bool hadError = Error();
#endif
_errorID = XML_SUCCESS;
_errorStr1 = 0;
_errorStr2 = 0;
_errorStr1.Reset();
_errorStr2.Reset();

delete [] _charBuffer;
_charBuffer = 0;
Expand Down Expand Up @@ -2112,8 +2110,14 @@ void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
{
TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
_errorID = error;
_errorStr1 = str1;
_errorStr2 = str2;

_errorStr1.Reset();
_errorStr2.Reset();

if (str1)
_errorStr1.SetStr(str1);
if (str2)
_errorStr2.SetStr(str2);
}

const char* XMLDocument::ErrorName() const
Expand All @@ -2131,11 +2135,11 @@ void XMLDocument::PrintError() const
char buf1[LEN] = { 0 };
char buf2[LEN] = { 0 };

if ( _errorStr1 ) {
TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
if ( !_errorStr1.Empty() ) {
TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1.GetStr() );
}
if ( _errorStr2 ) {
TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
if ( !_errorStr2.Empty() ) {
TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2.GetStr() );
}

// Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that
Expand Down
20 changes: 10 additions & 10 deletions tinyxml2.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ class StrPair
NEEDS_NEWLINE_NORMALIZATION = 0x02,
NEEDS_WHITESPACE_COLLAPSING = 0x04,

TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
ATTRIBUTE_NAME = 0,
ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
COMMENT = NEEDS_NEWLINE_NORMALIZATION
ATTRIBUTE_NAME = 0,
ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
COMMENT = NEEDS_NEWLINE_NORMALIZATION
};

StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Expand Down Expand Up @@ -164,9 +164,9 @@ class StrPair
char* ParseName( char* in );

void TransferTo( StrPair* other );
void Reset();

private:
void Reset();
void CollapseWhitespace();

enum {
Expand Down Expand Up @@ -1759,11 +1759,11 @@ class TINYXML2_LIB XMLDocument : public XMLNode

/// Return a possibly helpful diagnostic location or string.
const char* GetErrorStr1() const {
return _errorStr1;
return _errorStr1.GetStr();
}
/// Return a possibly helpful secondary diagnostic location or string.
const char* GetErrorStr2() const {
return _errorStr2;
return _errorStr2.GetStr();
}
/// If there is an error, print it to stdout.
void PrintError() const;
Expand All @@ -1789,8 +1789,8 @@ class TINYXML2_LIB XMLDocument : public XMLNode
bool _processEntities;
XMLError _errorID;
Whitespace _whitespace;
const char* _errorStr1;
const char* _errorStr2;
mutable StrPair _errorStr1;
mutable StrPair _errorStr2;
char* _charBuffer;

MemPoolT< sizeof(XMLElement) > _elementPool;
Expand Down

0 comments on commit 584af57

Please sign in to comment.