fix rest of resource leaks (#1351)

This commit is contained in:
Vincent Breitmoser 2015-06-17 21:30:25 +02:00
parent 5895385153
commit f5aa36ef9f
7 changed files with 79 additions and 65 deletions

View File

@ -411,13 +411,17 @@ public class ImportExportOperation extends BaseOperation {
try { try {
OutputStream outStream = new FileOutputStream(outputFile); OutputStream outStream = new FileOutputStream(outputFile);
try {
ExportResult result = exportKeyRings(log, masterKeyIds, exportSecret, outStream); ExportResult result = exportKeyRings(log, masterKeyIds, exportSecret, outStream);
if (result.cancelled()) { if (result.cancelled()) {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(outputFile).delete(); new File(outputFile).delete();
} }
return result; return result;
} catch (FileNotFoundException e) { } finally {
outStream.close();
}
} catch (IOException e) {
log.add(LogType.MSG_EXPORT_ERROR_FOPEN, 1); log.add(LogType.MSG_EXPORT_ERROR_FOPEN, 1);
return new ExportResult(ExportResult.RESULT_ERROR, log); return new ExportResult(ExportResult.RESULT_ERROR, log);
} }

View File

@ -391,11 +391,16 @@ public class KeychainDatabase extends SQLiteOpenHelper {
private static void copy(File in, File out) throws IOException { private static void copy(File in, File out) throws IOException {
FileInputStream is = new FileInputStream(in); FileInputStream is = new FileInputStream(in);
FileOutputStream os = new FileOutputStream(out); FileOutputStream os = new FileOutputStream(out);
try {
byte[] buf = new byte[512]; byte[] buf = new byte[512];
while (is.available() > 0) { while (is.available() > 0) {
int count = is.read(buf, 0, 512); int count = is.read(buf, 0, 512);
os.write(buf, 0, count); os.write(buf, 0, count);
} }
} finally {
is.close();
os.close();
}
} }
public static void debugBackup(Context context, boolean restore) throws IOException { public static void debugBackup(Context context, boolean restore) throws IOException {

View File

@ -217,13 +217,15 @@ public class AppSettingsActivity extends BaseActivity {
// show accounts only if available (deprecated API) // show accounts only if available (deprecated API)
Cursor cursor = getContentResolver().query(accountsUri, null, null, null, null); Cursor cursor = getContentResolver().query(accountsUri, null, null, null, null);
if (cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) try {
mAccountsLabel.setVisibility(View.VISIBLE); mAccountsLabel.setVisibility(View.VISIBLE);
mAccountsListFragment = AccountsListFragment.newInstance(accountsUri); mAccountsListFragment = AccountsListFragment.newInstance(accountsUri);
// Create an instance of the fragments // Create an instance of the fragments
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.api_accounts_list_fragment, mAccountsListFragment) .replace(R.id.api_accounts_list_fragment, mAccountsListFragment)
.commitAllowingStateLoss(); .commitAllowingStateLoss();
} finally {
cursor.close();
} }
// Create an instance of the fragments // Create an instance of the fragments

View File

@ -71,6 +71,7 @@ import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter; import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter;
import org.sufficientlysecure.keychain.ui.util.Notify; 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.ExportHelper;
import org.sufficientlysecure.keychain.util.FabContainer; import org.sufficientlysecure.keychain.util.FabContainer;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
@ -553,9 +554,12 @@ public class KeyListFragment extends LoaderFragment
} }
private void updateAllKeys() { 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( Cursor cursor = providerHelper.getContentResolver().query(
KeyRings.buildUnifiedKeyRingsUri(), new String[]{ KeyRings.buildUnifiedKeyRingsUri(), new String[]{
@ -563,14 +567,22 @@ public class KeyListFragment extends LoaderFragment
}, null, null, null }, null, null, null
); );
ArrayList<ParcelableKeyRing> keyList = new ArrayList<>(); if (cursor == null) {
Notify.create(activity, R.string.error_loading_keys, Style.ERROR);
return;
}
ArrayList<ParcelableKeyRing> keyList = new ArrayList<>();
try {
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
byte[] blob = cursor.getBlob(0);//fingerprint column is 0 byte[] blob = cursor.getBlob(0);//fingerprint column is 0
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob);
ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null);
keyList.add(keyEntry); keyList.add(keyEntry);
} }
} finally {
cursor.close();
}
ServiceProgressHandler serviceHandler = new ServiceProgressHandler(getActivity()) { ServiceProgressHandler serviceHandler = new ServiceProgressHandler(getActivity()) {
@Override @Override

View File

@ -17,6 +17,12 @@
package org.sufficientlysecure.keychain.ui.dialog; 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.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
@ -29,8 +35,8 @@ import android.os.Bundle;
import android.os.Message; import android.os.Message;
import android.os.Messenger; import android.os.Messenger;
import android.os.RemoteException; import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.test.suitebuilder.TestSuiteBuilder;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -47,15 +53,6 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.TlsHelper; 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 { public class AddKeyserverDialogFragment extends DialogFragment implements OnEditorActionListener {
private static final String ARG_MESSENGER = "messenger"; private static final String ARG_MESSENGER = "messenger";
@ -70,20 +67,11 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit
private EditText mKeyserverEditText; private EditText mKeyserverEditText;
private CheckBox mVerifyKeyserverCheckBox; private CheckBox mVerifyKeyserverCheckBox;
public static enum FailureReason { public enum FailureReason {
INVALID_URL, INVALID_URL,
CONNECTION_FAILED 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) { public static AddKeyserverDialogFragment newInstance(Messenger messenger) {
AddKeyserverDialogFragment frag = new AddKeyserverDialogFragment(); AddKeyserverDialogFragment frag = new AddKeyserverDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
@ -94,9 +82,7 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit
return frag; return frag;
} }
/** @NonNull
* Creates dialog
*/
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity(); final Activity activity = getActivity();
@ -222,19 +208,23 @@ public class AddKeyserverDialogFragment extends DialogFragment implements OnEdit
String scheme = keyserverUri.getScheme(); String scheme = keyserverUri.getScheme();
String schemeSpecificPart = keyserverUri.getSchemeSpecificPart(); String schemeSpecificPart = keyserverUri.getSchemeSpecificPart();
String fragment = keyserverUri.getFragment(); String fragment = keyserverUri.getFragment();
if (scheme == null) throw new MalformedURLException(); if (scheme == null) {
if (scheme.equalsIgnoreCase("hkps")) scheme = "https"; throw new MalformedURLException();
else if (scheme.equalsIgnoreCase("hkp")) scheme = "http"; }
if ("hkps".equalsIgnoreCase(scheme)) {
scheme = "https";
} else if ("hkp".equalsIgnoreCase(scheme)) {
scheme = "http";
}
URI newKeyserver = new URI(scheme, schemeSpecificPart, fragment); URI newKeyserver = new URI(scheme, schemeSpecificPart, fragment);
Log.d("Converted URL", newKeyserver.toString()); Log.d(Constants.TAG, "Converted URL" + newKeyserver);
TlsHelper.openConnection(newKeyserver.toURL()).getInputStream();
// just see if we can get a connection, then immediately close
TlsHelper.openConnection(newKeyserver.toURL()).getInputStream().close();
} catch (TlsHelper.TlsHelperException e) { } catch (TlsHelper.TlsHelperException e) {
reason = FailureReason.CONNECTION_FAILED; reason = FailureReason.CONNECTION_FAILED;
} catch (MalformedURLException e) { } catch (MalformedURLException | URISyntaxException e) {
Log.w(Constants.TAG, "Invalid keyserver URL entered by user.");
reason = FailureReason.INVALID_URL;
} catch (URISyntaxException e) {
Log.w(Constants.TAG, "Invalid keyserver URL entered by user."); Log.w(Constants.TAG, "Invalid keyserver URL entered by user.");
reason = FailureReason.INVALID_URL; reason = FailureReason.INVALID_URL;
} catch (IOException e) { } catch (IOException e) {

View File

@ -97,16 +97,15 @@ public class PasswordStrengthView extends View {
public PasswordStrengthView(Context context, AttributeSet attrs) { public PasswordStrengthView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
int COLOR_FAIL = context.getResources().getColor(R.color.android_red_light); int COLOR_FAIL = getResources().getColor(R.color.android_red_light);
int COLOR_WEAK = context.getResources().getColor(R.color.android_orange_light); int COLOR_WEAK = getResources().getColor(R.color.android_orange_light);
int COLOR_STRONG = context.getResources().getColor(R.color.android_green_light); int COLOR_STRONG = getResources().getColor(R.color.android_green_light);
TypedArray style = context.getTheme().obtainStyledAttributes( TypedArray style = context.getTheme().obtainStyledAttributes(
attrs, attrs,
R.styleable.PasswordStrengthView, R.styleable.PasswordStrengthView,
0, 0); 0, 0);
try {
mStrengthRequirement = style.getInteger(R.styleable.PasswordStrengthView_strength, mStrengthRequirement = style.getInteger(R.styleable.PasswordStrengthView_strength,
STRENGTH_MEDIUM); STRENGTH_MEDIUM);
mShowGuides = style.getBoolean(R.styleable.PasswordStrengthView_showGuides, true); mShowGuides = style.getBoolean(R.styleable.PasswordStrengthView_showGuides, true);
@ -114,9 +113,7 @@ public class PasswordStrengthView extends View {
mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK); mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK);
mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong, mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong,
COLOR_STRONG); COLOR_STRONG);
} catch (Exception e) {
e.printStackTrace();
}
// Create and style the paint used for drawing the guide on the indicator // Create and style the paint used for drawing the guide on the indicator
mGuidePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mGuidePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mGuidePaint.setStyle(Paint.Style.FILL_AND_STROKE); mGuidePaint.setStyle(Paint.Style.FILL_AND_STROKE);
@ -124,6 +121,9 @@ public class PasswordStrengthView extends View {
// Create and style paint for indicator // Create and style paint for indicator
mIndicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mIndicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mIndicatorPaint.setStyle(Paint.Style.FILL); mIndicatorPaint.setStyle(Paint.Style.FILL);
style.recycle();
} }
/** /**

View File

@ -1329,5 +1329,6 @@
<string name="snack_armor_off">"Output encoded as Binary."</string> <string name="snack_armor_off">"Output encoded as Binary."</string>
<string name="snack_compression_on">"Compression <b>enabled</b>."</string> <string name="snack_compression_on">"Compression <b>enabled</b>."</string>
<string name="snack_compression_off">"Compression <b>disabled</b>."</string> <string name="snack_compression_off">"Compression <b>disabled</b>."</string>
<string name="error_loading_keys">Error loading keys!</string>
</resources> </resources>