@@ -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 }
0 commit comments