FP on Java (Eclipse/SonarLint) S2259 and S2583 ?

42 views
Skip to first unread message

friso.v...@gmail.com

unread,
Apr 6, 2018, 8:59:24 AM4/6/18
to SonarQube
Hi all,

I think I found two false positives in the following code fragment: 

    public static String s2583Fail(String a) {
       
try {
           
int length = a.length(); // after this, a cannot be null but if a == null it will throw a
                                     
// NullPointerException
           
return a + length;
       
} catch (Exception e) {
           
return a == null ? "No a: " + e.toString() : a; // SonarLint warns about S2583, but 'a' *can* be null
       
}
   
}


   
public static void main(String[] args) {
        LOGGER
.info(s2583Fail(null)); // S2259: SonarLint says a NPE will be thrown here, which it won't
   
}

Tried to keep the sample as short as possible, but hope this is clear. If it's not I'll gladly elaborate. 

Groeten,

Friso

Michael Gumowski

unread,
Jun 5, 2018, 6:04:00 AM6/5/18
to friso.v...@gmail.com, SonarQube
Hello Friso,

Unexpected null pointer exceptions are indeed not well handled by SonarJava at the moment.
The following ticket has been created recently to handle such cases and correctly branch to catch block: SONARJAVA-2669

Regards,
Michael

--
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/5014d8b7-0b74-49d8-a2fb-f0664b3e073f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael Gumowski | SonarSource
Software Developer, Language Team
https://www.sonarsource.com
Reply all
Reply to author
Forward
0 new messages