1010CLR_RT_HeapBlock_String *CLR_RT_HeapBlock_String::CreateInstance (CLR_RT_HeapBlock &reference, CLR_UINT32 length)
1111{
1212 NATIVE_PROFILE_CLR_CORE ();
13+
14+ // compute required size for the string object (header + string length + null terminator)
1315 CLR_UINT32 totLength = sizeof (CLR_RT_HeapBlock_String) + length + 1 ;
1416 CLR_RT_HeapBlock_String *str;
1517
@@ -18,8 +20,11 @@ CLR_RT_HeapBlock_String *CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBloc
1820 str = (CLR_RT_HeapBlock_String *)g_CLR_RT_ExecutionEngine.ExtractHeapBytesForObjects (DATATYPE_STRING, 0 , totLength);
1921 if (str)
2022 {
23+ // grab a pointer to the string storage area (after the CLR_RT_HeapBlock_String header)
2124 char *szText = (char *)&str[1 ];
22- szText[0 ] = 0 ;
25+
26+ // zero out the string storage area
27+ memset (szText, 0 , CONVERTFROMSIZETOHEAPBLOCKS (totLength - sizeof (CLR_RT_HeapBlock_String)));
2328
2429#if defined(NANOCLR_NO_ASSEMBLY_STRINGS)
2530 str->SetStringText (szText);
@@ -39,7 +44,9 @@ HRESULT CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, con
3944 NANOCLR_HEADER ();
4045
4146 if (!szText)
47+ {
4248 szText = " " ;
49+ }
4350
4451 NANOCLR_SET_AND_LEAVE (CLR_RT_HeapBlock_String::CreateInstance (reference, szText, (CLR_UINT32)hal_strlen_s (szText)));
4552
@@ -57,8 +64,10 @@ HRESULT CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, con
5764 str = CreateInstance (reference, length);
5865 CHECK_ALLOCATION (str);
5966
67+ // grab a pointer to the string storage area (after the CLR_RT_HeapBlock_String header)
6068 szTextDst = (char *)str->StringText ();
6169
70+ // copy the string to the storage area
6271 memcpy (szTextDst, szText, length);
6372 szTextDst[length] = 0 ;
6473
@@ -70,21 +79,27 @@ HRESULT CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, con
7079 NATIVE_PROFILE_CLR_CORE ();
7180 NANOCLR_HEADER ();
7281
82+ #if defined(NANOCLR_NO_ASSEMBLY_STRINGS)
83+
84+ NANOCLR_CHECK_HRESULT (CLR_RT_HeapBlock_String::CreateInstance (reference, szText));
85+
86+ #else
87+
7388 CLR_RT_HeapBlock_String *str;
7489
7590 reference.SetObjectReference (NULL );
7691
92+ // get heap block for the string object
93+ // only the header is required as we're just pointing to the string stored in the assembly
7794 str = (CLR_RT_HeapBlock_String *)
7895 g_CLR_RT_ExecutionEngine.ExtractHeapBytesForObjects (DATATYPE_STRING, 0 , sizeof (CLR_RT_HeapBlock_String));
7996 CHECK_ALLOCATION (str);
8097
8198 reference.SetObjectReference (str);
8299
83- #if defined(NANOCLR_NO_ASSEMBLY_STRINGS)
84-
85- NANOCLR_CHECK_HRESULT (CLR_RT_HeapBlock_String::CreateInstance (reference, szText));
86- #else
100+ // store the pointers to the string and assembly
87101 str->SetStringText (szText, assm);
102+
88103#endif
89104
90105 NANOCLR_NOCLEANUP ();
0 commit comments