[java:S2095] FP when stream is wrapped in external method

Qube: Community 9.9

If a stream is created and not handled via try-with-resources and the stream is returned, the rule does not hit. That’s correct. But if it is pushed into an external method and a stream is returned by that method (probably to wrap the stream) and this result is returned, the rule produces an issue.

I think, in the described case the rule can safely assume, that the external method does not return a fresh stream and discard the passed in stream or what ever. Hence the issue is a FP.

Example:

public class X {
    public static InputStream wrap(InputStream in) {
        return new BufferedInputStream(in); // Wrap the passed in stream somehow.
    }
}
public InputStream foo() throws IOException {
    return X.wrap(new FileInputStream("foo.txt")); // FP
}

Hi Marvin,

From the analyzer perspective, X.wrap(new FileInputStream("foo.txt")) is not a false positive but a true positive. The file created by new FileInputStream("foo.txt") will not be properly closed if an exception occurs in the X.wrap(...) function. Even if the analyzer looks at the content of wrap(InputStream in), he will not be able to decide if an exception can occur during the new BufferedInputStream(in) .