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

Old SE: Handle unsupported syntax gracefully #6768

Merged
merged 22 commits into from Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c6704f1
Fix unsupported pattern kinds and add tests
martin-strecker-sonarsource Feb 17, 2023
a0020f2
Remove ifs and throw statement from BuildPatternExpression
martin-strecker-sonarsource Feb 21, 2023
93d37ca
Change handling of unsupported pattern to align with exisitng logic
martin-strecker-sonarsource Feb 21, 2023
270968c
Align test to exisitng implementation
martin-strecker-sonarsource Feb 21, 2023
4353d2c
Better exception validation
martin-strecker-sonarsource Feb 21, 2023
512c549
Fix test for type pattern
martin-strecker-sonarsource Feb 21, 2023
718d128
Fix test for type pattern
martin-strecker-sonarsource Feb 21, 2023
8a04810
Fix test in debug mode
martin-strecker-sonarsource Feb 21, 2023
6345e4f
Improve ExplodedGraph_IsPattern_Parentesized test
martin-strecker-sonarsource Feb 21, 2023
eb75766
Change test name
martin-strecker-sonarsource Feb 21, 2023
15735f9
Fix failing test on Release
martin-strecker-sonarsource Feb 21, 2023
9d278e8
Simplify ExplodedGraph_SwitchStatement_TypePattern test
martin-strecker-sonarsource Feb 21, 2023
b455abe
Fix code smell
martin-strecker-sonarsource Feb 21, 2023
774c4cd
Add TypePattern as unsupported
martin-strecker-sonarsource Feb 21, 2023
2c1dd92
Add test in S2259 for parenthesized pattern
martin-strecker-sonarsource Feb 23, 2023
0d8026e
Add more pattern for switch cases
martin-strecker-sonarsource Feb 23, 2023
0e21b8f
Remove commentd
martin-strecker-sonarsource Feb 24, 2023
7360dc9
Remove comments
martin-strecker-sonarsource Feb 24, 2023
b0a8ade
Fix test to cover switch cases instead of is pattern
martin-strecker-sonarsource Feb 24, 2023
8b43176
Remove throw in SonarExplodedGraph
martin-strecker-sonarsource Feb 28, 2023
4c3daa3
Code review
martin-strecker-sonarsource Feb 28, 2023
1f58b1a
Remove all pattern cases and just do default: nothing.
martin-strecker-sonarsource Feb 28, 2023
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
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]
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
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.");
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}

[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;
}
}
}
}