From 2d45e537394daef4a68a0c94a0287887619ba701 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sat, 13 Jun 2015 15:29:58 +0100 Subject: [PATCH 1/2] fix potential NPE --- .../java/com/fsck/k9/activity/Accounts.java | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/Accounts.java b/k9mail/src/main/java/com/fsck/k9/activity/Accounts.java index ff5028a83..840689146 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/Accounts.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/Accounts.java @@ -1183,41 +1183,40 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { if (menuInfo != null) { mSelectedContextAccount = (BaseAccount)getListView().getItemAtPosition(menuInfo.position); } - Account realAccount = null; if (mSelectedContextAccount instanceof Account) { - realAccount = (Account)mSelectedContextAccount; - } - switch (item.getItemId()) { - case R.id.delete_account: - onDeleteAccount(realAccount); - break; - case R.id.account_settings: - onEditAccount(realAccount); - break; - case R.id.activate: - onActivateAccount(realAccount); - break; - case R.id.clear_pending: - onClearCommands(realAccount); - break; - case R.id.empty_trash: - onEmptyTrash(realAccount); - break; - case R.id.clear: - onClear(realAccount); - break; - case R.id.recreate: - onRecreate(realAccount); - break; - case R.id.export: - onExport(false, realAccount); - break; - case R.id.move_up: - onMove(realAccount, true); - break; - case R.id.move_down: - onMove(realAccount, false); - break; + Account realAccount = (Account)mSelectedContextAccount; + switch (item.getItemId()) { + case R.id.delete_account: + onDeleteAccount(realAccount); + break; + case R.id.account_settings: + onEditAccount(realAccount); + break; + case R.id.activate: + onActivateAccount(realAccount); + break; + case R.id.clear_pending: + onClearCommands(realAccount); + break; + case R.id.empty_trash: + onEmptyTrash(realAccount); + break; + case R.id.clear: + onClear(realAccount); + break; + case R.id.recreate: + onRecreate(realAccount); + break; + case R.id.export: + onExport(false, realAccount); + break; + case R.id.move_up: + onMove(realAccount, true); + break; + case R.id.move_down: + onMove(realAccount, false); + break; + } } return true; } From 591785a3aba6c22b500cca2a26267868aabe0e86 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sat, 13 Jun 2015 16:09:24 +0100 Subject: [PATCH 2/2] fix up some dodgy nulls --- .../k9/mail/internet/BinaryTempFileBody.java | 16 ++++++++------- .../internet/BinaryTempFileMessageBody.java | 2 +- k9mail/src/main/java/com/fsck/k9/Account.java | 2 +- .../fsck/k9/fragment/MessageListFragment.java | 20 ++++++------------- .../com/fsck/k9/helper/HtmlConverterTest.java | 13 +++++++++--- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileBody.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileBody.java index 157b4046c..c14df1c5a 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileBody.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileBody.java @@ -46,24 +46,26 @@ public class BinaryTempFileBody implements RawDataBody, SizeAware { try { File newFile = File.createTempFile("body", null, mTempDirectory); - OutputStream out = new FileOutputStream(newFile); + final OutputStream out = new FileOutputStream(newFile); try { + OutputStream wrappedOut = null; if (MimeUtil.ENC_QUOTED_PRINTABLE.equals(encoding)) { - out = new QuotedPrintableOutputStream(out, false); + wrappedOut = new QuotedPrintableOutputStream(out, false); } else if (MimeUtil.ENC_BASE64.equals(encoding)) { - out = new Base64OutputStream(out); + wrappedOut = new Base64OutputStream(out); } else { throw new RuntimeException("Target encoding not supported: " + encoding); } InputStream in = getInputStream(); try { - IOUtils.copy(in, out); + IOUtils.copy(in, wrappedOut); } finally { - in.close(); + IOUtils.closeQuietly(in); + IOUtils.closeQuietly(wrappedOut); } } finally { - out.close(); + IOUtils.closeQuietly(out); } mFile = newFile; @@ -100,7 +102,7 @@ public class BinaryTempFileBody implements RawDataBody, SizeAware { try { IOUtils.copy(in, out); } finally { - in.close(); + IOUtils.closeQuietly(in); } } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java index a47f2dffd..d4eb0435d 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java @@ -46,7 +46,7 @@ public class BinaryTempFileMessageBody extends BinaryTempFileBody implements Com IOUtils.copy(in, out); } } finally { - in.close(); + IOUtils.closeQuietly(in); } } diff --git a/k9mail/src/main/java/com/fsck/k9/Account.java b/k9mail/src/main/java/com/fsck/k9/Account.java index 13c47c9ed..82029468b 100644 --- a/k9mail/src/main/java/com/fsck/k9/Account.java +++ b/k9mail/src/main/java/com/fsck/k9/Account.java @@ -1404,7 +1404,7 @@ public class Account implements BaseAccount, StoreConfig { if (i < identities.size()) { return identities.get(i); } - return null; + throw new IllegalArgumentException("Identity with index " + i + " not found"); } public boolean isAnIdentity(Address[] addrs) { diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java index 4a5bf8f07..78c14f8db 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java @@ -1024,15 +1024,10 @@ public class MessageListFragment extends Fragment implements OnItemClickListener } private String getFolderNameById(Account account, long folderId) { - try { - Folder folder = getFolderById(account, folderId); - if (folder != null) { - return folder.getName(); - } - } catch (Exception e) { - Log.e(K9.LOG_TAG, "getFolderNameById() failed.", e); + Folder folder = getFolderById(account, folderId); + if (folder != null) { + return folder.getName(); } - return null; } @@ -1042,9 +1037,8 @@ public class MessageListFragment extends Fragment implements OnItemClickListener LocalFolder localFolder = localStore.getFolderById(folderId); localFolder.open(Folder.OPEN_MODE_RO); return localFolder; - } catch (Exception e) { - Log.e(K9.LOG_TAG, "getFolderNameById() failed.", e); - return null; + } catch (MessagingException e) { + throw new RuntimeException(e); } } @@ -3162,10 +3156,8 @@ public class MessageListFragment extends Fragment implements OnItemClickListener try { return folder.getMessage(uid); } catch (MessagingException e) { - Log.e(K9.LOG_TAG, "Something went wrong while fetching a message", e); + throw new RuntimeException(e); } - - return null; } private List getCheckedMessages() { diff --git a/k9mail/src/test/java/com/fsck/k9/helper/HtmlConverterTest.java b/k9mail/src/test/java/com/fsck/k9/helper/HtmlConverterTest.java index 9292991b5..3ea67e03d 100644 --- a/k9mail/src/test/java/com/fsck/k9/helper/HtmlConverterTest.java +++ b/k9mail/src/test/java/com/fsck/k9/helper/HtmlConverterTest.java @@ -4,7 +4,9 @@ package com.fsck.k9.helper; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; +import java.io.IOException; +import org.apache.commons.io.IOUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @@ -136,18 +138,23 @@ public class HtmlConverterTest { if (!WRITE_TO_FILE) { return; } + + FileWriter fstream = null; + try { System.err.println(content); File f = new File(OUTPUT_FILE); f.delete(); - FileWriter fstream = new FileWriter(OUTPUT_FILE); + fstream = new FileWriter(OUTPUT_FILE); BufferedWriter out = new BufferedWriter(fstream); out.write(content); out.close(); - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(fstream); } }