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

Fix S2583 FP: Issue is raised when there is comparison to a constant. #8080

Closed
mary-georgiou-sonarsource opened this issue Sep 26, 2023 · 7 comments · Fixed by #8144
Closed
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
Milestone

Comments

@mary-georgiou-sonarsource
Copy link
Contributor

mary-georgiou-sonarsource commented Sep 26, 2023

Repro steps

public class TestClass
{
    protected const int Limit = 10;

    protected void Foo()
    {
        for (int week = 0; week < Limit; week++)
        {
            var test = week > 2 ? 0 : 42; // week>2 raises S2583, considering this condition always false
        }
    }
}

When the Limit is declared locally, no issue is raised.
It seems that there's some issue with learning numerical constraints for constants.

The issue has been reported by a community post.

@mary-georgiou-sonarsource mary-georgiou-sonarsource added the Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. label Sep 26, 2023
@Arthur-Robinson-Vincit
Copy link

It wasn't exclusively constants in my testing, it was any variable defined outside of the method.

@mary-georgiou-sonarsource
Copy link
Contributor Author

mary-georgiou-sonarsource commented Sep 26, 2023

Hi @Arthur-Robinson-Vincit

This is a symbolic execution rule.

In this context, the symbolic execution engine tracks const members and their values, so it should know that Limit == 10 always, and the issue should not have been raised.

In the case of nonconstant class members, their value is not tracked and the engine tries to learn number constraints from the context (just like they are parameters to the method, try to give Limit as an input parameter - you'll get the same FP). In this case, it's an FP for another reason and I will open another issue for it after investigating.

@Arthur-Robinson-Vincit
Copy link

Interesting, thanks for your explanation.

@Tim-Pohlmann Tim-Pohlmann self-assigned this Oct 4, 2023
@Tim-Pohlmann Tim-Pohlmann added this to In progress in Best Kanban Oct 4, 2023
@Tim-Pohlmann Tim-Pohlmann added this to the 9.12 milestone Oct 4, 2023
@Tim-Pohlmann Tim-Pohlmann added Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Oct 4, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Oct 4, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource modified the milestones: 9.12, 9.13 Oct 9, 2023
@steven-forrrest-vincit
Copy link

I am running into something very similar:

static bool Foo(string? someText)
{
    var parts = someText.Replace("F#3=", string.Empty).Replace("\r\n", string.Empty).Split(',');
    if (parts.Length != 5)
    {
        return false;
    }

    for (var i = 0; i < parts.Length; i++)
    {
        switch (i)
        {
            case 0:
                DoSomething();
                break;
            case 1:     // raises S2583 , considering this condition always true
                DoSomething();
                break;
            case 2:
                DoSomething();
                break;
            case 3:
                DoSomething();
                break;
        }
    }
    return true;
}

Is this the same issue as mentioned above?

@Tim-Pohlmann
Copy link
Contributor

@steven-forrrest-vincit This is a different issue. It is caused by the Symbolic Execution engine exploring loops only for two iterations.

@steven-forrrest-vincit
Copy link

@steven-forrrest-vincit This is a different issue. It is caused by the Symbolic Execution engine exploring loops only for two iterations.

Okay, will this issue be fixed in a future update?

@Tim-Pohlmann
Copy link
Contributor

The issue could be fixed in multiple ways.

  1. Change how we handle loops. We might be able to find a better strategy on how to analyze them.
  2. Track collection sizes instead of just whether or not a collection is empty.
  3. Track field and property values of objects. This would have a similar effect as 2), we know the value of parts.Length which in turn helps with the loop analysis.
    I want to get 2) and 3) done at some point for sure, but we don't have a fixed roadmap. 1) might come at some point if we find a better approach.

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. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
Best Kanban
  
Done
Development

Successfully merging a pull request may close this issue.

5 participants