diff --git a/src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs b/src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs index 4e11de38490591..0d792b26682d60 100644 --- a/src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs +++ b/src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs @@ -57,6 +57,19 @@ public void BasicDecodingInvalidInputLength() } } + [Fact] + public void BasicDecodingInvalidInputWithSlicedSource() + { + ReadOnlySpan source = stackalloc byte[] { (byte)'A', (byte)'B', (byte)'C', (byte)'D' }; + Span decodedBytes = stackalloc byte[128]; + + source = source[..3]; // now it's invalid as only 3 bytes are present + + Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount)); + Assert.Equal(0, consumed); + Assert.Equal(0, decodedByteCount); + } + [Fact] public void BasicDecodingWithFinalBlockFalse() { diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs index 2ec9959536427e..f4ebc942ac331a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs @@ -101,7 +101,7 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan utf8, Spa } ref sbyte decodingMap = ref MemoryMarshal.GetReference(DecodingMap); - srcMax = srcBytes + (uint)maxSrcLength; + srcMax = srcBytes + maxSrcLength; while (src < srcMax) {