facebook / facebook/infer

False positive resource leak in Java

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

Description

I have this code using a **try-with-resources** and Infer still generate a warning :
I am using Infer version v0.9.5.

```
try (
// Let's anticipate up to 80 characters on top of those already read.
ByteArrayOutputStream out = new ByteArrayOutputStream(end - pos + 80) {
@Override
public String toString() {
int length = (count > 0 && buf[count - 1] == CR) ? count - 1 : count;
try {
return new String(buf, 0, length, charset.name());
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // Since we control the charset this will never happen.
}
}
}
) {

while (true) {
out.write(buf, pos, end - pos);
// Mark unterminated line in case fillBuf throws EOFException or IOException.
end = -1;
fillBuf();
// Try to find LF in the buffered data and return the line if successful.
for (int i = pos; i != end; ++i) {
if (buf[i] == LF) {
if (i != pos) {
out.write(buf, pos, i - pos);
}
pos = i + 1;
return out.toString();
}
}
}
}
```

```
RESOURCE_LEAK ERROR HIGH
resource of type ...cache.lru.StrictLineReader$1 acquired to out by call to new()
at line 151 is not released after line 180
```

line 151 is `ByteArrayOutputStream out = new ByteArrayOutputStream(end - pos + 80) {`

Looks like this is a false-positive, is there an issue with Infer ?

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.