Browse Source

FS#runProcess: Fix OutputStream left unclosed after IOException

The runProcess method creates an OutputStream that is not managed by
a try-with-resource because it's manually closed and any IOException
raised by the close() method is explicitly ignored.

Suppress the resource warning with an explanatory comment.

Enclose the call to StreamGobbler#copy in an inner try-block, and move
the call to close() inside its finally block. This prevents the stream
from being left unclosed if StreamGobbler#copy raises IOException.

Change-Id: Idca9adfc4d87e0989d787ad8239c055c0c849814
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.0
David Pursehouse 7 years ago committed by Matthias Sohn
parent
commit
76b4ed6a85
  1. 4
      org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java

4
org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java

@ -1108,10 +1108,13 @@ public abstract class FS {
new StreamGobbler(process.getErrorStream(), errRedirect));
executor.execute(
new StreamGobbler(process.getInputStream(), outRedirect));
@SuppressWarnings("resource") // Closed in the finally block
OutputStream outputStream = process.getOutputStream();
try {
if (inRedirect != null) {
new StreamGobbler(inRedirect, outputStream).copy();
}
} finally {
try {
outputStream.close();
} catch (IOException e) {
@ -1122,6 +1125,7 @@ public abstract class FS {
// again. This causes another IOException. Since we ignore the
// IOException in StreamGobbler, we also ignore the exception here.
}
}
return process.waitFor();
} catch (IOException e) {
ioException = e;

Loading…
Cancel
Save