diff options
author | Martin Kellogg <kelloggm@cs.washington.edu> | 2021-05-15 23:50:02 +0800 |
---|---|---|
committer | maoling <maoling@apache.org> | 2021-05-15 23:50:02 +0800 |
commit | 766e173e9d51b6354920ebc136b246d221b87ec1 (patch) | |
tree | d46b40a5937d8d0c6842aa92d882504b9a794778 /zookeeper-server | |
parent | f5c29aab9adb49d9d3580074d4d1a805579fa20d (diff) | |
download | zookeeper-766e173e9d51b6354920ebc136b246d221b87ec1.tar.gz |
ZOOKEEPER-4246: Resource leaks in org.apache.zookeeper.server.persistence.SnapStream#getInputStream and #getOutputStream
Bug report is here: https://issues.apache.org/jira/browse/ZOOKEEPER-4246
This fix is simple: it just closes the possibly-leaked streams and re-throws the exception. We can't use a try-with-resources or a `finally` block here because in the happy case the resulting streams need to be returned open. I checked each of the other constructor calls in these methods, and none of the others can throw an exception as far as I can tell.
Author: Martin Kellogg <kelloggm@cs.washington.edu>
Reviewers: Andor Molnar <anmolnar@apache.org>, Enrico Olivelli <eolivelli@apache.org>, maoling <maoling@apache.org>
Closes #1638 from kelloggm/ZOOKEEPER-4246 and squashes the following commits:
fa1c0f08f [Martin Kellogg] surround whole switch with one try block instead of two inside the switch
f023851ba [Martin Kellogg] remove all the tabs for real
04c4b2fa6 [Martin Kellogg] fix accidental tab
e896efa6a [Martin Kellogg] ZOOKEEPER-4246: Resource leaks in org.apache.zookeeper.server.persistence.SnapStream#getInputStream and #getOutputStream
Diffstat (limited to 'zookeeper-server')
-rw-r--r-- | zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java index e73df4351..9bc8ee519 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java @@ -102,18 +102,23 @@ public class SnapStream { public static CheckedInputStream getInputStream(File file) throws IOException { FileInputStream fis = new FileInputStream(file); InputStream is; - switch (getStreamMode(file.getName())) { - case GZIP: - is = new GZIPInputStream(fis); - break; - case SNAPPY: - is = new SnappyInputStream(fis); - break; - case CHECKED: - default: - is = new BufferedInputStream(fis); + try { + switch (getStreamMode(file.getName())) { + case GZIP: + is = new GZIPInputStream(fis); + break; + case SNAPPY: + is = new SnappyInputStream(fis); + break; + case CHECKED: + default: + is = new BufferedInputStream(fis); + } + return new CheckedInputStream(is, new Adler32()); + } catch (IOException e) { + fis.close(); + throw e; } - return new CheckedInputStream(is, new Adler32()); } /** @@ -129,9 +134,16 @@ public class SnapStream { OutputStream os; switch (streamMode) { case GZIP: - os = new GZIPOutputStream(fos); + try { + os = new GZIPOutputStream(fos); + } catch (IOException e) { + fos.close(); + throw e; + } break; case SNAPPY: + // Unlike SnappyInputStream, the SnappyOutputStream + // constructor cannot throw an IOException. os = new SnappyOutputStream(fos); break; case CHECKED: |