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

Commit c0445dd

Browse files
committed
Do not try to allocate memory on de-serialization when there is no need
to do so(empty strings). Also, do not attempt to free a string if empty.
1 parent 9708816 commit c0445dd

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/lib/support/SerializationUtils.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -877,18 +877,23 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
877877

878878
case SerializedFieldTypeUTF8String:
879879
{
880-
char *dst = NULL;
881880
// TLV Strings are not null terminated
882881
uint32_t length = aReader.GetLength() + 1;
883882

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

887-
err = aReader.GetString(dst, length);
888-
SuccessOrExit(err);
887+
dst = (char *)memMgmt->mem_alloc(length);
888+
VerifyOrExit(dst != NULL, err = WEAVE_ERROR_NO_MEMORY);
889+
890+
err = aReader.GetString(dst, length);
891+
SuccessOrExit(err);
889892

890-
LogReadWrite("%s utf8string '%s' allocating %d bytes at %p", "R", dst, length, dst);
891-
*static_cast<char**>(aStructureData) = dst;
893+
LogReadWrite("%s utf8string '%s' allocating %d bytes at %p", "R", dst, length, dst);
894+
895+
*static_cast<char**>(aStructureData) = dst;
896+
}
892897
break;
893898
}
894899

@@ -897,12 +902,16 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
897902
SerializedByteString byteString;
898903
byteString.mLen = aReader.GetLength();
899904

900-
byteString.mBuf = static_cast<uint8_t *>(memMgmt->mem_alloc(byteString.mLen));
901-
VerifyOrExit(byteString.mBuf != NULL, err = WEAVE_ERROR_NO_MEMORY);
902-
aReader.GetBytes(byteString.mBuf, byteString.mLen);
905+
if (byteString.mLen > 0)
906+
{
907+
byteString.mBuf = static_cast<uint8_t *>(memMgmt->mem_alloc(byteString.mLen));
908+
VerifyOrExit(byteString.mBuf != NULL, err = WEAVE_ERROR_NO_MEMORY);
909+
aReader.GetBytes(byteString.mBuf, byteString.mLen);
910+
911+
LogReadWrite("%s bytestring allocated %d bytes at %p", "R", byteString.mLen, byteString.mBuf);
903912

904-
LogReadWrite("%s bytestring allocated %d bytes at %p", "R", byteString.mLen, byteString.mBuf);
905-
*static_cast<SerializedByteString *>(aStructureData) = byteString;
913+
*static_cast<SerializedByteString *>(aStructureData) = byteString;
914+
}
906915
break;
907916
}
908917

@@ -1298,10 +1307,13 @@ WEAVE_ERROR DeallocateDeserializedArray(void *aArrayData,
12981307

12991308
if (aFieldDescriptors == NULL)
13001309
{
1301-
LogReadWrite("%s Freeing array of primitive type at 0x%x", "R", array->mElementBuffer);
1310+
// The elements are of a primitive type, we can free the array now if not empty
1311+
if (array->mNumElements > 0)
1312+
{
1313+
LogReadWrite("%s Freeing array of primitive type at 0x%x", "R", array->mElementBuffer);
13021314

1303-
// The elements are of a primitive type, we can free the array now.
1304-
memMgmt->mem_free(array->mElementBuffer);
1315+
memMgmt->mem_free(array->mElementBuffer);
1316+
}
13051317
}
13061318
else
13071319
{
@@ -1377,10 +1389,14 @@ WEAVE_ERROR DeallocateDeserializedStructure(void *aStructureData,
13771389
{
13781390
char *str = *((char **)currentFieldData);
13791391

1380-
LogReadWrite("%s Freeing UTF8String '%s' at 0x%x", "R", str, str);
1392+
// Go ahead and free it here if not empty.
1393+
if (!(*str == 0))
1394+
{
1395+
LogReadWrite("%s Freeing UTF8String '%s' at 0x%x", "R", str, str);
1396+
1397+
memMgmt->mem_free(str);
1398+
}
13811399

1382-
// Go ahead and free it here.
1383-
memMgmt->mem_free(str);
13841400
break;
13851401
}
13861402

0 commit comments

Comments
 (0)