Skip to content

Commit

Permalink
throw an exception on status being not done
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan Caruso committed Feb 29, 2024
1 parent e8e6f23 commit 38d9b18
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ private static unsafe byte[] UnsafeNetCoreDecode(ReadOnlySpan<char> strSpan, boo

int length = Encoding.UTF8.GetBytes(source, bytesSpan);
Span<byte> utf8Span = bytesSpan.Slice(0, length);

OperationStatus status = System.Buffers.Text.Base64.DecodeFromUtf8InPlace(utf8Span, out int bytesWritten);
Debug.Assert(status == OperationStatus.Done, "Expected DecodeFromUtf8 to be successful");
if (status != OperationStatus.Done)
throw LogHelper.LogExceptionMessage(new FormatException(LogHelper.FormatInvariant(LogMessages.IDX10400, strSpan.ToString())));

byte[] result = bytesSpan.Slice(0, bytesWritten).ToArray();

Expand Down Expand Up @@ -296,8 +298,10 @@ private static unsafe void UnsafeNetCoreDecode(ReadOnlySpan<char> strSpan, Span<

int length = Encoding.UTF8.GetBytes(source, bytesSpan);
Span<byte> utf8Span = bytesSpan.Slice(0, length);

OperationStatus status = System.Buffers.Text.Base64.DecodeFromUtf8InPlace(utf8Span, out int bytesWritten);
Debug.Assert(status == OperationStatus.Done, "Expected DecodeFromUtf8 to be successful");
if (status != OperationStatus.Done)
throw LogHelper.LogExceptionMessage(new FormatException(LogHelper.FormatInvariant(LogMessages.IDX10400, strSpan.ToString())));

utf8Span.Slice(0, bytesWritten).CopyTo(output);

Expand Down

0 comments on commit 38d9b18

Please sign in to comment.