[Breaking change]: Base64.DecodeFromUtf8{InPlace} ignore whitespace #35282
Labels
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 8
Work items for the .NET 8 release
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Pri1
High priority, do before Pri2 and Pri3
📌 seQUESTered
Identifies that an issue has been imported into Quest.
Description
The
System.Convert.FromBase64String
,System.Convert.FromBase64CharArray
, and correspondingTry
methods onSystem.Convert
have long ignored some ASCII whitespace characters (' ', '\t', '\r', '\n'), allowing any amount of such whitespace to be in the input. However, when theSystem.Buffers.Text.Base64.DecodeFromUtf8
andSystem.Buffers.Text.Base64.DecodeFromUtf8InPlace
methods were added, they did not ignore these whitespace characters and would instead fail to decode any input that included whitespace. This makes the behavior when using the UTF16-based APIs different from that using the UTF8-based APIs, and means that theBase64.DecodeFromUtf8
andBase64.DecodeFromUtf8InPlace
methods couldn't roundtrip UTF-encoded base-64 encoded data produced byConvert.ToBase64String
with theBase64FormattingOptions.InsertLineBreaks
option. It also means that the newBase64.IsValid(ReadOnlySpan<char>)
andBase64.IsValid(ReadOnlySpan<byte>)
methods would either need to have behavior inconsistent with each other or with their corresponding methods for UTF16 and UTF8 data onConvert
andBase64
.Version
.NET 8 Preview 5
Previous behavior
System.Buffers.Text.Base64.DecodeFromUtf8
andSystem.Buffers.Text.Base64.DecodeFromUtf8InPlace
would fail to process input containing whitespace, returningOperationStatus.InvalidData
if any whitespace was encountered.New behavior
System.Buffers.Text.Base64.DecodeFromUtf8
andSystem.Buffers.Text.Base64.DecodeFromUtf8InPlace
now ignore whitespace (specifically ' ', '\t', '\r', and '\n') in the input, matching the behavior ofConvert.FromBase64String
.Type of breaking change
Reason for change
The change was made so that:
Base64
methods could decode a wider range of input data, including that produced byConvert.ToBase64String
with theBase64FormattingOptions.InsertLineBreaks
option as well as common formatting of data in configuration files and other real data sources.Base64
methods would be consistent with the corresponding decoding APIs onSystem.Convert
.Base64.IsValid
APIs could be added in a manner where their behavior is consistent with each other and with the existingConvert
andBase64
APIs.Recommended action
If the new behavior is problematic for your code, you can use
IndexOfAny(" \t\r\n"u8)
to search the input for the whitespace that previously would have triggered anInvalidData
result.Feature area
Core .NET libraries
Affected APIs
System.Buffers.Text.Base64.DecodeFromUtf8
System.Buffers.Text.Base64.DecodeFromUtf8InPlace
Associated WorkItem - 97023
The text was updated successfully, but these errors were encountered: