Skip to content

Commit

Permalink
Fix resource leak in loadAppKeyStore()
Browse files Browse the repository at this point in the history
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
Flowdalic committed Apr 22, 2015
1 parent 1ee2c25 commit 190c57a
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/de/duenndns/ssl/MemorizingTrustManager.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 190c57a

Please sign in to comment.