Skip to content
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

Remove unsafe code from System.Web.HttpUtility #43422

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal static void HtmlAttributeEncode(string? value, TextWriter output)
HtmlAttributeEncodeInternal(value, output); HtmlAttributeEncodeInternal(value, output);
} }


private static unsafe void HtmlAttributeEncodeInternal(string s, TextWriter output) private static void HtmlAttributeEncodeInternal(string s, TextWriter output)
{ {
int index = IndexOfHtmlAttributeEncodingChars(s, 0); int index = IndexOfHtmlAttributeEncodingChars(s, 0);
if (index == -1) if (index == -1)
Expand All @@ -73,18 +73,12 @@ private static unsafe void HtmlAttributeEncodeInternal(string s, TextWriter outp
} }
else else
{ {
int cch = s.Length - index; output.Write(s.AsSpan(0, index));
fixed (char* str = s)
{
char* pch = str;
while (index-- > 0)
{
output.Write(*pch++);
}


while (cch-- > 0) ReadOnlySpan<char> remaining = s.AsSpan(index);
for (int i = 0; i < remaining.Length; i++)
{ {
char ch = *pch++; char ch = remaining[i];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the above could also be simply:

foreach (char ch in s.AsSpan(index))

The compiler will rewrite that to be basically identical to what's above.

if (ch <= '<') if (ch <= '<')
{ {
switch (ch) switch (ch)
Expand Down Expand Up @@ -113,7 +107,6 @@ private static unsafe void HtmlAttributeEncodeInternal(string s, TextWriter outp
} }
} }
} }
}


[return: NotNullIfNotNull("value")] [return: NotNullIfNotNull("value")]
internal static string? HtmlDecode(string? value) => string.IsNullOrEmpty(value) ? value : WebUtility.HtmlDecode(value); internal static string? HtmlDecode(string? value) => string.IsNullOrEmpty(value) ? value : WebUtility.HtmlDecode(value);
Expand Down Expand Up @@ -141,15 +134,14 @@ internal static void HtmlEncode(string? value, TextWriter output)
output.Write(WebUtility.HtmlEncode(value)); output.Write(WebUtility.HtmlEncode(value));
} }


private static unsafe int IndexOfHtmlAttributeEncodingChars(string s, int startPos) private static int IndexOfHtmlAttributeEncodingChars(string s, int startPos)
{ {
Debug.Assert(0 <= startPos && startPos <= s.Length, "0 <= startPos && startPos <= s.Length"); Debug.Assert(0 <= startPos && startPos <= s.Length, "0 <= startPos && startPos <= s.Length");
int cch = s.Length - startPos;
fixed (char* str = s) ReadOnlySpan<char> span = s.AsSpan(startPos);
{ for (int i = 0; i < span.Length; i++)
for (char* pch = &str[startPos]; cch > 0; pch++, cch--)
{ {
char ch = *pch; char ch = span[i];
if (ch <= '<') if (ch <= '<')
{ {
switch (ch) switch (ch)
Expand All @@ -158,8 +150,7 @@ private static unsafe int IndexOfHtmlAttributeEncodingChars(string s, int startP
case '"': case '"':
case '\'': case '\'':
case '&': case '&':
return s.Length - cch; return startPos + i;
}
} }
} }
} }
Expand Down