Skip to content

Commit

Permalink
Fix invalid IndexOutOfRangeException in Convert (#105339)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 28, 2024
1 parent ce91013 commit 11ca923
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2397,7 +2397,7 @@ public static unsafe int ToBase64CharArray(byte[] inArray, int offsetIn, int len

ArgumentOutOfRangeException.ThrowIfGreaterThan(offsetIn, inArrayLength - length);

if (inArrayLength == 0)
if (length == 0)
return 0;

// This is the maximally required length that must be available in the char array
Expand Down Expand Up @@ -2798,7 +2798,7 @@ public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length);

if (inArray.Length == 0)
if (length == 0)
{
return Array.Empty<byte>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public static void ToBase64CharArrayTest()
Assert.Equal(344, length2);
}

[Fact]
public static void ToBase64CharArray_NonEmptyInput_ZeroLength_Test()
{
Assert.Equal(0, Convert.ToBase64CharArray(new byte[1], 0, 0, new char[0], 0, Base64FormattingOptions.None));
}

[Fact]
public static void ToBase64StringTest()
{
Expand Down Expand Up @@ -77,6 +83,12 @@ public static void Base64_AllMethodsRoundtripConsistently()
}
}

[Fact]
public static void FromBase64CharArray_NonEmptyInputZeroLength_ReturnsEmptyArray()
{
Assert.Same(Array.Empty<byte>(), Convert.FromBase64CharArray(new char[42], 0, 0));
}

[Fact]
public void ToBooleanTests()
{
Expand Down

0 comments on commit 11ca923

Please sign in to comment.