-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Removing unsafe parts of Decode method in QueryStringEnumerable #56630
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
using System; | ||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Intrinsics; | ||
|
@@ -88,21 +89,20 @@ public ReadOnlyMemory<char> DecodeName() | |
public ReadOnlyMemory<char> DecodeValue() | ||
=> Decode(EncodedValue); | ||
|
||
private static unsafe ReadOnlyMemory<char> Decode(ReadOnlyMemory<char> chars) | ||
private static ReadOnlyMemory<char> Decode(ReadOnlyMemory<char> chars) | ||
{ | ||
// If the value is short, it's cheap to check up front if it really needs decoding. If it doesn't, | ||
// then we can save some allocations. | ||
if (chars.Length < 16 && chars.Span.IndexOfAny('%', '+') < 0) | ||
ReadOnlySpan<char> source = chars.Span; | ||
if (source.Length < 16 && source.IndexOfAny('%', '+') < 0) | ||
{ | ||
return chars; | ||
} | ||
|
||
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type | ||
ReadOnlySpan<char> span = chars.Span; | ||
return Uri.UnescapeDataString( | ||
string.Create(span.Length, | ||
(IntPtr)(&span), static (dest, ptr) => ((ReadOnlySpan<char>*)ptr)->Replace(dest, '+', ' '))).AsMemory(); | ||
#pragma warning restore CS8500 | ||
var buffer = new char[source.Length]; | ||
source.Replace(buffer, '+', ' '); | ||
var success = Uri.TryUnescapeDataString(buffer, buffer, out var unescapedLength); | ||
Debug.Assert(success); | ||
return buffer.AsMemory(0, unescapedLength); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, TryUnescapeDataString shouldn't be able to return false in our case. |
||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.