Skip to content

Fix a few IndexOf{Any} uses #63852

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

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
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 Diff line number Diff line change
Expand Up @@ -728,16 +728,16 @@ private static void ValidateKeyName(string name)
throw new ArgumentNullException(nameof(name));
}

int nextSlash = name.IndexOf("\\", StringComparison.OrdinalIgnoreCase);
int nextSlash = name.IndexOf('\\');
int current = 0;
while (nextSlash != -1)
while (nextSlash >= 0)
{
if ((nextSlash - current) > MaxKeyLength)
{
throw new ArgumentException(SR.Arg_RegKeyStrLenBug, nameof(name));
}
current = nextSlash + 1;
nextSlash = name.IndexOf("\\", current, StringComparison.OrdinalIgnoreCase);
nextSlash = name.IndexOf('\\', current);
}

if ((name.Length - current) > MaxKeyLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,31 +422,25 @@ private void Parse(string challenge)

internal static class StringBuilderExtensions
{
// Characters that require escaping in quoted string
private static readonly char[] SpecialCharacters = new[] { '"', '\\' };

public static void AppendKeyValue(this StringBuilder sb, string key, string value, bool includeQuotes = true, bool includeComma = true)
{
sb.Append(key);
sb.Append('=');
sb.Append(key).Append('=');

if (includeQuotes)
{
ReadOnlySpan<char> valueSpan = value;
sb.Append('"');
int lastSpecialIndex = 0;
int specialIndex;
while (true)
{
specialIndex = value.IndexOfAny(SpecialCharacters, lastSpecialIndex);
if (specialIndex >= 0)
int i = valueSpan.IndexOfAny('"', '\\'); // Characters that require escaping in quoted string
if (i >= 0)
{
sb.Append(value, lastSpecialIndex, specialIndex - lastSpecialIndex);
sb.Append('\\');
sb.Append(value[specialIndex]);
lastSpecialIndex = specialIndex + 1;
sb.Append(valueSpan.Slice(0, i)).Append('\\').Append(valueSpan[i]);
valueSpan = valueSpan.Slice(i + 1);
}
else
{
sb.Append(value, lastSpecialIndex, value.Length - lastSpecialIndex);
sb.Append(valueSpan);
break;
}
}
Expand All @@ -459,8 +453,7 @@ public static void AppendKeyValue(this StringBuilder sb, string key, string valu

if (includeComma)
{
sb.Append(',');
sb.Append(' ');
sb.Append(',').Append(' ');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public abstract class AttachmentBase : IDisposable
{
internal bool disposed;
private readonly MimePart _part = new MimePart();
private static readonly char[] s_contentCIDInvalidChars = new char[] { '<', '>' };

internal AttachmentBase()
{
Expand Down Expand Up @@ -248,7 +247,7 @@ public string ContentId
}
else
{
if (value.IndexOfAny(s_contentCIDInvalidChars) != -1)
if (value.AsSpan().IndexOfAny('<', '>') >= 0) // invalid chars
{
throw new ArgumentException(SR.MailHeaderInvalidCID, nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ private static string GetSeparator(string format, string timeParts)
private static int IndexOfTimePart(string format, int startIndex, string timeParts)
{
Debug.Assert(startIndex >= 0, "startIndex cannot be negative");
Debug.Assert(timeParts.IndexOfAny(new char[] { '\'', '\\' }) < 0, "timeParts cannot include quote characters");
Debug.Assert(timeParts.AsSpan().IndexOfAny('\'', '\\') < 0, "timeParts cannot include quote characters");
bool inQuote = false;
for (int i = startIndex; i < format.Length; ++i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public sealed class SecurityElement
private const int AttributesTypical = 4 * 2; // 4 attributes, times 2 strings per attribute
private const int ChildrenTypical = 1;

private static readonly char[] s_tagIllegalCharacters = new char[] { ' ', '<', '>' };
private static readonly char[] s_textIllegalCharacters = new char[] { '<', '>' };
private static readonly char[] s_valueIllegalCharacters = new char[] { '<', '>', '\"' };
private static readonly char[] s_escapeChars = new char[] { '<', '>', '\"', '\'', '&' };
private static readonly string[] s_escapeStringPairs = new string[]
{
Expand Down Expand Up @@ -298,34 +295,17 @@ public SecurityElement Copy()
return element;
}

public static bool IsValidTag([NotNullWhen(true)] string? tag)
{
if (tag == null)
return false;

return tag.IndexOfAny(s_tagIllegalCharacters) < 0;
}

public static bool IsValidText([NotNullWhen(true)] string? text)
{
if (text == null)
return false;
public static bool IsValidTag([NotNullWhen(true)] string? tag) =>
tag != null && tag.AsSpan().IndexOfAny(' ', '<', '>') < 0;

return text.IndexOfAny(s_textIllegalCharacters) < 0;
}

public static bool IsValidAttributeName([NotNullWhen(true)] string? name)
{
return IsValidTag(name);
}
public static bool IsValidText([NotNullWhen(true)] string? text) =>
text != null && text.AsSpan().IndexOfAny('<', '>') < 0;

public static bool IsValidAttributeValue([NotNullWhen(true)] string? value)
{
if (value == null)
return false;
public static bool IsValidAttributeName([NotNullWhen(true)] string? name) =>
IsValidTag(name);

return value.IndexOfAny(s_valueIllegalCharacters) < 0;
}
public static bool IsValidAttributeValue([NotNullWhen(true)] string? value) =>
value != null && value.AsSpan().IndexOfAny('<', '>', '\"') < 0;

private static string GetEscapeSequence(char c)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,16 +1055,14 @@ private QilNode CompileAvt(string source)
return result;
}

private static readonly char[] s_curlyBraces = { '{', '}' };

[return: NotNullIfNotNull("avt")]
private QilNode? CompileStringAvt(string? avt)
{
if (avt == null)
{
return null;
}
if (avt.IndexOfAny(s_curlyBraces) == -1)
if (avt.AsSpan().IndexOfAny('{', '}') < 0)
{
return _f.String(avt);
}
Expand All @@ -1074,7 +1072,7 @@ private QilNode CompileAvt(string source)
private QilNode CompileTextAvt(string avt)
{
Debug.Assert(avt != null);
if (avt.IndexOfAny(s_curlyBraces) == -1)
if (avt.AsSpan().IndexOfAny('{', '}') < 0)
{
return _f.TextCtor(_f.String(avt));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ namespace System.Web.Util
{
internal static class UriUtil
{
private static readonly char[] s_queryFragmentSeparators = { '?', '#' };

// Just extracts the query string and fragment from the input path by splitting on the separator characters.
// Doesn't perform any validation as to whether the input represents a valid URL.
// Concatenating the pieces back together will form the original input string.
private static void ExtractQueryAndFragment(string input, out string path, out string? queryAndFragment)
{
int queryFragmentSeparatorPos = input.IndexOfAny(s_queryFragmentSeparators);
if (queryFragmentSeparatorPos != -1)
int queryFragmentSeparatorPos = input.AsSpan().IndexOfAny('?', '#'); // query fragment separators
if (queryFragmentSeparatorPos >= 0)
{
path = input.Substring(0, queryFragmentSeparatorPos);
queryAndFragment = input.Substring(queryFragmentSeparatorPos);
Expand Down