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

Base64.Decode: fixed latent bug for non-ASCII inputs #76795

Merged
merged 4 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ public void BasicDecodingWithFinalBlockTrueKnownInputInvalid(string inputString,
Assert.True(Base64TestHelper.VerifyDecodingCorrectness(expectedConsumed, decodedBytes.Length, source, decodedBytes));
}

[Theory]
[InlineData("�z/T", 0, 0)] // scalar code-path
gfoidl marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("z/Ta123�", 4, 3)]
[InlineData("�z/TpH7sqEkerqMweH1uSw==", 0, 0)] // Vector128 code-path
[InlineData("z/TpH7sqEkerqMweH1uSw�==", 20, 15)]
[InlineData("�z/TpH7sqEkerqMweH1uSw1a5ebaAF9xa8B0ze1wet4epo==", 0, 0)] // Vector256 / AVX code-path
[InlineData("z/TpH7sqEkerqMweH1uSw1a5ebaAF9xa8B0ze1wet4epo�==", 44, 33)]
public void BasicDecodingNonAsciiInputInvalid(string inputString, int expectedConsumed, int expectedWritten)
{
Span<byte> source = Encoding.UTF8.GetBytes(inputString);
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];

Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount));
Assert.Equal(expectedConsumed, consumed);
Assert.Equal(expectedWritten, decodedByteCount); // expectedWritten == decodedBytes.Length
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
}

[Theory]
[InlineData("AQID", 3)]
[InlineData("AQIDBAUG", 6)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,9 @@ private static unsafe void Vector128Decode(ref byte* srcBytes, ref byte* destByt

// lookup
Vector128<byte> hiNibbles = Vector128.ShiftRightLogical(str.AsInt32(), 4).AsByte() & mask2F;
Vector128<byte> loNibbles = str & mask2F;
Vector128<byte> hi = SimdShuffle(lutHi, hiNibbles, mask8F);
Vector128<byte> lo = SimdShuffle(lutLo, str, mask8F);
Vector128<byte> lo = SimdShuffle(lutLo, loNibbles, mask8F);

// Check for invalid input: if any "and" values from lo and hi are not zero,
// fall back on bytewise code to do error checking and reporting:
Expand Down