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

FPs and FNs in the Symbolic Execution rules when null coalescing is combined with arithmetic expressions because we do not support constraints on integers #2528

Closed
ChaosCrafter opened this issue Aug 1, 2019 · 8 comments · Fixed by #7176
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. Type: False Negative Rule is NOT triggered when it should be.
Milestone

Comments

@ChaosCrafter
Copy link

ChaosCrafter commented Aug 1, 2019

When an IF statement uses the null conditional rule and (or?) the null coalescing operator then the else path is not marked as cannot be null, resulting in false S2259 messages.

Eg.

if ((x?.value??0)==0)
{ // this happens if x is null, x.value is null or x.value == 0
}
else
{ // this part can't be called if x is null or x.value is null
  text = $"{x.value} items";
}

In this example, the line
text = $"{x.value} items";
gets a message S2259 'x' is null on at least one execution path.
Unless I'm misunderstanding something, that is not true.

@duncanp-sonar duncanp-sonar transferred this issue from SonarSource/sonarlint-visualstudio Aug 1, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource added Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: Symbolic Execution Type: False Positive Rule IS triggered when it shouldn't be. labels Aug 5, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource added this to the 7.16 milestone Aug 5, 2019
@andrei-epure-sonarsource
Copy link
Contributor

This is most likely a duplicate of #2361 which got fixed during this sprint, it needs to be reviewed and confirmed it got fixed.

@christophe-zurn-sonarsource
Copy link
Contributor

christophe-zurn-sonarsource commented Aug 5, 2019

@andrei-epure-sonarsource I had a look and this is not actually a duplicate of #2361. The issue here comes from the fact that we do not support constraints on integers in the symbolic execution engine.
Basically, when (x?.value??0)==0 is false, this indeed implies that x is not null, but since we do not track integers, we cannot infer the non-nullability of x from the equals comparing integers.

A simpler reproducer for this FP:

class ValueHolder
{
  public int? value;
}

string Foo(ValueHolder x)
{
  if ((x == null ? 0 : 1) == 0)
  {
    return "x is null";
  }
  else
  {
    return $"{x.value} items";
  }
}

@christophe-zurn-sonarsource christophe-zurn-sonarsource modified the milestones: 7.16, CFG + Symbolic Execution Aug 5, 2019
@costin-zaharia-sonarsource costin-zaharia-sonarsource removed this from the CFG + Symbolic Execution milestone Dec 2, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title handling of null conditional and null coalescing operators can lead to a false positive for S2259 S2259: FP for nullable value types Jul 8, 2020
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title S2259: FP for nullable value types S2259: FP for nullable value types when null coalescing is combined with arithmetic expressions Dec 22, 2020
@andrei-epure-sonarsource andrei-epure-sonarsource added the Type: False Negative Rule is NOT triggered when it should be. label Dec 22, 2020
@andrei-epure-sonarsource
Copy link
Contributor

andrei-epure-sonarsource commented Dec 22, 2020

Another repro for this FP on community.

And this can also manifest as a False Negative in S2583/S2589, e.g.

    public class Repro_2528
    {
        public int Count { get; }
        public int Foo()
        {
            Repro_2528 x = null;
            if (x == null) { } // Noncompliant
            if ((x?.Count ?? 0) == 0) // FN
            {
                return -1;
            }

            return 1;
        }
    }

@andrei-epure-sonarsource andrei-epure-sonarsource changed the title S2259: FP for nullable value types when null coalescing is combined with arithmetic expressions FP and FN in Symbolic Execution rules when null coalescing is combined with arithmetic expressions (do not support constraints on integers) Dec 22, 2020
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title FP and FN in Symbolic Execution rules when null coalescing is combined with arithmetic expressions (do not support constraints on integers) FPs and FNs in the Symbolic Execution rules when null coalescing is combined with arithmetic expressions (do not support constraints on integers) Dec 22, 2020
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title FPs and FNs in the Symbolic Execution rules when null coalescing is combined with arithmetic expressions (do not support constraints on integers) FPs and FNs in the Symbolic Execution rules when null coalescing is combined with arithmetic expressions because we do not support constraints on integers Dec 22, 2020
@pavel-mikula-sonarsource
Copy link
Contributor

Another repro, from #4537

        private void Sample()
        {
            string someString = null;

            if (!someString?.Contains("a") ?? true)
                Console.WriteLine("It's null or doesn't contain 'a'");
            else
                Console.WriteLine(someString.Length); //S2259 reported here
        }

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: False Positive Rule IS triggered when it shouldn't be. labels Jun 25, 2021
@andrei-epure-sonarsource
Copy link
Contributor

andrei-epure-sonarsource commented Jun 29, 2021

@pavel-mikula-sonarsource I guess after adding this reproducer, the title should be updated as well? currently it's "... because we do not support constraints on integers" ?

LE: actually I saw your update on the #4537 (comment) , so there's a separate ticket tracking this (#4537)

@bkromhout
Copy link

bkromhout commented Oct 29, 2021

Can confirm we're also seeing this in our source. We don't want to disable SS2259, but alternatives are either suppressing in source (gross), or adding redundant null checks (annoying). Please consider finding a way to address this soon, it's been two years since it was initially reported.

@Simply-Coder
Copy link

Simply-Coder commented May 6, 2022

I am also facing same error while using sonar cloud on below line. What would be fix from sonar cloud?

Var httpconnectextension= httpAccrssor.HttpContext==null?null: Httpconnectextension.current;

@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 8.43 milestone Jul 18, 2022
@pavel-mikula-sonarsource
Copy link
Contributor

We should take #4537 first to see if it fixes this one too

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. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. Type: False Negative Rule is NOT triggered when it should be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants