@@ -877,18 +877,23 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
877
877
878
878
case SerializedFieldTypeUTF8String:
879
879
{
880
- char *dst = NULL ;
881
880
// TLV Strings are not null terminated
882
881
uint32_t length = aReader.GetLength () + 1 ;
883
882
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 ;
886
886
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);
889
894
890
- LogReadWrite ( " %s utf8string '%s' allocating %d bytes at %p " , " R " , dst, length, dst) ;
891
- * static_cast < char **>(aStructureData) = dst;
895
+ * static_cast < char **>(aStructureData) = dst;
896
+ }
892
897
break ;
893
898
}
894
899
@@ -897,12 +902,16 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
897
902
SerializedByteString byteString;
898
903
byteString.mLen = aReader.GetLength ();
899
904
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 );
903
912
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
+ }
906
915
break ;
907
916
}
908
917
0 commit comments