diff --git a/src/lib/support/SerializationUtils.cpp b/src/lib/support/SerializationUtils.cpp index 77607453dd..cc15e5d406 100644 --- a/src/lib/support/SerializationUtils.cpp +++ b/src/lib/support/SerializationUtils.cpp @@ -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(aStructureData) = dst; + } + + else + { + *static_cast(aStructureData) = ""; + } - LogReadWrite("%s utf8string '%s' allocating %d bytes at %p", "R", dst, length, dst); - *static_cast(aStructureData) = dst; break; } @@ -897,11 +908,14 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel SerializedByteString byteString; byteString.mLen = aReader.GetLength(); - byteString.mBuf = static_cast(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(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(aStructureData) = byteString; break; } @@ -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 { @@ -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: @@ -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; }