[Java] False positive on squid:S2589, try catch block is seen as always returning true

13 views
Skip to first unread message

bartf...@gmail.com

unread,
Mar 10, 2018, 10:23:03 AM3/10/18
to SonarQube
Hi everyone,

When writing a short helper class to check if a tcp port is available, I encountered a false positive. The false positive is both reported in my local sonarlint and on instalint: http://instalint.io/#/Java/4.14.0/KJ9RjosPa7MyvibB

Code snippet:
- - - >8 - - - - - - - - - - - - - - - -
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Logger;

public class FreePort {

    private FreePort() {
    }

    public static void main(String[] args) {
        if(available()) { // <= issue reported here
            Logger.getAnonymousLogger().info("available");
        }
    }

    private static boolean available() {
        try {
            try (ServerSocket ss = new ServerSocket(4200)) {
                ss.setReuseAddress(true);
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}
- - - - - - - - - - - - - - - - 8< - - -

Best wishes from Munich!
Daniel

Nicolas Peru

unread,
Apr 27, 2018, 9:35:33 AM4/27/18
to bartf...@gmail.com, SonarQube
Hi Daniel, 

Sorry for late answer, thanks for the feedback and reproducer. This is a tricky one and might not be solved right away. 

This seems to be due to the nesting of try and the fact that the outer one is catching not, the inner one. In other words we are most probably not bubbling up the exception properly and thus we are not branching to the catch. 

Ticket created to handle the issue : https://jira.sonarsource.com/browse/SONARJAVA-2737

Cheers, 

--
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/dec6e17f-8755-475b-b1da-00b9d55e284f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas Peru | SonarSource
Reply all
Reply to author
Forward
0 new messages