Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StringSegment.Buffer can be null #57858

Merged
merged 11 commits into from
Aug 24, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public partial interface IChangeToken
private readonly object _dummy;
private readonly int _dummyPrimitive;
public static readonly Microsoft.Extensions.Primitives.StringSegment Empty;
public StringSegment(string buffer) { throw null; }
public StringSegment(string? buffer) { throw null; }
public StringSegment(string buffer, int offset, int length) { throw null; }
public string Buffer { get { throw null; } }
public string? Buffer { get { throw null; } }
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(Buffer))]
public bool HasValue { get { throw null; } }
public char this[int index] { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Microsoft.Extensions.Primitives
/// <param name="buffer">
/// The original <see cref="string"/>. The <see cref="StringSegment"/> includes the whole <see cref="string"/>.
/// </param>
public StringSegment(string buffer)
public StringSegment(string? buffer)
{
Buffer = buffer;
Offset = 0;
Expand Down Expand Up @@ -62,7 +62,7 @@ public StringSegment(string buffer, int offset, int length)
/// <summary>
/// Gets the <see cref="string"/> buffer for this <see cref="StringSegment"/>.
/// </summary>
public string Buffer { get; }
public string? Buffer { get; }

/// <summary>
/// Gets the offset within the buffer for this <see cref="StringSegment"/>.
Expand Down Expand Up @@ -102,7 +102,7 @@ public char this[int index]
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}

return Buffer[Offset + index];
return Buffer![Offset + index];
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public bool Equals([NotNullWhen(true)] string? text, StringComparison comparison
{
if (text == null)
{
return false;
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);
}

if (!HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,6 @@ public static TheoryData<string, StringComparison, bool> EqualsStringData
{
{ "eLLo", StringComparison.OrdinalIgnoreCase, true },
{ "eLLo", StringComparison.Ordinal, false },
{ null, StringComparison.OrdinalIgnoreCase, false },
{ null, StringComparison.Ordinal, false },
};
}
}
Expand All @@ -482,6 +480,17 @@ public void StringSegment_Equals_String_Valid(string candidate, StringComparison
Assert.Equal(expectedResult, result);
}

[Fact]
public void StringSegment_Equals_NullString_Throws()
{
// Arrange
var segment = new StringSegment();

// Act & assert
Assert.Throws<ArgumentNullException>("text", () => segment.Equals((string)null));
Assert.Throws<ArgumentNullException>("text", () => segment.Equals((string)null, StringComparison.Ordinal));
}

[Fact]
public void StringSegment_Equals_String_InvalidComparisonType_Throws()
{
Expand Down