Skip to content

Commit

Permalink
Old SE: Handle unsupported syntax gracefully (#6768)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource committed Feb 28, 2023
1 parent b89dd7a commit 5c8e7e3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
Expand Up @@ -577,7 +577,9 @@ protected override void VisitInstruction(ExplodedGraphNode node)
break;

default:
throw new NotSupportedException($"{instruction.Kind()}");
// Not Supported
// Do nothing
break;
}

newProgramState = InvokeChecks(newProgramState, (ps, check) => check.PostProcessInstruction(node.ProgramPoint, ps));
Expand Down
Expand Up @@ -4141,6 +4141,22 @@ public void Cfg_OrPattern_InIf_IsNotSupported()
exception.Message.Should().Be("OrPattern");
}

[TestMethod]
public void Cfg_ParenthesizedPattern_InIf_IsNotSupported()
{
var exception = Assert.ThrowsException<NotSupportedException>(() => Build(@"if (tainted is (string s)) { }"));

exception.Message.Should().Be("ParenthesizedPattern");
}

[TestMethod]
public void Cfg_ListPattern_InIf_IsNotSupported()
{
var exception = Assert.ThrowsException<NotSupportedException>(() => Build(@"if (tainted is []) { }"));

exception.Message.Should().Be("ListPattern");
}

[TestMethod]
public void Cfg_Switch_Patterns_NoDefault()
{
Expand Down
Expand Up @@ -28,7 +28,6 @@
using SonarAnalyzer.LiveVariableAnalysis.CSharp;
using SonarAnalyzer.SymbolicExecution.Constraints;
using SonarAnalyzer.SymbolicExecution.Sonar;
using SonarAnalyzer.SymbolicExecution.Sonar.Constraints;
using SonarAnalyzer.UnitTest.CFG.Sonar;
using SonarAnalyzer.UnitTest.Helpers;

Expand Down Expand Up @@ -901,6 +900,27 @@ public void ExplodedGraph_IsPattern_WithPositionalPattern()
context.WalkWithInstructions(4);
}

[DataTestMethod]
[DataRow("int")]
[DataRow("(string s)")]
[DataRow("1 or 2")]
[DataRow("1 and 2")]
[DataRow("not 1")]
[DataRow("> 5")]
[DataRow("[] empty")]
public void ExplodedGraph_SwitchStatement_UnsupportedPatternKinds(string pattern)
{
var testInput = $$"""
switch (new object())
{
case {{pattern}}: break;
}
""";
var context = new ExplodedGraphContext(testInput);
var walk = () => context.WalkWithInstructions(2);
walk.Should().Throw<Exception>().WithMessage("Expected NumberOfExitBlockReached to be 1, but found 0.");
}

[TestMethod]
public void ExplodedGraph_SwitchExpressionVisit()
{
Expand Down
Expand Up @@ -19,4 +19,29 @@ public void ListPattern(object[] objects)
}
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/6766
public class Repo_6766
{
public void SwitchCasePattern(object o, object[] array)
{
switch (o)
{
case (1 or 2): // Parenthesized
case 3 or 4: // Binary
case > 5: // Relational
case char: // Type
case Exception { Message.Length: 1 }: // Recursive
case (int _, int _): // Recursive
case not "": // Unary
break;
}
switch (array)
{
case []: // list pattern
case [1, .., 2]: // list pattern with slice
break;
}
}
}
}

0 comments on commit 5c8e7e3

Please sign in to comment.