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/S2589 FP: For loop with Array.Length #8428

Closed
Tim-Pohlmann opened this issue Nov 30, 2023 · 0 comments · Fixed by #8656
Closed

Fix S2583/S2589 FP: For loop with Array.Length #8428

Tim-Pohlmann opened this issue Nov 30, 2023 · 0 comments · Fixed by #8656
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.
Milestone

Comments

@Tim-Pohlmann
Copy link
Contributor

Tim-Pohlmann commented Nov 30, 2023

The SE engine does not learn Array.Length which results in FPs.
This could be solved by tracking array sizes or by changing how the engine handles for-loops with unknown limits.

void Test()
{
    var ids = new int[1000];
    for (var i = 0; i < ids.Length; i++)
    {
        if (i % 100 != 0 || i <= 0)   // Noncompliant FP
        {
            System.Diagnostics.Debug.WriteLine(i);
        }
    }
}

Similar problems arise with C#12 collection expressions:

void CollectionExpressions(int[] array)
{
    int[] knownLength = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    for (var i = 0; i < knownLength.Length; i++)
    {
        if (i > 5)                  // Noncompliant FP
        {
            Console.WriteLine(i);   // Secondary
        }
    }

    int[] unknownLength = [1, 2, .. array, 3, 4];
    for (var i = 0; i < unknownLength.Length; i++)
    {
        if (i > 5)                  // Noncompliant FP
        {
            Console.WriteLine(i);   // Secondary
        }
    }

    int[] knownLength2 = [1, 2, .. knownLength, 3, 4];
    for (var i = 0; i < knownLength.Length; i++)
    {
        if (i > 5)                  // Noncompliant FP
        {
            Console.WriteLine(i);   // Secondary
        }
    }
}

and inline arrays:

[System.Runtime.CompilerServices.InlineArray(10)]
public struct Buffer
{
    private int _element0;
}
var buffer = new Buffer(); // buffer.Length is 10

Originally reported here.

@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. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Nov 30, 2023
martin-strecker-sonarsource added a commit that referenced this issue Nov 30, 2023
Co-authored-by: Martin Strecker <103252490+martin-strecker-sonarsource@users.noreply.github.com>
@Tim-Pohlmann Tim-Pohlmann added this to the 9.16 milestone Dec 6, 2023
@Tim-Pohlmann Tim-Pohlmann changed the title Fix S2583/S259 FP: For loop with Array.Length Fix S2583/S2589 FP: For loop with Array.Length Dec 6, 2023
@Tim-Pohlmann Tim-Pohlmann removed this from the 9.16 milestone Dec 18, 2023
@Tim-Pohlmann Tim-Pohlmann added this to the 9.20 milestone Jan 31, 2024
@Tim-Pohlmann Tim-Pohlmann linked a pull request Feb 5, 2024 that will close this issue
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
None yet
Development

Successfully merging a pull request may close this issue.

3 participants