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

Code Health | Enforcing Ordinal for StringComparison #2068

Merged
merged 4 commits into from Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -143,6 +143,9 @@ dotnet_diagnostic.CA2100.severity = silent
# CA1416: Validate platform compatibility
dotnet_diagnostic.CA1416.severity = silent

# CA1310: Specify StringComparison for correctness
dotnet_diagnostic.CA1310.severity = error
JRahnama marked this conversation as resolved.
Show resolved Hide resolved

[*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2

Expand Down
Expand Up @@ -675,7 +675,7 @@ private void ReportSNIError(SNIProviders provider)
private bool InferNamedPipesInformation()
{
// If we have a datasource beginning with a pipe or we have already determined that the protocol is Named Pipe
if (_dataSourceAfterTrimmingProtocol.StartsWith(PipeBeginning) || _connectionProtocol == Protocol.NP)
if (_dataSourceAfterTrimmingProtocol.StartsWith(PipeBeginning, StringComparison.Ordinal) || _connectionProtocol == Protocol.NP)
{
// If the data source is "np:servername"
if (!_dataSourceAfterTrimmingProtocol.Contains(PipeBeginning))
Expand Down Expand Up @@ -714,7 +714,7 @@ private bool InferNamedPipesInformation()
return false;
}

if (tokensByBackSlash[4].StartsWith(NamedPipeInstanceNameHeader))
if (tokensByBackSlash[4].StartsWith(NamedPipeInstanceNameHeader, StringComparison.Ordinal))
{
InstanceName = tokensByBackSlash[4].Substring(NamedPipeInstanceNameHeader.Length);
}
Expand Down
Expand Up @@ -369,7 +369,7 @@ private RSACng CreateRSACngProvider(string keyPath, bool isSystemOp)
/// <param name="keyIdentifier">Key identifier inside the CNG provider</param>
private void GetCngProviderAndKeyId(string keyPath, bool isSystemOp, out string cngProvider, out string keyIdentifier)
{
int indexOfSlash = keyPath.IndexOf(@"/");
int indexOfSlash = keyPath.IndexOf(@"/", StringComparison.Ordinal);
if (indexOfSlash == -1)
{
throw SQL.InvalidCngPath(keyPath, isSystemOp);
Expand Down
Expand Up @@ -395,7 +395,7 @@ private RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath, bool is
/// <param name="keyIdentifier">output containing the key name</param>
private void GetCspProviderAndKeyName(string keyPath, bool isSystemOp, out string cspProviderName, out string keyIdentifier)
{
int indexOfSlash = keyPath.IndexOf(@"/");
int indexOfSlash = keyPath.IndexOf(@"/", StringComparison.Ordinal);
if (indexOfSlash == -1)
{
throw SQL.InvalidCspPath(keyPath, isSystemOp);
Expand Down
Expand Up @@ -434,7 +434,7 @@ internal void ProcessPendingAck(TdsParserStateObject stateObj)

FQDNforDNSCache = serverInfo.ResolvedServerName;

int commaPos = FQDNforDNSCache.IndexOf(",");
int commaPos = FQDNforDNSCache.IndexOf(",", StringComparison.Ordinal);
if (commaPos != -1)
{
FQDNforDNSCache = FQDNforDNSCache.Substring(0, commaPos);
Expand Down
Expand Up @@ -453,7 +453,7 @@ static private string GetFullPathInternal(string path)
}

// make sure path is not DOS device path
if (!path.StartsWith(@"\\") && !System.IO.PathInternal.IsDevice(path.AsSpan()))
if (!path.StartsWith(@"\\", StringComparison.Ordinal) && !System.IO.PathInternal.IsDevice(path.AsSpan()))
{
throw ADP.Argument(StringsHelper.GetString(Strings.SqlFileStream_InvalidPath), "path");
}
Expand Down
Expand Up @@ -373,7 +373,7 @@ private static bool IsValidAuthenticationMethodEnum()
{
for (int i = 0; i < l; i++)
{
if (s_supportedAuthenticationModes[i].CompareTo(names[i]) != 0)
if (string.Compare(s_supportedAuthenticationModes[i], names[i], StringComparison.Ordinal) != 0)
{
listValid = false;
}
Expand Down
Expand Up @@ -122,7 +122,7 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
// Use Connection timeout value to cancel token acquire request after certain period of time.
cts.CancelAfter(parameters.ConnectionTimeout * 1000); // Convert to milliseconds

string scope = parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix;
string scope = parameters.Resource.EndsWith(s_defaultScopeSuffix, StringComparison.Ordinal) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix;
string[] scopes = new string[] { scope };
TokenRequestContext tokenRequestContext = new(scopes);

Expand Down
Expand Up @@ -983,7 +983,7 @@ private static string GetComputerNameDnsFullyQualified()
{
var domainName = "." + IPGlobalProperties.GetIPGlobalProperties().DomainName;
var hostName = Dns.GetHostName();
if (domainName != "." && !hostName.EndsWith(domainName))
if (domainName != "." && !hostName.EndsWith(domainName, StringComparison.Ordinal))
hostName += domainName;
return hostName;
}
Expand Down
Expand Up @@ -406,7 +406,7 @@ internal static void VerifyColumnMasterKeySignature(string keyStoreName, string
private static bool ShouldUseInstanceLevelProviderFlow(string keyStoreName, SqlConnection connection, SqlCommand command)
{
return InstanceLevelProvidersAreRegistered(connection, command) &&
!keyStoreName.StartsWith(ADP.ColumnEncryptionSystemProviderNamePrefix);
!keyStoreName.StartsWith(ADP.ColumnEncryptionSystemProviderNamePrefix, StringComparison.Ordinal);
}

private static bool InstanceLevelProvidersAreRegistered(SqlConnection connection, SqlCommand command) =>
Expand Down