Skip to content

Commit

Permalink
Fix replacement loop in Base64UrlEncoder.UnsafeDecode (#2190)
Browse files Browse the repository at this point in the history
It was doing unnecessary work by starting the search from the beginning each time.
  • Loading branch information
stephentoub authored and Brent Schmaltz committed Jul 28, 2023
1 parent d4d0c61 commit 03627cb
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ internal static unsafe byte[] UnsafeDecode(ReadOnlyMemory<char> str)

if (needReplace)
{
Span<char> remaining = charsSpan;
int pos;
while ((pos = charsSpan.IndexOfAny(base64UrlCharacter62, base64UrlCharacter63)) >= 0)
while ((pos = remaining.IndexOfAny(base64UrlCharacter62, base64UrlCharacter63)) >= 0)
{
charsSpan[pos] = charsSpan[pos] == base64UrlCharacter62 ? base64Character62 : base64Character63;
remaining[pos] = (remaining[pos] == base64UrlCharacter62) ? base64Character62 : base64Character63;
remaining = remaining.Slice(pos + 1);
}
}

Expand Down

0 comments on commit 03627cb

Please sign in to comment.