From f5aa36ef9f092240f99cb64b83cc64b91544d638 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 17 Jun 2015 21:30:25 +0200 Subject: fix rest of resource leaks (#1351) --- .../keychain/operations/ImportExportOperation.java | 16 ++++--- .../keychain/provider/KeychainDatabase.java | 13 ++++-- .../keychain/remote/ui/AppSettingsActivity.java | 4 +- .../keychain/ui/KeyListFragment.java | 28 +++++++---- .../ui/dialog/AddKeyserverDialogFragment.java | 54 +++++++++------------- .../keychain/ui/widget/PasswordStrengthView.java | 28 +++++------ 6 files changed, 78 insertions(+), 65 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java index ffce2f39c..cdda42dae 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java @@ -411,13 +411,17 @@ public class ImportExportOperation extends BaseOperation { try { OutputStream outStream = new FileOutputStream(outputFile); - ExportResult result = exportKeyRings(log, masterKeyIds, exportSecret, outStream); - if (result.cancelled()) { - //noinspection ResultOfMethodCallIgnored - new File(outputFile).delete(); + try { + ExportResult result = exportKeyRings(log, masterKeyIds, exportSecret, outStream); + if (result.cancelled()) { + //noinspection ResultOfMethodCallIgnored + new File(outputFile).delete(); + } + return result; + } finally { + outStream.close(); } - return result; - } catch (FileNotFoundException e) { + } catch (IOException e) { log.add(LogType.MSG_EXPORT_ERROR_FOPEN, 1); return new ExportResult(ExportResult.RESULT_ERROR, log); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index 36ba47672..a91eca453 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -391,10 +391,15 @@ public class KeychainDatabase extends SQLiteOpenHelper { private static void copy(File in, File out) throws IOException { FileInputStream is = new FileInputStream(in); FileOutputStream os = new FileOutputStream(out); - byte[] buf = new byte[512]; - while (is.available() > 0) { - int count = is.read(buf, 0, 512); - os.write(buf, 0, count); + try { + byte[] buf = new byte[512]; + while (is.available() > 0) { + int count = is.read(buf, 0, 512); + os.write(buf, 0, count); + } + } finally { + is.close(); + os.close(); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java index 2b71d6dc1..d25249b14 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java @@ -217,13 +217,15 @@ public class AppSettingsActivity extends BaseActivity { // show accounts only if available (deprecated API) Cursor cursor = getContentResolver().query(accountsUri, null, null, null, null); - if (cursor.moveToFirst()) { + if (cursor != null && cursor.moveToFirst()) try { mAccountsLabel.setVisibility(View.VISIBLE); mAccountsListFragment = AccountsListFragment.newInstance(accountsUri); // Create an instance of the fragments getSupportFragmentManager().beginTransaction() .replace(R.id.api_accounts_list_fragment, mAccountsListFragment) .commitAllowingStateLoss(); + } finally { + cursor.close(); } // Create an instance of the fragments diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 36074f6ba..780558b27 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -71,6 +71,7 @@ import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter; import org.sufficientlysecure.keychain.ui.util.Notify; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.util.ExportHelper; import org.sufficientlysecure.keychain.util.FabContainer; import org.sufficientlysecure.keychain.util.Log; @@ -553,9 +554,12 @@ public class KeyListFragment extends LoaderFragment } private void updateAllKeys() { - Context context = getActivity(); + Activity activity = getActivity(); + if (activity == null) { + return; + } - ProviderHelper providerHelper = new ProviderHelper(context); + ProviderHelper providerHelper = new ProviderHelper(activity); Cursor cursor = providerHelper.getContentResolver().query( KeyRings.buildUnifiedKeyRingsUri(), new String[]{ @@ -563,13 +567,21 @@ public class KeyListFragment extends LoaderFragment }, null, null, null ); - ArrayList keyList = new ArrayList<>(); + if (cursor == null) { + Notify.create(activity, R.string.error_loading_keys, Style.ERROR); + return; + } - while (cursor.moveToNext()) { - byte[] blob = cursor.getBlob(0);//fingerprint column is 0 - String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); - ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); - keyList.add(keyEntry); + ArrayList keyList = new ArrayList<>(); + try { + while (cursor.moveToNext()) { + byte[] blob = cursor.getBlob(0);//fingerprint column is 0 + String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); + ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); + keyList.add(keyEntry); + } + } finally { + cursor.close(); } ServiceProgressHandler serviceHandler = new ServiceProgressHandler(getActivity()) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java index cfad76fc3..2c1714b67 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddKeyserverDialogFragment.java @@ -17,6 +17,12 @@ package org.sufficientlysecure.keychain.ui.dialog; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; + import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -29,8 +35,8 @@ import android.os.Bundle; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; +import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; -import android.test.suitebuilder.TestSuiteBuilder; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -47,15 +53,6 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.TlsHelper; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; - -import javax.net.ssl.HttpsURLConnection; - public class AddKeyserverDialogFragment extends DialogFragment implements OnEditorActionListener { private static final String ARG_MESSENGER = "messenger"; @@ -70,20 +67,11 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit private EditText mKeyserverEditText; private CheckBox mVerifyKeyserverCheckBox; - public static enum FailureReason { + public enum FailureReason { INVALID_URL, CONNECTION_FAILED } - ; - - /** - * Creates new instance of this dialog fragment - * - * @param title title of dialog - * @param messenger to communicate back after setting the passphrase - * @return - */ public static AddKeyserverDialogFragment newInstance(Messenger messenger) { AddKeyserverDialogFragment frag = new AddKeyserverDialogFragment(); Bundle args = new Bundle(); @@ -94,9 +82,7 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit return frag; } - /** - * Creates dialog - */ + @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); @@ -222,19 +208,23 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit String scheme = keyserverUri.getScheme(); String schemeSpecificPart = keyserverUri.getSchemeSpecificPart(); String fragment = keyserverUri.getFragment(); - if (scheme == null) throw new MalformedURLException(); - if (scheme.equalsIgnoreCase("hkps")) scheme = "https"; - else if (scheme.equalsIgnoreCase("hkp")) scheme = "http"; + if (scheme == null) { + throw new MalformedURLException(); + } + if ("hkps".equalsIgnoreCase(scheme)) { + scheme = "https"; + } else if ("hkp".equalsIgnoreCase(scheme)) { + scheme = "http"; + } URI newKeyserver = new URI(scheme, schemeSpecificPart, fragment); - Log.d("Converted URL", newKeyserver.toString()); - TlsHelper.openConnection(newKeyserver.toURL()).getInputStream(); + Log.d(Constants.TAG, "Converted URL" + newKeyserver); + + // just see if we can get a connection, then immediately close + TlsHelper.openConnection(newKeyserver.toURL()).getInputStream().close(); } catch (TlsHelper.TlsHelperException e) { reason = FailureReason.CONNECTION_FAILED; - } catch (MalformedURLException e) { - Log.w(Constants.TAG, "Invalid keyserver URL entered by user."); - reason = FailureReason.INVALID_URL; - } catch (URISyntaxException e) { + } catch (MalformedURLException | URISyntaxException e) { Log.w(Constants.TAG, "Invalid keyserver URL entered by user."); reason = FailureReason.INVALID_URL; } catch (IOException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java index 1487c3053..0ec145657 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PasswordStrengthView.java @@ -97,26 +97,23 @@ public class PasswordStrengthView extends View { public PasswordStrengthView(Context context, AttributeSet attrs) { super(context, attrs); - int COLOR_FAIL = context.getResources().getColor(R.color.android_red_light); - int COLOR_WEAK = context.getResources().getColor(R.color.android_orange_light); - int COLOR_STRONG = context.getResources().getColor(R.color.android_green_light); + int COLOR_FAIL = getResources().getColor(R.color.android_red_light); + int COLOR_WEAK = getResources().getColor(R.color.android_orange_light); + int COLOR_STRONG = getResources().getColor(R.color.android_green_light); TypedArray style = context.getTheme().obtainStyledAttributes( attrs, R.styleable.PasswordStrengthView, 0, 0); - try { - mStrengthRequirement = style.getInteger(R.styleable.PasswordStrengthView_strength, - STRENGTH_MEDIUM); - mShowGuides = style.getBoolean(R.styleable.PasswordStrengthView_showGuides, true); - mColorFail = style.getColor(R.styleable.PasswordStrengthView_color_fail, COLOR_FAIL); - mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK); - mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong, - COLOR_STRONG); - } catch (Exception e) { - e.printStackTrace(); - } + mStrengthRequirement = style.getInteger(R.styleable.PasswordStrengthView_strength, + STRENGTH_MEDIUM); + mShowGuides = style.getBoolean(R.styleable.PasswordStrengthView_showGuides, true); + mColorFail = style.getColor(R.styleable.PasswordStrengthView_color_fail, COLOR_FAIL); + mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK); + mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong, + COLOR_STRONG); + // Create and style the paint used for drawing the guide on the indicator mGuidePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mGuidePaint.setStyle(Paint.Style.FILL_AND_STROKE); @@ -124,6 +121,9 @@ public class PasswordStrengthView extends View { // Create and style paint for indicator mIndicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mIndicatorPaint.setStyle(Paint.Style.FILL); + + style.recycle(); + } /** -- cgit v1.2.3