@@ -46,7 +46,9 @@ using namespace nl::Weave::TLV;
46
46
//
47
47
// The assumption is that de-serialization will likely be performed
48
48
// 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.
50
52
//
51
53
52
54
#if WEAVE_CONFIG_SERIALIZATION_ENABLE_DESERIALIZATION
@@ -877,18 +879,29 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
877
879
878
880
case SerializedFieldTypeUTF8String:
879
881
{
880
- char *dst = NULL ;
881
882
// TLV Strings are not null terminated
882
883
uint32_t length = aReader.GetLength () + 1 ;
883
884
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 ;
886
888
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
+ }
889
904
890
- LogReadWrite (" %s utf8string '%s' allocating %d bytes at %p" , " R" , dst, length, dst);
891
- *static_cast <char **>(aStructureData) = dst;
892
905
break ;
893
906
}
894
907
@@ -897,11 +910,14 @@ WEAVE_ERROR ReadDataForType(TLVReader &aReader, void *aStructureData, const Fiel
897
910
SerializedByteString byteString;
898
911
byteString.mLen = aReader.GetLength ();
899
912
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 );
903
918
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
+ }
905
921
*static_cast <SerializedByteString *>(aStructureData) = byteString;
906
922
break ;
907
923
}
0 commit comments