SonarJS icon indicating copy to clipboard operation
SonarJS copied to clipboard

Fix FN S2259 (`null-dereference`): when property is called `length`

Open saberduck opened this issue 3 years ago • 2 comments

We don't detect the issue for S2259 (null-dereference) when property is called length

 var x;
  if (x === undefined) {
      console.log(x.length); // FN
  }

saberduck avatar Jun 14 '21 15:06 saberduck

Hi @saberduck , any update on this? I'd like to add that this issue not only happens with .length, Sonarlint doesn't detect not declared variables either. Example: var x; if (x === undefined) { console.log(x); // FN console.log(asdf); // Should be detected too } This is a big issue for me since we are trying to use this as a standard in the team, but seems to be not possible atm. Thanks.

neoligero avatar Oct 07 '21 06:10 neoligero

For this reproducer:

let x;
  if (x === undefined) {
    x.length; // FN
}

For some reason, isUndefinedOrNull() returns false for x at line 3 when the property is .length, and true when it's anything else. So it breaks the function of the CheckNullDereference() function.

ilia-kebets-sonarsource avatar Nov 01 '23 07:11 ilia-kebets-sonarsource