diff --git a/src/com/fsck/k9/mail/store/ImapResponseParser.java b/src/com/fsck/k9/mail/store/ImapResponseParser.java index 6341b9520..c73324753 100644 --- a/src/com/fsck/k9/mail/store/ImapResponseParser.java +++ b/src/com/fsck/k9/mail/store/ImapResponseParser.java @@ -409,7 +409,7 @@ public class ImapResponseParser { public Object getKeyedValue(Object key) { - for (int i = 0, count = size(); i < count; i++) { + for (int i = 0, count = size() - 1; i < count; i++) { if (equalsIgnoreCase(get(i), key)) { return get(i + 1); } @@ -434,7 +434,7 @@ public class ImapResponseParser { return false; } - for (int i = 0, count = size(); i < count; i++) { + for (int i = 0, count = size() - 1; i < count; i++) { if (equalsIgnoreCase(key, get(i))) { return true; } @@ -443,7 +443,7 @@ public class ImapResponseParser { } public int getKeyIndex(Object key) { - for (int i = 0, count = size(); i < count; i++) { + for (int i = 0, count = size() - 1; i < count; i++) { if (equalsIgnoreCase(key, get(i))) { return i; } diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index 043920aa6..5661e4af9 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -1655,13 +1655,16 @@ public class ImapStore extends Store { if (fetchList.containsKey("BODY")) { int index = fetchList.getKeyIndex("BODY") + 2; - result = fetchList.getObject(index); + int size = fetchList.size(); + if (index < size) { + result = fetchList.getObject(index); - // Check if there's an origin octet - if (result instanceof String) { - String originOctet = (String)result; - if (originOctet.startsWith("<")) { - result = fetchList.getObject(index + 1); + // Check if there's an origin octet + if (result instanceof String) { + String originOctet = (String) result; + if (originOctet.startsWith("<") && (index + 1) < size) { + result = fetchList.getObject(index + 1); + } } } } diff --git a/tests/src/com/fsck/k9/mail/store/ImapResponseParserTest.java b/tests/src/com/fsck/k9/mail/store/ImapResponseParserTest.java index 7353e23e7..f521b80b0 100644 --- a/tests/src/com/fsck/k9/mail/store/ImapResponseParserTest.java +++ b/tests/src/com/fsck/k9/mail/store/ImapResponseParserTest.java @@ -59,6 +59,36 @@ public class ImapResponseParserTest extends TestCase { assertEquals("token2", respTextCode.get(1)); } + public void testImapListMethods() throws IOException { + ImapList list = new ImapList(); + list.add("ONE"); + list.add("TWO"); + list.add("THREE"); + + assertTrue(list.containsKey("ONE")); + assertTrue(list.containsKey("TWO")); + assertFalse(list.containsKey("THREE")); + assertFalse(list.containsKey("nonexistent")); + + assertEquals("TWO", list.getKeyedValue("ONE")); + assertEquals("THREE", list.getKeyedValue("TWO")); + assertNull(list.getKeyedValue("THREE")); + assertNull(list.getKeyedValue("nonexistent")); + + assertEquals(0, list.getKeyIndex("ONE")); + assertEquals(1, list.getKeyIndex("TWO")); + + try { + list.getKeyIndex("THREE"); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException e) { /* do nothing */ } + + try { + list.getKeyIndex("nonexistent"); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException e) { /* do nothing */ } + } + private ImapResponseParser createParser(String response) { ByteArrayInputStream in = new ByteArrayInputStream(response.getBytes()); PeekableInputStream pin = new PeekableInputStream(in);