mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-17 14:25:08 -05:00
proper null checks and closing of cursors everywhere
This commit is contained in:
parent
79117b1ef8
commit
cbc3988628
@ -112,16 +112,18 @@ public class FileHelper {
|
|||||||
|
|
||||||
if ("content".equalsIgnoreCase(uri.getScheme())) {
|
if ("content".equalsIgnoreCase(uri.getScheme())) {
|
||||||
String[] projection = {"_data"};
|
String[] projection = {"_data"};
|
||||||
Cursor cursor = null;
|
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cursor = context.getContentResolver().query(uri, projection, null, null, null);
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
int columnIndex = cursor.getColumnIndexOrThrow("_data");
|
int columnIndex = cursor.getColumnIndexOrThrow("_data");
|
||||||
if (cursor.moveToFirst()) {
|
|
||||||
return cursor.getString(columnIndex);
|
return cursor.getString(columnIndex);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Eat it
|
// Eat it
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
||||||
return uri.getPath();
|
return uri.getPath();
|
||||||
|
@ -255,16 +255,17 @@ public class KeychainDatabase extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
}.getReadableDatabase();
|
}.getReadableDatabase();
|
||||||
|
|
||||||
Cursor c = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
// we insert in two steps: first, all public keys that have secret keys
|
// we insert in two steps: first, all public keys that have secret keys
|
||||||
c = db.rawQuery("SELECT key_ring_data FROM key_rings WHERE type = 1 OR EXISTS ("
|
cursor = db.rawQuery("SELECT key_ring_data FROM key_rings WHERE type = 1 OR EXISTS ("
|
||||||
+ " SELECT 1 FROM key_rings d2 WHERE key_rings.master_key_id = d2.master_key_id"
|
+ " SELECT 1 FROM key_rings d2 WHERE key_rings.master_key_id = d2.master_key_id"
|
||||||
+ " AND d2.type = 1) ORDER BY type ASC", null);
|
+ " AND d2.type = 1) ORDER BY type ASC", null);
|
||||||
Log.d(Constants.TAG, "Importing " + c.getCount() + " secret keyrings from apg.db...");
|
Log.d(Constants.TAG, "Importing " + cursor.getCount() + " secret keyrings from apg.db...");
|
||||||
for (int i = 0; i < c.getCount(); i++) {
|
if (cursor != null) {
|
||||||
c.moveToPosition(i);
|
for (int i = 0; i < cursor.getCount(); i++) {
|
||||||
byte[] data = c.getBlob(0);
|
cursor.moveToPosition(i);
|
||||||
|
byte[] data = cursor.getBlob(0);
|
||||||
PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data);
|
PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||||
ProviderHelper providerHelper = new ProviderHelper(context);
|
ProviderHelper providerHelper = new ProviderHelper(context);
|
||||||
if (ring instanceof PGPPublicKeyRing)
|
if (ring instanceof PGPPublicKeyRing)
|
||||||
@ -275,18 +276,23 @@ public class KeychainDatabase extends SQLiteOpenHelper {
|
|||||||
Log.e(Constants.TAG, "Unknown blob data type!");
|
Log.e(Constants.TAG, "Unknown blob data type!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
|
||||||
// afterwards, insert all keys, starting with public keys that have secret keys, then
|
// afterwards, insert all keys, starting with public keys that have secret keys, then
|
||||||
// secret keys, then all others. this order is necessary to ensure all certifications
|
// secret keys, then all others. this order is necessary to ensure all certifications
|
||||||
// are recognized properly.
|
// are recognized properly.
|
||||||
c = db.rawQuery("SELECT key_ring_data FROM key_rings ORDER BY (type = 0 AND EXISTS ("
|
cursor = db.rawQuery("SELECT key_ring_data FROM key_rings ORDER BY (type = 0 AND EXISTS ("
|
||||||
+ " SELECT 1 FROM key_rings d2 WHERE key_rings.master_key_id = d2.master_key_id AND"
|
+ " SELECT 1 FROM key_rings d2 WHERE key_rings.master_key_id = d2.master_key_id AND"
|
||||||
+ " d2.type = 1)) DESC, type DESC", null);
|
+ " d2.type = 1)) DESC, type DESC", null);
|
||||||
// import from old database
|
// import from old database
|
||||||
Log.d(Constants.TAG, "Importing " + c.getCount() + " keyrings from apg.db...");
|
Log.d(Constants.TAG, "Importing " + cursor.getCount() + " keyrings from apg.db...");
|
||||||
for (int i = 0; i < c.getCount(); i++) {
|
if (cursor != null) {
|
||||||
c.moveToPosition(i);
|
for (int i = 0; i < cursor.getCount(); i++) {
|
||||||
byte[] data = c.getBlob(0);
|
cursor.moveToPosition(i);
|
||||||
|
byte[] data = cursor.getBlob(0);
|
||||||
PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data);
|
PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||||
ProviderHelper providerHelper = new ProviderHelper(context);
|
ProviderHelper providerHelper = new ProviderHelper(context);
|
||||||
if (ring instanceof PGPPublicKeyRing) {
|
if (ring instanceof PGPPublicKeyRing) {
|
||||||
@ -297,11 +303,12 @@ public class KeychainDatabase extends SQLiteOpenHelper {
|
|||||||
Log.e(Constants.TAG, "Unknown blob data type!");
|
Log.e(Constants.TAG, "Unknown blob data type!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "Error importing apg.db!", e);
|
Log.e(Constants.TAG, "Error importing apg.db!", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (c != null) {
|
if (cursor != null) {
|
||||||
c.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
if (db != null) {
|
if (db != null) {
|
||||||
db.close();
|
db.close();
|
||||||
|
@ -541,20 +541,21 @@ public class KeychainProvider extends ContentProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SQLiteDatabase db = getDb().getReadableDatabase();
|
SQLiteDatabase db = getDb().getReadableDatabase();
|
||||||
Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, having, orderBy);
|
Cursor cursor = qb.query(db, projection, selection, selectionArgs, groupBy, having, orderBy);
|
||||||
|
if (cursor != null) {
|
||||||
// Tell the cursor what uri to watch, so it knows when its source data changes
|
// Tell the cursor what uri to watch, so it knows when its source data changes
|
||||||
c.setNotificationUri(getContext().getContentResolver(), uri);
|
cursor.setNotificationUri(getContext().getContentResolver(), uri);
|
||||||
|
}
|
||||||
|
|
||||||
if (Constants.DEBUG) {
|
if (Constants.DEBUG) {
|
||||||
Log.d(Constants.TAG,
|
Log.d(Constants.TAG,
|
||||||
"Query: "
|
"Query: "
|
||||||
+ qb.buildQuery(projection, selection, selectionArgs, null, null,
|
+ qb.buildQuery(projection, selection, selectionArgs, null, null,
|
||||||
orderBy, null));
|
orderBy, null));
|
||||||
Log.d(Constants.TAG, "Cursor: " + DatabaseUtils.dumpCursorToString(c));
|
Log.d(Constants.TAG, "Cursor: " + DatabaseUtils.dumpCursorToString(cursor));
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,6 +100,7 @@ public class ProviderHelper {
|
|||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
Cursor cursor = mContentResolver.query(uri, proj, null, null, null);
|
Cursor cursor = mContentResolver.query(uri, proj, null, null, null);
|
||||||
|
|
||||||
|
try {
|
||||||
HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
|
HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
@ -125,11 +126,12 @@ public class ProviderHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} finally {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getUnifiedData(long masterKeyId, String column, int type)
|
public Object getUnifiedData(long masterKeyId, String column, int type)
|
||||||
@ -576,6 +578,7 @@ public class ProviderHelper {
|
|||||||
}, inMasterKeyList, null, null);
|
}, inMasterKeyList, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID);
|
int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID);
|
||||||
int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA);
|
int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA);
|
||||||
@ -594,10 +597,11 @@ public class ProviderHelper {
|
|||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (output.size() > 0) {
|
if (output.size() > 0) {
|
||||||
return output;
|
return output;
|
||||||
@ -610,6 +614,7 @@ public class ProviderHelper {
|
|||||||
Cursor cursor = mContentResolver.query(ApiApps.CONTENT_URI, null, null, null, null);
|
Cursor cursor = mContentResolver.query(ApiApps.CONTENT_URI, null, null, null, null);
|
||||||
|
|
||||||
ArrayList<String> packageNames = new ArrayList<String>();
|
ArrayList<String> packageNames = new ArrayList<String>();
|
||||||
|
try {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
int packageNameCol = cursor.getColumnIndex(ApiApps.PACKAGE_NAME);
|
int packageNameCol = cursor.getColumnIndex(ApiApps.PACKAGE_NAME);
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
@ -618,10 +623,11 @@ public class ProviderHelper {
|
|||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return packageNames;
|
return packageNames;
|
||||||
}
|
}
|
||||||
@ -668,13 +674,19 @@ public class ProviderHelper {
|
|||||||
public AppSettings getApiAppSettings(Uri uri) {
|
public AppSettings getApiAppSettings(Uri uri) {
|
||||||
AppSettings settings = null;
|
AppSettings settings = null;
|
||||||
|
|
||||||
Cursor cur = mContentResolver.query(uri, null, null, null, null);
|
Cursor cursor = mContentResolver.query(uri, null, null, null, null);
|
||||||
if (cur != null && cur.moveToFirst()) {
|
try {
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
settings = new AppSettings();
|
settings = new AppSettings();
|
||||||
settings.setPackageName(cur.getString(
|
settings.setPackageName(cursor.getString(
|
||||||
cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME)));
|
cursor.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME)));
|
||||||
settings.setPackageSignature(cur.getBlob(
|
settings.setPackageSignature(cursor.getBlob(
|
||||||
cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_SIGNATURE)));
|
cursor.getColumnIndex(KeychainContract.ApiApps.PACKAGE_SIGNATURE)));
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return settings;
|
return settings;
|
||||||
@ -683,20 +695,26 @@ public class ProviderHelper {
|
|||||||
public AccountSettings getApiAccountSettings(Uri accountUri) {
|
public AccountSettings getApiAccountSettings(Uri accountUri) {
|
||||||
AccountSettings settings = null;
|
AccountSettings settings = null;
|
||||||
|
|
||||||
Cursor cur = mContentResolver.query(accountUri, null, null, null, null);
|
Cursor cursor = mContentResolver.query(accountUri, null, null, null, null);
|
||||||
if (cur != null && cur.moveToFirst()) {
|
try {
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
settings = new AccountSettings();
|
settings = new AccountSettings();
|
||||||
|
|
||||||
settings.setAccountName(cur.getString(
|
settings.setAccountName(cursor.getString(
|
||||||
cur.getColumnIndex(KeychainContract.ApiAccounts.ACCOUNT_NAME)));
|
cursor.getColumnIndex(KeychainContract.ApiAccounts.ACCOUNT_NAME)));
|
||||||
settings.setKeyId(cur.getLong(
|
settings.setKeyId(cursor.getLong(
|
||||||
cur.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID)));
|
cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID)));
|
||||||
settings.setCompression(cur.getInt(
|
settings.setCompression(cursor.getInt(
|
||||||
cur.getColumnIndexOrThrow(KeychainContract.ApiAccounts.COMPRESSION)));
|
cursor.getColumnIndexOrThrow(KeychainContract.ApiAccounts.COMPRESSION)));
|
||||||
settings.setHashAlgorithm(cur.getInt(
|
settings.setHashAlgorithm(cursor.getInt(
|
||||||
cur.getColumnIndexOrThrow(KeychainContract.ApiAccounts.HASH_ALORITHM)));
|
cursor.getColumnIndexOrThrow(KeychainContract.ApiAccounts.HASH_ALORITHM)));
|
||||||
settings.setEncryptionAlgorithm(cur.getInt(
|
settings.setEncryptionAlgorithm(cursor.getInt(
|
||||||
cur.getColumnIndexOrThrow(KeychainContract.ApiAccounts.ENCRYPTION_ALGORITHM)));
|
cursor.getColumnIndexOrThrow(KeychainContract.ApiAccounts.ENCRYPTION_ALGORITHM)));
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return settings;
|
return settings;
|
||||||
@ -706,12 +724,18 @@ public class ProviderHelper {
|
|||||||
Set<Long> keyIds = new HashSet<Long>();
|
Set<Long> keyIds = new HashSet<Long>();
|
||||||
|
|
||||||
Cursor cursor = mContentResolver.query(uri, null, null, null, null);
|
Cursor cursor = mContentResolver.query(uri, null, null, null, null);
|
||||||
|
try {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
int keyIdColumn = cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID);
|
int keyIdColumn = cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID);
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
keyIds.add(cursor.getLong(keyIdColumn));
|
keyIds.add(cursor.getLong(keyIdColumn));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return keyIds;
|
return keyIds;
|
||||||
}
|
}
|
||||||
@ -722,18 +746,18 @@ public class ProviderHelper {
|
|||||||
String[] projection = new String[]{ApiApps.PACKAGE_SIGNATURE};
|
String[] projection = new String[]{ApiApps.PACKAGE_SIGNATURE};
|
||||||
|
|
||||||
Cursor cursor = mContentResolver.query(queryUri, projection, null, null, null);
|
Cursor cursor = mContentResolver.query(queryUri, projection, null, null, null);
|
||||||
|
try {
|
||||||
byte[] signature = null;
|
byte[] signature = null;
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
int signatureCol = 0;
|
int signatureCol = 0;
|
||||||
|
|
||||||
signature = cursor.getBlob(signatureCol);
|
signature = cursor.getBlob(signatureCol);
|
||||||
}
|
}
|
||||||
|
return signature;
|
||||||
|
} finally {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return signature;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,20 +70,26 @@ public class OpenPgpService extends RemoteService {
|
|||||||
|
|
||||||
for (String email : encryptionUserIds) {
|
for (String email : encryptionUserIds) {
|
||||||
Uri uri = KeyRings.buildUnifiedKeyRingsFindByEmailUri(email);
|
Uri uri = KeyRings.buildUnifiedKeyRingsFindByEmailUri(email);
|
||||||
Cursor cur = getContentResolver().query(uri, null, null, null, null);
|
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
|
||||||
if (cur.moveToFirst()) {
|
try {
|
||||||
long id = cur.getLong(cur.getColumnIndex(KeyRings.MASTER_KEY_ID));
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
long id = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID));
|
||||||
keyIds.add(id);
|
keyIds.add(id);
|
||||||
} else {
|
} else {
|
||||||
missingUserIdsCheck = true;
|
missingUserIdsCheck = true;
|
||||||
missingUserIds.add(email);
|
missingUserIds.add(email);
|
||||||
Log.d(Constants.TAG, "user id missing");
|
Log.d(Constants.TAG, "user id missing");
|
||||||
}
|
}
|
||||||
if (cur.moveToNext()) {
|
if (cursor != null && cursor.moveToNext()) {
|
||||||
duplicateUserIdsCheck = true;
|
duplicateUserIdsCheck = true;
|
||||||
duplicateUserIds.add(email);
|
duplicateUserIds.add(email);
|
||||||
Log.d(Constants.TAG, "more than one user id with the same email");
|
Log.d(Constants.TAG, "more than one user id with the same email");
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert to long[]
|
// convert to long[]
|
||||||
|
@ -690,8 +690,7 @@ public class KeychainIntentService extends IntentService
|
|||||||
new String[]{KeyRings.MASTER_KEY_ID, KeyRings.HAS_ANY_SECRET},
|
new String[]{KeyRings.MASTER_KEY_ID, KeyRings.HAS_ANY_SECRET},
|
||||||
selection, null, null);
|
selection, null, null);
|
||||||
try {
|
try {
|
||||||
cursor.moveToFirst();
|
if (cursor != null && cursor.moveToFirst()) do {
|
||||||
do {
|
|
||||||
// export public either way
|
// export public either way
|
||||||
publicMasterKeyIds.add(cursor.getLong(0));
|
publicMasterKeyIds.add(cursor.getLong(0));
|
||||||
// add secret if available (and requested)
|
// add secret if available (and requested)
|
||||||
@ -699,8 +698,10 @@ public class KeychainIntentService extends IntentService
|
|||||||
secretMasterKeyIds.add(cursor.getLong(0));
|
secretMasterKeyIds.add(cursor.getLong(0));
|
||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
} finally {
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PgpImportExport pgpImportExport = new PgpImportExport(this, this, this);
|
PgpImportExport pgpImportExport = new PgpImportExport(this, this, this);
|
||||||
Bundle resultData = pgpImportExport
|
Bundle resultData = pgpImportExport
|
||||||
|
Loading…
Reference in New Issue
Block a user