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

SE: Branch away from fixed-count loops #7192

Closed
Tim-Pohlmann opened this issue May 9, 2023 · 3 comments · Fixed by #7211
Closed

SE: Branch away from fixed-count loops #7192

Tim-Pohlmann opened this issue May 9, 2023 · 3 comments · Fixed by #7211
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues.
Milestone

Comments

@Tim-Pohlmann
Copy link
Contributor

For i As Integer = 0 To 9
    '  Do Something
Next
Monitor.Enter(Other)    ' FN, exploration stopped after loop
If Condition Then Monitor.Exit(Other)

Due to #7174 and #7142 code after Next will not be explored.

Note: Check if something similar can happen here:

var i = 0;
while (result is null)
{
    i++;
    if (i > 10)
        result = i;
}
@Tim-Pohlmann Tim-Pohlmann added Sprint: SE Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels May 9, 2023
@Tim-Pohlmann Tim-Pohlmann added this to the 9.1 milestone May 9, 2023
@pavel-mikula-sonarsource
Copy link
Contributor

Same for C# for loops with i++
This is tricky, because of combination of visiting for cycles only twice and fixed-number loops.

for(var i=0; i<10;i++)
{
  // Visited for 0
  // Visited for 1
}

Simple implementation will cause that the for is never existed and this line is not visited

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Update SE engine to not get stuck in loops SE: Branch away from fixed-count loops May 15, 2023
@pavel-mikula-sonarsource
Copy link
Contributor

This applies to any fixed-count loop, also like

            var i = 0;
            while(i < 10)   
            {
                i++;
            }
            // Not visited

@pavel-mikula-sonarsource
Copy link
Contributor

We need to prevent Binary.BoolConstraintFromOperation from learning True. That will let LearnBranchingConstraint to infer i=0..10 and i != 0..10 and branch out of the loop.

This should happen only if the branching is a loop-branching condition => one branch loops back, the other one does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants