@@ -877,18 +877,29 @@ 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);
892+
893+ LogReadWrite (" %s utf8string '%s' allocating %d bytes at %p" , " R" , dst, length, dst);
894+
895+ *static_cast <char **>(aStructureData) = dst;
896+ }
897+
898+ else
899+ {
900+ *static_cast <char **>(aStructureData) = " " ;
901+ }
889902
890- LogReadWrite (" %s utf8string '%s' allocating %d bytes at %p" , " R" , dst, length, dst);
891- *static_cast <char **>(aStructureData) = dst;
892903 break ;
893904 }
894905
@@ -897,11 +908,14 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
897908 SerializedByteString byteString;
898909 byteString.mLen = aReader.GetLength ();
899910
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 );
911+ if (byteString.mLen > 0 )
912+ {
913+ byteString.mBuf = static_cast <uint8_t *>(memMgmt->mem_alloc (byteString.mLen ));
914+ VerifyOrExit (byteString.mBuf != NULL , err = WEAVE_ERROR_NO_MEMORY);
915+ aReader.GetBytes (byteString.mBuf , byteString.mLen );
903916
904- LogReadWrite (" %s bytestring allocated %d bytes at %p" , " R" , byteString.mLen , byteString.mBuf );
917+ LogReadWrite (" %s bytestring allocated %d bytes at %p" , " R" , byteString.mLen , byteString.mBuf );
918+ }
905919 *static_cast <SerializedByteString *>(aStructureData) = byteString;
906920 break ;
907921 }
@@ -1300,8 +1314,9 @@ WEAVE_ERROR DeallocateDeserializedArray(void *aArrayData,
13001314 {
13011315 LogReadWrite (" %s Freeing array of primitive type at 0x%x" , " R" , array->mElementBuffer );
13021316
1303- // The elements are of a primitive type, we can free the array now.
1304- memMgmt->mem_free (array->mElementBuffer );
1317+ // The elements are of a primitive type, we can free the array now if not empty
1318+ if (array->mNumElements > 0 )
1319+ memMgmt->mem_free (array->mElementBuffer );
13051320 }
13061321 else
13071322 {
@@ -1315,8 +1330,9 @@ WEAVE_ERROR DeallocateDeserializedArray(void *aArrayData,
13151330
13161331 LogReadWrite (" %s Freeing array of structures at 0x%x" , " R" , array->mElementBuffer );
13171332
1318- // Free the array now.
1319- memMgmt->mem_free (array->mElementBuffer );
1333+ // Free the array now if not empty
1334+ if (array->mNumElements > 0 )
1335+ memMgmt->mem_free (array->mElementBuffer );
13201336 }
13211337
13221338exit:
@@ -1379,8 +1395,9 @@ WEAVE_ERROR DeallocateDeserializedStructure(void *aStructureData,
13791395
13801396 LogReadWrite (" %s Freeing UTF8String '%s' at 0x%x" , " R" , str, str);
13811397
1382- // Go ahead and free it here.
1383- memMgmt->mem_free (str);
1398+ // Go ahead and free it here if not empty.
1399+ if (!strcmp (str, " " ))
1400+ memMgmt->mem_free (str);
13841401 break ;
13851402 }
13861403
0 commit comments