Java S2095 False Positive stream

404 views
Skip to first unread message

alexandre....@gmail.com

unread,
Jan 13, 2016, 6:56:28 AM1/13/16
to SonarQube
The folowing lines are creating an issue S2095 ("Close this stream") :

        Arrays.stream(getTransportAddresses().split(",")).forEach(s -> {
            String[] address = s.split(":");
            localClient.addTransportAddress(new InetSocketTransportAddress(address[0], Integer.parseInt(address[1])));
        });

Obviously this stream does not have to be closed...

Michael Gumowski

unread,
Jan 13, 2016, 10:33:48 AM1/13/16
to alexandre....@gmail.com, SonarQube
Hello Alexandre,

This case should be correctly handled by the last version of the java plugin (3.9). Which version are you using?

Regards,

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
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/719efe2e-6c40-491c-b9df-e7d862b1077a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

alexandre....@gmail.com

unread,
Jan 13, 2016, 10:43:50 AM1/13/16
to SonarQube, alexandre....@gmail.com
Hello Michael,

Thanks for your rapid answer.

I'm using SonarQube 5.3 with JavaPlugin 3.9. This new issue appears after the update from sonarqube 5.2 to 5.3.
Note that the new version has also detected real issues (close the Reader for exemple) that were not detected with 5.2 version.

Michael Gumowski

unread,
Jan 13, 2016, 12:01:53 PM1/13/16
to Cartapanis Alexandre, SonarQube
Thanks for your answer.
I was not able to reproduce it locally with SQ 5.3 and java 3.9, but with as slightly adapted code.
To be sure, can you precise on which line of the code snippet you provided is the issue? (I guess the first one, but as I don't know what is your 'InetSocketTransportAddress' object [from elasticsearch?], I can't be sure).

Can you provide a self-contained code snippet reproducing the issue? It would really helps us identifying the problem with the implementation.

Regards,

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com

alexandre....@gmail.com

unread,
Jan 13, 2016, 1:10:59 PM1/13/16
to SonarQube, alexandre....@gmail.com
The issue is on the first line of the snippet, and yes InetSocketTransportAddress is coming from elasticsearch (org.elasticsearch.common.transport.InetSocketTransportAddress), and is not implementing Closeable or AutoCloseable (nor her parents).

This snippet is also throwing the same issue : 

        java.util.Arrays.stream("toto:1,tata:2".split(",")).forEach(s -> {
            String[] address = s.split(":");
            System.out.printf("%s:%d%n", address[0], Integer.parseInt(address[1]));
        });

This even simpler snippet also throw the same issue :

        Arrays.stream("toto,tata".split(",")).forEach(s -> System.out.println(s));

Michael Gumowski

unread,
Jan 14, 2016, 2:57:53 AM1/14/16
to Cartapanis Alexandre, SonarQube
Thanks a lot! Reproduced, and indeed this case should not raise issues. It will be fixed with resolution of the following JIRA ticket: https://jira.sonarsource.com/browse/SONARJAVA-1478

Regards,

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com

alexandre....@gmail.com

unread,
Jan 14, 2016, 4:46:41 AM1/14/16
to SonarQube, alexandre....@gmail.com
Thanks to you, i will follow bug resolution :)
Message has been deleted

Meindert

unread,
Feb 16, 2016, 5:10:39 AM2/16/16
to SonarQube
I think I have the same issue with the Spring ApplicationContext (as in 
 ApplicationContext ctx = SpringApplication.run(Application.class, args);
see: http://projects.spring.io/spring-boot/#quick-start)

Does the existing issue also cover the above example?

Env: SonarQube 5.3 with Java plugin 3.10

Sincerely,

Meindert

Op donderdag 14 januari 2016 08:57:53 UTC+1 schreef Michael Gumowski:

afo...@mobiquityinc.com

unread,
Aug 29, 2017, 10:16:47 AM8/29/17
to SonarQube
Hi Michael, 

sorry, first time I'm posting here.

We are using SonarQube 5.6 and we updated to SonarJava 4.12 yesterday.

It seems that https://jira.sonarsource.com/browse/SONARJAVA-1478 has been re-introduced?

It is complaining about this code:
 final List<java.nio.file.Path> files = java.nio.file.Files.list(parentDirectory).collect(toList());
 
 with parentDirectory being a java.nio.file.Path


Kind regards,

Alfred Foltin

Michael Gumowski

unread,
Aug 30, 2017, 12:06:27 PM8/30/17
to afo...@mobiquityinc.com, SonarQube
Hey Alfred,

We reworked the rule and came to the conclusion that this case was still valid: calling Files.list(parentDirectory) requires closing.
See the related thread from this ML: Java false positive for Files.list(Path)

Cheers,
Michael


For more options, visit https://groups.google.com/d/optout.
--
Michael Gumowski | SonarSource
Software Developer, Language Team
http://sonarsource.com

afo...@mobiquityinc.com

unread,
Aug 31, 2017, 2:18:58 AM8/31/17
to SonarQube, afo...@mobiquityinc.com
Thank you for your explanation and the quick reply!
Reply all
Reply to author
Forward
0 new messages