Important Bug fix:

- Fixed memory errors and leaks in DataTreeCore:
  - Initial values not set correctly for Name, Next/PrevPeer
  - Correctly set Last/FirstChild & Prev/NextPeer references on delete
  - Added methods SetValuePtr()
- Fixed memory errors and leaks in JSONparseCore:
  - Set set string/int/float values by passing pointers, ie. SetValuePtr()
This commit is contained in:
Charl Wentzel
2018-11-18 12:47:33 +02:00
parent 2382ff4527
commit 85a811c19f
3 changed files with 58 additions and 24 deletions

View File

@@ -16,24 +16,27 @@
CDataMember::CDataMember( const char * pName, const int pLen ) CDataMember::CDataMember( const char * pName, const int pLen )
{ {
if (Name) { if (pName) {
NameLen = (pLen == -1)? strlen( pName ) : pLen ; NameLen = (pLen == -1)? strlen( pName ) : pLen ;
Name = (char *)malloc( NameLen+1 ); Name = (char *)malloc( NameLen+1 );
memcpy( Name, pName, NameLen ); memcpy( Name, pName, NameLen );
Name[ NameLen ] = 0; Name[ NameLen ] = 0;
} }
else {
Name = NULL;
NameLen = 0;
}
Type = jtNull; Type = jtNull;
Value = NULL;
Value = NULL; Len = 0;
Len = 0;
FirstChild = NULL; FirstChild = NULL;
LastChild = NULL; LastChild = NULL;
Parent = NULL; Parent = NULL;
PrevPeer = NULL; PrevPeer = NULL;
NextPeer = NULL; NextPeer = NULL;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -45,19 +48,22 @@ CDataMember::CDataMember( CDataMember * pParent, const char * pName, const int p
memcpy( Name, pName, NameLen ); memcpy( Name, pName, NameLen );
Name[ NameLen ] = 0; Name[ NameLen ] = 0;
} }
else {
Name = NULL;
NameLen = 0;
}
Type = jtNull; Type = jtNull;
Value = NULL;
Value = NULL; Len = 0;
Len = 0;
FirstChild = NULL; FirstChild = NULL;
LastChild = NULL; LastChild = NULL;
if (!pParent) { if (!pParent) {
Parent = NULL; Parent = NULL;
PrevPeer = NULL; PrevPeer = NULL;
NextPeer = NULL; NextPeer = NULL;
} }
else { else {
// Clear/reset parent if not object // Clear/reset parent if not object
@@ -69,12 +75,17 @@ CDataMember::CDataMember( CDataMember * pParent, const char * pName, const int p
// Insert into Parent & Peer lists // Insert into Parent & Peer lists
Parent->Len++; Parent->Len++;
if (!Parent->FirstChild) { if (!Parent->FirstChild) {
PrevPeer = NULL;
NextPeer = NULL;
Parent->FirstChild = this; Parent->FirstChild = this;
Parent->LastChild = this; Parent->LastChild = this;
} }
else { else {
PrevPeer = Parent->LastChild; PrevPeer = Parent->LastChild;
Parent->LastChild->NextPeer = this; Parent->LastChild->NextPeer = this;
NextPeer = NULL;
Parent->LastChild = this; Parent->LastChild = this;
} }
} }
@@ -84,13 +95,21 @@ CDataMember::CDataMember( CDataMember * pParent, const char * pName, const int p
CDataMember::~CDataMember() CDataMember::~CDataMember()
{ {
// Remove from parent // Remove from parent
if (Parent) if (Parent) {
Parent->Len--; Parent->Len--;
if (this == Parent->LastChild) { if (this == Parent->LastChild) {
Parent->LastChild = PrevPeer; Parent->LastChild = PrevPeer;
}
if (this == Parent->FirstChild) {
Parent->FirstChild = NextPeer;
}
}
if (PrevPeer) {
PrevPeer->NextPeer = NextPeer;
}
if (NextPeer) {
NextPeer->PrevPeer = PrevPeer;
} }
PrevPeer->NextPeer = NextPeer;
NextPeer->PrevPeer = PrevPeer;
// Destroy value/children // Destroy value/children
Clear(); Clear();
@@ -332,6 +351,20 @@ bool CDataMember::Delete( const char * Path )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CDataMember::SetValuePtr( EDataType pType, const char * pValue, int pLen )
{
Clear();
Type = pType;
if ((pType == jtString) || (pType == jtFloat) || (pType == jtInt) || (pType == jtBool))
{
Len = (pLen == -1)? strlen(pValue) : pLen;
Value = (char*)pValue;
}
return true;
}
//---------------------------------------------------------------------------
bool CDataMember::SetValue( EDataType pType, const char * pValue, int pLen ) bool CDataMember::SetValue( EDataType pType, const char * pValue, int pLen )
{ {
Clear(); Clear();

View File

@@ -41,7 +41,8 @@ class CDataMember
CDataMember * CreateChild( const char * Name, const int Len = -1 ); CDataMember * CreateChild( const char * Name, const int Len = -1 );
// Set Member value // Set Member value
bool SetValue( EDataType Type, const char * Value = NULL, int Len = -1 ); bool SetValue( EDataType Type, const char * Value = NULL, int Len = -1 );
bool SetValuePtr( EDataType pType, const char * pValue, int pLen = -1 );
public: public:
CDataMember( const char * pName = NULL, const int pLen = -1 ); CDataMember( const char * pName = NULL, const int pLen = -1 );

View File

@@ -681,7 +681,7 @@ bool CJSONparse::ParseString( CDataMember * Member )
} }
// Set string // Set string
Member->SetValue( jtString, Value, Len ); Member->SetValuePtr( jtString, Value, Len );
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -712,7 +712,7 @@ bool CJSONparse::ParsePrimitive( CDataMember * Member )
// Check for primitive values // Check for primitive values
if ((Len == 4) && !strncasecmp( Mark, "null", 4 )) { if ((Len == 4) && !strncasecmp( Mark, "null", 4 )) {
Member->SetValue( jtNull, NULL, -1 ); Member->SetValue( jtNull );
} }
else if ((Len == 4) && !strncasecmp( Mark, "true", 4 )) { else if ((Len == 4) && !strncasecmp( Mark, "true", 4 )) {
Member->SetValue( jtBool, "1" ); Member->SetValue( jtBool, "1" );
@@ -727,7 +727,7 @@ bool CJSONparse::ParsePrimitive( CDataMember * Member )
Value = (char*)malloc( Len+1 ); Value = (char*)malloc( Len+1 );
memcpy( Value, Mark, Len ); memcpy( Value, Mark, Len );
Value[Len] = 0; Value[Len] = 0;
Member->SetValue( jtInt, Value, Len ); Member->SetValuePtr( jtInt, Value, Len );
} }
else { else {
// Try conversion to float // Try conversion to float
@@ -736,7 +736,7 @@ bool CJSONparse::ParsePrimitive( CDataMember * Member )
Value = (char*)malloc( Len+1 ); Value = (char*)malloc( Len+1 );
memcpy( Value, Mark, Len ); memcpy( Value, Mark, Len );
Value[Len] = 0; Value[Len] = 0;
Member->SetValue( jtFloat, Value, Len ); Member->SetValuePtr( jtFloat, Value, Len );
} }
else { else {
Error = true; Error = true;