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 17, 2020
1 parent 9708816 commit 0cba4f7
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/lib/support/SerializationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,18 +877,23 @@ 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);

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

Expand All @@ -897,12 +902,16 @@ 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;
*static_cast<SerializedByteString *>(aStructureData) = byteString;
}
break;
}

Expand Down

0 comments on commit 0cba4f7

Please sign in to comment.