Permalink
Browse files

Fix resource leak in loadAppKeyStore()

The FileInputStream was never closed.

Also improve exception handling of the load(null, null) case, i.e. only
catch declared checked exceptions of load(). Luckily we can use
try/multi-catch since 1ee2c25.
  • Loading branch information...
1 parent 1ee2c25 commit 190c57a9a8385f4726c817924b123438af6adc2f @Flowdalic committed Apr 22, 2015
Showing with 19 additions and 4 deletions.
  1. +19 −4 src/de/duenndns/ssl/MemorizingTrustManager.java
@@ -39,11 +39,14 @@
import android.os.Handler;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
import java.security.cert.*;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.SimpleDateFormat;
@@ -317,12 +320,24 @@ KeyStore loadAppKeyStore() {
}
try {
ks.load(null, null);
- ks.load(new java.io.FileInputStream(keyStoreFile), "MTM".toCharArray());
- } catch (java.io.FileNotFoundException e) {
- LOGGER.log(Level.INFO, "getAppKeyStore(" + keyStoreFile + ") - file does not exist");
- } catch (Exception e) {
+ } catch (NoSuchAlgorithmException | CertificateException | IOException e) {
LOGGER.log(Level.SEVERE, "getAppKeyStore(" + keyStoreFile + ")", e);
}
+ InputStream is = null;
+ try {
+ is = new java.io.FileInputStream(keyStoreFile);
+ ks.load(is, "MTM".toCharArray());
+ } catch (NoSuchAlgorithmException | CertificateException | IOException e) {
+ LOGGER.log(Level.INFO, "getAppKeyStore(" + keyStoreFile + ") - exception loading file key store");
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ LOGGER.log(Level.FINE, "getAppKeyStore(" + keyStoreFile + ") - exception closing file key store input stream");
+ }
+ }
+ }
return ks;
}

0 comments on commit 190c57a

Please sign in to comment.