Skip to content

Commit 039b28e

Browse files
github-actions[bot]gfoidladamsitnik
authored
[release/7.0] Base64.Decode: fixed latent bug for non-ASCII inputs (#76812)
* Added test cases * Fix * Use unicode escape sequence in tests * Apply suggestions from code review Co-authored-by: Günther Foidl <[email protected]> Co-authored-by: Adam Sitnik <[email protected]>
1 parent 9c14efc commit 039b28e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ public void BasicDecodingWithFinalBlockTrueKnownInputInvalid(string inputString,
272272
Assert.True(Base64TestHelper.VerifyDecodingCorrectness(expectedConsumed, decodedBytes.Length, source, decodedBytes));
273273
}
274274

275+
[Theory]
276+
[InlineData("\u00ecz/T", 0, 0)] // scalar code-path
277+
[InlineData("z/Ta123\u00ec", 4, 3)]
278+
[InlineData("\u00ecz/TpH7sqEkerqMweH1uSw==", 0, 0)] // Vector128 code-path
279+
[InlineData("z/TpH7sqEkerqMweH1uSw\u00ec==", 20, 15)]
280+
[InlineData("\u00ecz/TpH7sqEkerqMweH1uSw1a5ebaAF9xa8B0ze1wet4epo==", 0, 0)] // Vector256 / AVX code-path
281+
[InlineData("z/TpH7sqEkerqMweH1uSw1a5ebaAF9xa8B0ze1wet4epo\u00ec==", 44, 33)]
282+
public void BasicDecodingNonAsciiInputInvalid(string inputString, int expectedConsumed, int expectedWritten)
283+
{
284+
Span<byte> source = Encoding.UTF8.GetBytes(inputString);
285+
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
286+
287+
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount));
288+
Assert.Equal(expectedConsumed, consumed);
289+
Assert.Equal(expectedWritten, decodedByteCount);
290+
}
291+
275292
[Theory]
276293
[InlineData("AQID", 3)]
277294
[InlineData("AQIDBAUG", 6)]

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,9 @@ private static unsafe void Vector128Decode(ref byte* srcBytes, ref byte* destByt
592592

593593
// lookup
594594
Vector128<byte> hiNibbles = Vector128.ShiftRightLogical(str.AsInt32(), 4).AsByte() & mask2F;
595+
Vector128<byte> loNibbles = str & mask2F;
595596
Vector128<byte> hi = SimdShuffle(lutHi, hiNibbles, mask8F);
596-
Vector128<byte> lo = SimdShuffle(lutLo, str, mask8F);
597+
Vector128<byte> lo = SimdShuffle(lutLo, loNibbles, mask8F);
597598

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

0 commit comments

Comments
 (0)