-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix Base64.DecodeFromUtf8 consuming whitespace in partial final quantum when isFinalBlock=false #123313
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
base: main
Are you sure you want to change the base?
Conversation
|
This is consistent with what I had thought might be going wrong in the code, but I did not try stepping into the code to verify. |
…al quantum when isFinalBlock=false Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
MihaZupan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot expand the tests to include cases where there's more valid data before the call to DecodeFromUtf8, not just the single final block.
Include cases where the start is just base64, and where previous blocks already contain spaces.
...libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs
Show resolved
Hide resolved
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
The existing tests already demonstrate the streaming scenario with the reproduction cases from the issue. These tests validate that when calling |
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Added in 2f3d368. The new tests include:
All tests use a 100-byte buffer to avoid #123222. |
src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Outdated
Show resolved
Hide resolved
|
@copilot address the new feedback |
…rtions Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Outdated
Show resolved
Hide resolved
|
@copilot there are related test failures |
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
... Fixed in 72b87b8. Updated expected values to match actual decoder behavior:
The remaining string now includes the leading space: " AQ\r\nQ=" instead of "AQ\r\nQ=". |
|
We'll want to merge #123260 first since that'll change the tests |
48f935b to
00fd4f5
Compare
There was a problem hiding this 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 a bug in Base64.DecodeFromUtf8() where whitespace characters were incorrectly consumed when a final quantum (containing padding) was split by whitespace and isFinalBlock=false. This broke streaming decoders that need to retry with isFinalBlock=true after receiving more data.
Changes:
- Fixed
DecodeWithWhiteSpaceBlockwiseto only updatebytesConsumedafter successful decode, not before attempting decode - Added debug assertions to ensure no bytes are consumed/written on decode failure
- Added comprehensive tests for whitespace-split final quantum scenarios with both UTF-8 and char decoders
Reviewed changes
Copilot reviewed 2 out of 2 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 both DecodeWithWhiteSpaceBlockwise overloads (byte and ushort) to defer updating bytesConsumed until after successful decode, preventing whitespace from being consumed on failure; added leading whitespace skip logic and debug assertions |
| src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs | Added comprehensive test coverage for whitespace-split final quantum scenarios, including tests for both DecodeFromUtf8 and DecodeFromChars with various whitespace patterns and streaming scenarios |
Description
Base64.DecodeFromUtf8()incorrectly consumed whitespace characters when a final quantum (containing padding=) was split by whitespace andisFinalBlock=false. This broke streaming decoders that retry withisFinalBlock=trueafter receiving more data.Changes
DecodeWithWhiteSpaceBlockwise: Moved
bytesConsumed += skippedfrom before decode attempt to after successful decode. On failure, returns without updatingbytesConsumed. Added debug assert to verifylocalConsumedandlocalWrittenare both 0 when returning non-Done status.Tests: Added comprehensive coverage for whitespace-split final quantums with
isFinalBlock=falseand streaming scenarios, including:OperationStatus.InvalidData, exact bytes consumed/written, and remaining dataCustomer Impact
Streaming base64 decoders (e.g., HTTP response handlers, file parsers) fail when final quantum contains whitespace. Workarounds require buffer trimming and padding detection, adding complexity.
Regression?
No. Issue exists in current and previous versions.
Risk
Low. Change is minimal and only affects the failure path. All existing tests pass. Debug assert ensures contract is maintained.
Testing
OperationStatus.InvalidData, exact bytes consumed/written, and verification of remaining dataOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.