From 28012a92ef6e21948980c855367da9e194bedbea Mon Sep 17 00:00:00 2001 From: Valeri Hristov Date: Mon, 15 May 2017 10:13:52 +0200 Subject: [PATCH] Fix S2275: InvalidCastExteption when analyzing single argument method --- .../SonarAnalyzer.CSharp/Rules/StringFormatValidator.cs | 8 +++++--- .../StringFormatRuntimeExceptionFreeValidator.cs | 7 ++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/StringFormatValidator.cs b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/StringFormatValidator.cs index 0e213b66110..2dbb2060174 100644 --- a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/StringFormatValidator.cs +++ b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/StringFormatValidator.cs @@ -122,7 +122,7 @@ private static void CheckForFormatStringIssues(SyntaxNodeAnalysisContext analysi return; } - var failure = TryParseAndValidate((string)constValue.Value, invocation.ArgumentList, + var failure = TryParseAndValidate(constValue.Value as string, invocation.ArgumentList, formatArgumentIndex, analysisContext.SemanticModel); if (failure == null || CanIgnoreFailure(failure, currentMethodSignature.Name, invocation.ArgumentList.Arguments.Count)) @@ -150,8 +150,10 @@ private static bool CanIgnoreFailure(ValidationFailure failure, string methodNam failure == ValidationFailure.FormatItemIndexTooHigh) { return false; - } - + } + + // All methods in HandledFormatMethods that do not end on Format have an overload + // with only one argument and the rule should not raise an issue return argumentsCount == 1; } diff --git a/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/StringFormatRuntimeExceptionFreeValidator.cs b/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/StringFormatRuntimeExceptionFreeValidator.cs index a49072be767..3510727ec14 100644 --- a/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/StringFormatRuntimeExceptionFreeValidator.cs +++ b/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/StringFormatRuntimeExceptionFreeValidator.cs @@ -68,6 +68,7 @@ void System_Console_Write(string[] args) Console.Write("{0}", 42); Console.Write("{{}}"); // Compliant, displays {} Console.Write("{"); // Compliant + Console.Write(ulong.MaxValue); Console.Write("[0}", args[0]); // Noncompliant Console.Write("{-1}", args[0]); // Noncompliant @@ -80,6 +81,7 @@ void System_Console_WriteLine(string[] args) Console.WriteLine("0"); Console.WriteLine("{0}", 42); Console.WriteLine("{"); // Compliant + Console.WriteLine(ulong.MaxValue); Console.WriteLine("[0}", args[0]); // Noncompliant Console.WriteLine("{-1}", args[0]); // Noncompliant @@ -102,6 +104,7 @@ void System_IO_TextWriter_Write(string[] args) System.IO.TextWriter.Write("0"); System.IO.TextWriter.Write("{0}", 42); System.IO.TextWriter.Write("{"); // Compliant + System.IO.TextWriter.Write(ulong.MaxValue); System.IO.TextWriter.Write("[0}", args[0]); // Noncompliant System.IO.TextWriter.Write("{-1}", args[0]); // Noncompliant @@ -114,6 +117,7 @@ void System_IO_TextWriter_WriteLine(string[] args) System.IO.TextWriter.WriteLine("0"); System.IO.TextWriter.WriteLine("{0}", 42); System.IO.TextWriter.WriteLine("{"); // Compliant + System.IO.TextWriter.WriteLine(ulong.MaxValue); System.IO.TextWriter.WriteLine("[0}", args[0]); // Noncompliant System.IO.TextWriter.WriteLine("{-1}", args[0]); // Noncompliant @@ -126,6 +130,7 @@ void System_Diagnostics_Debug_WriteLine(string[] args) System.Diagnostics.Debug.WriteLine("0"); System.Diagnostics.Debug.WriteLine("{0}", 42); System.Diagnostics.Debug.WriteLine("{"); // Compliant + System.Diagnostics.Debug.WriteLine(ulong.MaxValue); System.Diagnostics.Debug.WriteLine("[0}", args[0]); // Noncompliant System.Diagnostics.Debug.WriteLine("{-1}", args[0]); // Noncompliant @@ -173,7 +178,7 @@ void System_Diagnostics_TraceSource_TraceInformation(string[] args) { System.Diagnostics.TraceSource.TraceInformation("0"); System.Diagnostics.TraceSource.TraceInformation("{0}", 42); - System.Diagnostics.TraceSource.TraceInformation( "{"); // Compliant + System.Diagnostics.TraceSource.TraceInformation("{"); // Compliant System.Diagnostics.TraceSource.TraceInformation("[0}", args[0]); // Noncompliant System.Diagnostics.TraceSource.TraceInformation("{-1}", args[0]); // Noncompliant