Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Do not try to allocate memory on de-serialization when there is no need
Browse files Browse the repository at this point in the history
to do so.
  • Loading branch information
Shumaro committed Feb 14, 2020
1 parent 9708816 commit 318c12c
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/lib/support/SerializationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,18 +877,29 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel

case SerializedFieldTypeUTF8String:
{
char *dst = NULL;
// TLV Strings are not null terminated
uint32_t length = aReader.GetLength() + 1;

dst = (char *)memMgmt->mem_alloc(length);
VerifyOrExit(dst != NULL, err = WEAVE_ERROR_NO_MEMORY);
if (length > 1)
{
char *dst = NULL;

err = aReader.GetString(dst, length);
SuccessOrExit(err);
dst = (char *)memMgmt->mem_alloc(length);
VerifyOrExit(dst != NULL, err = WEAVE_ERROR_NO_MEMORY);

err = aReader.GetString(dst, length);
SuccessOrExit(err);

LogReadWrite("%s utf8string '%s' allocating %d bytes at %p", "R", dst, length, dst);

*static_cast<char**>(aStructureData) = dst;
}

else
{
*static_cast<char**>(aStructureData) = "";
}

LogReadWrite("%s utf8string '%s' allocating %d bytes at %p", "R", dst, length, dst);
*static_cast<char**>(aStructureData) = dst;
break;
}

Expand All @@ -897,11 +908,14 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
SerializedByteString byteString;
byteString.mLen = aReader.GetLength();

byteString.mBuf = static_cast<uint8_t *>(memMgmt->mem_alloc(byteString.mLen));
VerifyOrExit(byteString.mBuf != NULL, err = WEAVE_ERROR_NO_MEMORY);
aReader.GetBytes(byteString.mBuf, byteString.mLen);
if (byteString.mLen > 0)
{
byteString.mBuf = static_cast<uint8_t *>(memMgmt->mem_alloc(byteString.mLen));
VerifyOrExit(byteString.mBuf != NULL, err = WEAVE_ERROR_NO_MEMORY);
aReader.GetBytes(byteString.mBuf, byteString.mLen);

LogReadWrite("%s bytestring allocated %d bytes at %p", "R", byteString.mLen, byteString.mBuf);
LogReadWrite("%s bytestring allocated %d bytes at %p", "R", byteString.mLen, byteString.mBuf);
}
*static_cast<SerializedByteString *>(aStructureData) = byteString;
break;
}
Expand Down Expand Up @@ -1300,8 +1314,9 @@ WEAVE_ERROR DeallocateDeserializedArray(void *aArrayData,
{
LogReadWrite("%s Freeing array of primitive type at 0x%x", "R", array->mElementBuffer);

// The elements are of a primitive type, we can free the array now.
memMgmt->mem_free(array->mElementBuffer);
// The elements are of a primitive type, we can free the array now if not empty
if(array->mNumElements > 0)
memMgmt->mem_free(array->mElementBuffer);
}
else
{
Expand All @@ -1315,8 +1330,9 @@ WEAVE_ERROR DeallocateDeserializedArray(void *aArrayData,

LogReadWrite("%s Freeing array of structures at 0x%x", "R", array->mElementBuffer);

// Free the array now.
memMgmt->mem_free(array->mElementBuffer);
// Free the array now if not empty
if(array->mNumElements > 0)
memMgmt->mem_free(array->mElementBuffer);
}

exit:
Expand Down Expand Up @@ -1379,8 +1395,9 @@ WEAVE_ERROR DeallocateDeserializedStructure(void *aStructureData,

LogReadWrite("%s Freeing UTF8String '%s' at 0x%x", "R", str, str);

// Go ahead and free it here.
memMgmt->mem_free(str);
// Go ahead and free it here if not empty.
if (!strcmp(str, ""))
memMgmt->mem_free(str);
break;
}

Expand Down

0 comments on commit 318c12c

Please sign in to comment.