Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 7d4b78f

Browse files
marek-safarjkotas
authored andcommitted
Updates unsafe String constructors to be consistent when handling null value (#17002)
Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 2b6109b commit 7d4b78f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Common/src/CoreLib/System/String.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,9 @@ private unsafe string Ctor(sbyte* value)
164164

165165
int numBytes = new ReadOnlySpan<byte>((byte*)value, int.MaxValue).IndexOf<byte>(0);
166166

167-
#if BIT64
168167
// Check for overflow
169168
if (numBytes < 0)
170169
throw new ArgumentException(SR.Arg_MustBeNullTerminatedString);
171-
#else
172-
Debug.Assert(numBytes >= 0);
173-
#endif
174170

175171
return CreateStringForSByteConstructor(pb, numBytes);
176172
}
@@ -194,7 +190,12 @@ private unsafe string Ctor(sbyte* value, int startIndex, int length)
194190
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength);
195191

196192
if (value == null)
193+
{
194+
if (length == 0)
195+
return Empty;
196+
197197
throw new ArgumentNullException(nameof(value));
198+
}
198199

199200
byte* pStart = (byte*)(value + startIndex);
200201

@@ -253,6 +254,14 @@ private unsafe string Ctor(sbyte* value, int startIndex, int length, Encoding en
253254
if (startIndex < 0)
254255
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
255256

257+
if (value == null)
258+
{
259+
if (length == 0)
260+
return Empty;
261+
262+
throw new ArgumentNullException(nameof(value));
263+
}
264+
256265
byte* pStart = (byte*)(value + startIndex);
257266

258267
// overflow check

0 commit comments

Comments
 (0)