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
This commit is contained in:
Andrzej Hunt 2017-04-14 20:58:59 -07:00
Родитель 9614743c8f
Коммит 1c5d02830f
2 изменённых файлов: 7 добавлений и 22 удалений

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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);
}
}