facebook / facebook/infer

Resource Leak FP?

Open
#999 4 comments 0 reactions 0 assignees View on GitHub
false-positive java
Dominant language
OCaml
Stars
15.7k
Forks
2.1k
Avg merge
19h 36m
Merged PRs (30d)
13

Description

For the code given below:

```
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.InputStream;
import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;

public class Test {
public static void main(String[] args) {
char[] chars = new char[100];
int c;
InputStreamReader isr = null;
try (InputStream inputStreamReader = new FileInputStream("nl-data.txt")) {
isr = new InputStreamReader(inputStreamReader, StandardCharsets.UTF_8);
c = isr.read();
} catch (IOException e) {
e.printStackTrace();
}

try {
while((c = isr.read()) > -1) {
System.out.println(c);
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
```
the following two issues are reported. Infer command line:
`infer -a checkers -- javac Test.java`

```
Found 2 issues

Test.java:22: error: RESOURCE_LEAK
resource of type `java.io.InputStreamReader` acquired to `isr` by call to `new()` at line 15 is not released after line 22.
20.
21. try {
22. > while((c = isr.read()) > -1) {
23.
24. }

Test.java:26: error: RESOURCE_LEAK
resource of type `java.io.InputStreamReader` acquired to `isr` by call to `new()` at line 15 is not released after line 26.
**Note**: potential exception at line 22
24. }
25. }
26. > catch(IOException e) {
27. e.printStackTrace();
28. }

```

Due to the `try-with-resources`, `isr` will indeed be closed when the execution goes beyond the first `try`. I can confirm it based on the program execution where an IOException is indeed thrown on `Stream Closed`.

1) Is this a false positive by Infer? Or is there some corner case that is not very obvious?
2) If it is a FP, is there an option to suppress such false positives?

I am trying to deploy the Infer tool for a large codebase and this (and similar looking) defects are shown to avoid deployment. So, any suggestions on checker options to improve precision is welcome.

Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.