From 1c5d02830fd553b92913b3b50a3812cf0220441c Mon Sep 17 00:00:00 2001 From: Andrzej Hunt Date: Fri, 14 Apr 2017 20:58:59 -0700 Subject: [PATCH] Bug 1356693 - infer: fix RESOURCE_LEAK's in services r=Grisha The primary issue is that we use a throwing InputStreamReader constructor. If it throws, then any nested streams will be lost. We can fix that by using the non-throwing InputStreamReader constructor (which uses a Charset as the second parameter, instead of a String which causes an Exception to be thrown if it can't be parsed) We also simplify some nested Stream's a little: most of the Stream constructors don't throw, so there's no harm in not keeping individual references to those that don't throw - and that results in less Stream references for us to handle. MozReview-Commit-ID: 2hyRFGVmGnU --HG-- extra : rebase_source : 15dd97d28012a017326b01ae8ddc370c7f1ec484 --- .../java/org/mozilla/gecko/sync/Utils.java | 22 +++---------------- .../mozilla/gecko/sync/net/MozResponse.java | 7 +++--- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/Utils.java b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/Utils.java index 5047a528d234..0cec6f5138c9 100644 --- a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/Utils.java +++ b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/Utils.java @@ -31,6 +31,7 @@ import org.mozilla.apache.commons.codec.binary.Base64; import org.mozilla.gecko.background.common.log.Logger; import org.mozilla.gecko.background.nativecode.NativeCrypto; import org.mozilla.gecko.sync.setup.Constants; +import org.mozilla.gecko.util.IOUtils; import org.mozilla.gecko.util.StringUtils; import android.content.Context; @@ -488,14 +489,10 @@ public class Utils { throw new IllegalArgumentException("Passed null filename in readFile."); } - FileInputStream fis = null; - InputStreamReader isr = null; BufferedReader br = null; try { - fis = context.openFileInput(filename); - isr = new InputStreamReader(fis, StringUtils.UTF_8); - br = new BufferedReader(isr); + br = new BufferedReader(new InputStreamReader(context.openFileInput(filename), StringUtils.UTF_8)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { @@ -505,20 +502,7 @@ public class Utils { } catch (Exception e) { return null; } finally { - if (isr != null) { - try { - isr.close(); - } catch (IOException e) { - // Ignore. - } - } - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - // Ignore. - } - } + IOUtils.safeStreamClose(br); } } diff --git a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/MozResponse.java b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/MozResponse.java index 3792abbeda8c..da389fe539ab 100644 --- a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/MozResponse.java +++ b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/MozResponse.java @@ -18,6 +18,7 @@ import org.mozilla.gecko.background.common.log.Logger; import org.mozilla.gecko.sync.ExtendedJSONObject; import org.mozilla.gecko.sync.NonArrayJSONException; import org.mozilla.gecko.sync.NonObjectJSONException; +import org.mozilla.gecko.util.IOUtils; import org.mozilla.gecko.util.StringUtils; import ch.boye.httpclientandroidlib.Header; @@ -106,12 +107,12 @@ public class MozResponse { throw new IOException("no entity"); } - InputStream content = entity.getContent(); + Reader in = null; try { - Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8")); + in = new BufferedReader(new InputStreamReader(entity.getContent(), StringUtils.UTF_8)); return new ExtendedJSONObject(in); } finally { - content.close(); + IOUtils.safeStreamClose(in); } }