[JAVA] 3.9 - FP for S2259 Null pointers should not be dereferenced

92 views
Skip to first unread message

andreas...@coremedia.com

unread,
Jan 20, 2016, 3:00:43 AM1/20/16
to SonarQube
Hi,

version 3.9 of the Java Plugin shows a false positive for rule S2583 in the following code. Is this a known issue? Is there a JIRA ticket or can you open one?


public class FpS2259 {

 
public void test() {
   
Foo foo = new Foo();
   
boolean fooCalled = false;
   
Bar bar;
   
try {
      bar
= new Bar();
      foo
.foo();
      fooCalled
= true;
   
} finally {
     
if (!fooCalled) {
        foo
.foo();
     
}
   
}

    bar
.bar(); // <------- reported violation: NullPointerException might be thrown as 'bar' is nullable here
  }

 
private static class Foo {
   
public void foo() {}
 
}

 
private static class Bar {
   
public void bar() {}
 
}
}


Cheers,
Andreas

Nicolas Peru

unread,
Jan 20, 2016, 4:13:57 AM1/20/16
to andreas...@coremedia.com, SonarQube
Hi Andreas,
Thanks for the feedback. 

This sounds indeed like a bug in the flow when an exception is raised.
The approximation of the try catch flow is such that we consider an exception can be thrown just after the curly brace of the try. So Bar might not be initialized but then the last line of your example should not be evaluated in this case. Ticket opened : https://jira.sonarsource.com/browse/SONARJAVA-1490

Thanks a lot for the minimalized reproducer !

Cheers



Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com


--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/4752a999-246e-42c2-bbdc-8a44a5564ce9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages