Skip to content

Conversation

@MihaZupan
Copy link
Member

Fixes #123222
Replaces #123258

From InvalidDataFallback we'll call back into DecodeFrom to try and process more data.
But if the destination is too small and the next block of Base64 contains whitespace, it won't be able to make progress. Use the block-wise fallback in those cases.

Tests in #123151 now pass when including this change.

@MihaZupan MihaZupan added this to the 11.0.0 milestone Jan 16, 2026
@MihaZupan MihaZupan requested a review from stephentoub January 16, 2026 14:31
@MihaZupan MihaZupan self-assigned this Jan 16, 2026
Copilot AI review requested due to automatic review settings January 16, 2026 14:31
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes edge cases in Base64 decoding when dealing with small destination buffers and embedded whitespace in the input. The core issue was that the InvalidDataFallback method would get stuck when trying to decode data with whitespace interspersed in Base64 blocks when the destination buffer was too small to hold the complete decoded output.

Changes:

  • Modified the loop exit condition in InvalidDataFallback to be more explicit about which statuses terminate the loop
  • Added logic to detect when DecodeFrom returns DestinationTooSmall but makes no progress (localConsumed == 0), falling back to block-wise decoding in those cases
  • Added comprehensive test coverage for the edge case with whitespace-heavy input and small destination buffers
  • Enhanced existing test assertions to verify Base64.DecodeFromUtf8 behavior is consistent with Convert.TryFromBase64String

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs Fixed the InvalidDataFallback loop to properly handle DestinationTooSmall status by continuing to consume whitespace when progress is made, or falling back to block-wise decoding when stuck
src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs Added test case DecodingWithEmbeddedWhiteSpaceIntoSmallDestination_TrailingWhiteSpacesAreConsumed to verify trailing whitespace is properly consumed even with small destination buffers
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Convert.cs Added assertions to existing parameterized tests to verify Base64.DecodeFromUtf8 behaves consistently with Convert methods across various input patterns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Base64{Url} incorrectly handles small destinations when inputs contain whitespace

1 participant