sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

NET-1692 Fix S3949 FP: Unchecked keyword is sometimes ignored

Open Tim-Pohlmann opened this issue 2 years ago • 2 comments

Description

S3949 is supposed to not trigger when the unchecked keyword is present in the method containing the potential issue location. In some cases this exception is ignored.

Repro steps

void Foo1()
{
    _ = 1 + 1;
    unchecked
    {
        var i = int.MaxValue;
        _ = i + 1;  // Noncompliant FP
    }
}

Related information

The bug is caused by faulty logic in the ShouldExecute implementation. If the visitor finds a risky operation (setting HasOverflow true) it will stop execution and not find unchecked keywords following in later nodes.

Tim-Pohlmann avatar Sep 05 '23 15:09 Tim-Pohlmann

@Tim-Pohlmann I think this issues can be closed. The code you linked to looks completely different now and there are a lot of test cases which seem to be written exactly for such cases.

Internal ticket NET-1692