From df136a387de74ac3670a0782d10a1a528deaf597 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 21 Dec 2022 17:22:50 -0800 Subject: [PATCH 1/3] Fix findings found with new CA1853 analyzer --- .../src/System/Management/ManagementQuery.cs | 6 +++--- .../DebugDirectory/DebugDirectoryBuilder.cs | 7 ++++++- .../src/Internal/ObjectToken/ObjectTokenCategory.cs | 2 +- .../System.Speech/src/Internal/SrgsCompiler/BackEnd.cs | 4 ++-- .../System.Speech/src/Internal/SrgsParser/XmlParser.cs | 2 +- .../src/Recognition/SrgsGrammar/SrgsRuleRef.cs | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Management/src/System/Management/ManagementQuery.cs b/src/libraries/System.Management/src/System/Management/ManagementQuery.cs index e23db0d19d047..4304123de2171 100644 --- a/src/libraries/System.Management/src/System/Management/ManagementQuery.cs +++ b/src/libraries/System.Management/src/System/Management/ManagementQuery.cs @@ -126,7 +126,7 @@ internal static void ParseToken(ref string q, string token, string op, ref bool // Next character should be the operator if any if (op != null) { - if (0 != q.IndexOf(op, StringComparison.Ordinal)) + if (!q.StartsWith(op, StringComparison.Ordinal)) throw new ArgumentException(SR.InvalidQuery); // Invalid query // Strip off the op and any leading WS @@ -1666,7 +1666,7 @@ protected internal override void ParseQuery(string query) q = q.Remove(0, TokenOf.Length).TrimStart(null); // Next character should be "{" - if (0 != q.IndexOf('{')) + if (!q.StartsWith('{')) throw new ArgumentException(SR.InvalidQuery); // Invalid query // Strip off the "{" and any leading WS @@ -2183,7 +2183,7 @@ protected internal override void ParseQuery(string query) q = q.Remove(0, TokenOf.Length).TrimStart(null); // Next character should be "{" - if (0 != q.IndexOf('{')) + if (!q.StartsWith('{')) throw new ArgumentException(SR.InvalidQuery); // Invalid query // Strip off the "{" and any leading WS diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs index 82c3a16bc967d..db8a5bf59396e 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs @@ -114,7 +114,12 @@ public void AddCodeViewEntry( } // We allow NUL characters to allow for padding for backward compat purposes. - if (pdbPath.Length == 0 || pdbPath.IndexOf('\0') == 0) + if (pdbPath.Length == 0 || +#if NETCOREAPP + pdbPath.StartsWith('\0')) +#else + pdbPath.Length > 0 && pdbPath[0] == '\0') +#endif { Throw.InvalidArgument(SR.ExpectedNonEmptyString, nameof(pdbPath)); } diff --git a/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs b/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs index e78160f17e78e..ee9cf46645101 100644 --- a/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs +++ b/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs @@ -32,7 +32,7 @@ internal ObjectToken OpenToken(string keyName) { // Check if the token is for a voice string tokenName = keyName; - if (!string.IsNullOrEmpty(tokenName) && tokenName.IndexOf("HKEY_", StringComparison.Ordinal) != 0) + if (!string.IsNullOrEmpty(tokenName) && !tokenName.StartsWith("HKEY_", StringComparison.Ordinal)) { tokenName = string.Format(CultureInfo.InvariantCulture, @"{0}\Tokens\{1}", Id, tokenName); } diff --git a/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs b/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs index 7b0e5fd2070e9..aaa125d6c67c1 100644 --- a/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs +++ b/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs @@ -576,7 +576,7 @@ internal void CloneSubGraph(Rule rule, Backend org, Backend extra, Dictionary Date: Wed, 21 Dec 2022 20:40:40 -0800 Subject: [PATCH 2/3] Update src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs Co-authored-by: Dan Moseley --- .../PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs index db8a5bf59396e..f1c2f4d7c5836 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs @@ -118,7 +118,7 @@ public void AddCodeViewEntry( #if NETCOREAPP pdbPath.StartsWith('\0')) #else - pdbPath.Length > 0 && pdbPath[0] == '\0') + pdbPath[0] == '\0') #endif { Throw.InvalidArgument(SR.ExpectedNonEmptyString, nameof(pdbPath)); From 0f2d5c2d9b04a91dde2443835f8eb146c8765442 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 22 Dec 2022 09:48:00 -0800 Subject: [PATCH 3/3] Update src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs --- .../DebugDirectory/DebugDirectoryBuilder.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs index f1c2f4d7c5836..32cd1e6d8386e 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DebugDirectory/DebugDirectoryBuilder.cs @@ -114,12 +114,7 @@ public void AddCodeViewEntry( } // We allow NUL characters to allow for padding for backward compat purposes. - if (pdbPath.Length == 0 || -#if NETCOREAPP - pdbPath.StartsWith('\0')) -#else - pdbPath[0] == '\0') -#endif + if (pdbPath.Length == 0 || pdbPath[0] == '\0') { Throw.InvalidArgument(SR.ExpectedNonEmptyString, nameof(pdbPath)); }