@@ -46,7 +46,9 @@ using namespace nl::Weave::TLV;
4646//
4747// The assumption is that de-serialization will likely be performed
4848// only on resource-rich platforms where dynamic memory allocation is
49- // supported.
49+ // supported. However, it is still possible to use de-serialization without
50+ // memory allocation provided that there is no string to de-serialize or if so,
51+ // strings are empty.
5052//
5153
5254#if WEAVE_CONFIG_SERIALIZATION_ENABLE_DESERIALIZATION
@@ -877,18 +879,29 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
877879
878880 case SerializedFieldTypeUTF8String:
879881 {
880- char *dst = NULL ;
881882 // TLV Strings are not null terminated
882883 uint32_t length = aReader.GetLength () + 1 ;
883884
884- dst = (char *)memMgmt->mem_alloc (length);
885- VerifyOrExit (dst != NULL , err = WEAVE_ERROR_NO_MEMORY);
885+ if (length > 1 )
886+ {
887+ char *dst = NULL ;
886888
887- err = aReader.GetString (dst, length);
888- SuccessOrExit (err);
889+ dst = (char *)memMgmt->mem_alloc (length);
890+ VerifyOrExit (dst != NULL , err = WEAVE_ERROR_NO_MEMORY);
891+
892+ err = aReader.GetString (dst, length);
893+ SuccessOrExit (err);
894+
895+ LogReadWrite (" %s utf8string '%s' allocating %d bytes at %p" , " R" , dst, length, dst);
896+
897+ *static_cast <char **>(aStructureData) = dst;
898+ }
899+
900+ else
901+ {
902+ *static_cast <char **>(aStructureData) = " " ;
903+ }
889904
890- LogReadWrite (" %s utf8string '%s' allocating %d bytes at %p" , " R" , dst, length, dst);
891- *static_cast <char **>(aStructureData) = dst;
892905 break ;
893906 }
894907
@@ -897,11 +910,14 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
897910 SerializedByteString byteString;
898911 byteString.mLen = aReader.GetLength ();
899912
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 );
913+ if (byteString.mLen > 0 )
914+ {
915+ byteString.mBuf = static_cast <uint8_t *>(memMgmt->mem_alloc (byteString.mLen ));
916+ VerifyOrExit (byteString.mBuf != NULL , err = WEAVE_ERROR_NO_MEMORY);
917+ aReader.GetBytes (byteString.mBuf , byteString.mLen );
903918
904- LogReadWrite (" %s bytestring allocated %d bytes at %p" , " R" , byteString.mLen , byteString.mBuf );
919+ LogReadWrite (" %s bytestring allocated %d bytes at %p" , " R" , byteString.mLen , byteString.mBuf );
920+ }
905921 *static_cast <SerializedByteString *>(aStructureData) = byteString;
906922 break ;
907923 }
0 commit comments