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 )
{
if (Name) {
if (pName) {
NameLen = (pLen == -1)? strlen( pName ) : pLen ;
Name = (char *)malloc( NameLen+1 );
memcpy( Name, pName, NameLen );
Name[ NameLen ] = 0;
}
else {
Name = NULL;
NameLen = 0;
}
Type = jtNull;
Value = NULL;
Len = 0;
Type = jtNull;
Value = NULL;
Len = 0;
FirstChild = NULL;
LastChild = NULL;
Parent = NULL;
PrevPeer = NULL;
NextPeer = NULL;
PrevPeer = NULL;
NextPeer = NULL;
}
//---------------------------------------------------------------------------
@@ -45,19 +48,22 @@ CDataMember::CDataMember( CDataMember * pParent, const char * pName, const int p
memcpy( Name, pName, NameLen );
Name[ NameLen ] = 0;
}
else {
Name = NULL;
NameLen = 0;
}
Type = jtNull;
Value = NULL;
Len = 0;
Type = jtNull;
Value = NULL;
Len = 0;
FirstChild = NULL;
LastChild = NULL;
if (!pParent) {
Parent = NULL;
PrevPeer = NULL;
NextPeer = NULL;
Parent = NULL;
PrevPeer = NULL;
NextPeer = NULL;
}
else {
// 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
Parent->Len++;
if (!Parent->FirstChild) {
PrevPeer = NULL;
NextPeer = NULL;
Parent->FirstChild = this;
Parent->LastChild = this;
}
else {
PrevPeer = Parent->LastChild;
Parent->LastChild->NextPeer = this;
NextPeer = NULL;
Parent->LastChild = this;
}
}
@@ -84,13 +95,21 @@ CDataMember::CDataMember( CDataMember * pParent, const char * pName, const int p
CDataMember::~CDataMember()
{
// Remove from parent
if (Parent)
if (Parent) {
Parent->Len--;
if (this == Parent->LastChild) {
Parent->LastChild = PrevPeer;
if (this == Parent->LastChild) {
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
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 )
{
Clear();