Only detects the first resource closure in finally block

594 views
Skip to first unread message

adrien....@gmail.com

unread,
Feb 9, 2017, 12:04:21 PM2/9/17
to SonarQube
Hello all,

I've got this little sample of code :

        FileInputStream fis = null;
       
FileOutputStream fos = null;
       
FileOutputStream fos2 = null;
       
       
try {
            fis
= new FileInputStream(new File(""));
            fos
= new FileOutputStream(new File(""));
            fos2
= new FileOutputStream(new File(""));
           
           
// ...
       
} catch (IOException e) {
           
// log
       
} finally {
           
if (fis != null) {
               
try {
                    fis
.close();
               
} catch (IOException e) {
                   
// log
               
}
           
}
           
if (fos != null) {
               
try {
                    fos
.close();
               
} catch (IOException e) {
                   
// log
               
}
           
}
           
if (fos2 != null) {
               
try {
                    fos2
.close();
               
} catch (IOException e) {
                   
// log
               
}
           
}
       
}

Sonar says the two last resources (in red, above) should be closed, but they are closed in the finally block exactly like the first resource, which does not return any error.
And if I switch, for example, the fos closure and the fis closure, the following lines are highlighted, just like it just cares about the first "try/catch" (or "if") block in the finally block :

FileInputStream fis = null;
       
FileOutputStream fos = null;
       
FileOutputStream fos2 = null;
       
       
try {
           
fis = new FileInputStream(new File(""));
            fos
= new FileOutputStream(new File(""));
           
fos2 = new FileOutputStream(new File(""));
           
           
// ...
       
} catch (IOException e) {
           
// log
       
} finally {
           
if (fos != null) {
               
try {
                    fos
.close();
               
} catch (IOException e) {
                   
// log
               
}
           
}
           
if (fis != null) {
               
try {
                    fis
.close();
               
} catch (IOException e) {
                   
// log
               
}
           
}
           
if (fos2 != null) {
               
try {
                    fos2
.close();
               
} catch (IOException e) {
                   
// log
               
}
           
}
       
}

I use the last stable SonarLint version, and SonarQube 6.1.

Best regards,
Adrien

Tibor Blenessy

unread,
Feb 9, 2017, 1:10:50 PM2/9/17
to adrien....@gmail.com, SonarQube
Hello,

can you tell which version of SonarJava analyzer you are using (you can find this information in the update center of your SonarQube instance)? Thanks

Best regards

--
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/a810512c-5fc3-4a0b-8dd7-6bd1ab115706%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Tibor BLENESSY | SonarSource
SonarJava Developer

adrien....@gmail.com

unread,
Feb 9, 2017, 1:22:05 PM2/9/17
to SonarQube, adrien....@gmail.com
Hello Tibor,

I use the last version of Java analyzer (4.5.0.8398).
But I faced this issue with an older version too (I can't remember which one, just 4.2 or later).
I was hoping the issue will be gone the last version ;)

Best regards,
Adrien

adrien....@gmail.com

unread,
Feb 16, 2017, 4:54:30 AM2/16/17
to SonarQube, adrien....@gmail.com
Hi all,

Is there any update about this issue ?

Best regards,
Adrien

Tibor Blenessy

unread,
Mar 13, 2017, 11:10:45 AM3/13/17
to adrien....@gmail.com, SonarQube
Hello Adrien,

sorry for the delay. I am only able to reproduce your issue when catch block around call to close() is empty. Can you confirm that this is the case? Try to add some statement to the catch blocks and see if you can still reproduce the issue.

Thanks




For more options, visit https://groups.google.com/d/optout.

adrien....@gmail.com

unread,
Mar 28, 2017, 10:59:20 AM3/28/17
to SonarQube, adrien....@gmail.com
Hello Tibor,

Don't worry about that and sorry for the delay too.
I confirm your saying : with any statement in catch block around call to close(), the false positive for the next try/catch block calling close() disappears.

Best regards,
Adrien

Tibor Blenessy

unread,
Mar 28, 2017, 11:12:26 AM3/28/17
to adrien....@gmail.com, SonarQube
Thanks for coming back with this. I already created a ticket to fix the issue


Regards 
Reply all
Reply to author
Forward
0 new messages