Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public void BasicDecodingInvalidInputLength()
}
}

[Fact]
public void BasicDecodingInvalidInputWithSlicedSource()
{
ReadOnlySpan<byte> source = stackalloc byte[] { (byte)'A', (byte)'B', (byte)'C', (byte)'D' };
Span<byte> 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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
}

ref sbyte decodingMap = ref MemoryMarshal.GetReference(DecodingMap);
srcMax = srcBytes + (uint)maxSrcLength;
srcMax = srcBytes + maxSrcLength;
Copy link
Member Author

@gfoidl gfoidl Dec 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uint-cast was there to avoid the movsxd (on x86), so removing the cast will introduce the movsxd and I expect that be cheaper than having a if to guard the loop.

PS: this is a reason why I dislike walking with pointers around (ptr++), and prefer index-based addressing (ptr[i] or ptr + offset) as it's clearer where to start and where to end.


while (src < srcMax)
{
Expand Down