diff --git a/src/com/fsck/k9/mail/store/imap/ImapUtility.java b/src/com/fsck/k9/mail/store/imap/ImapUtility.java index d0ac2b31a..b7312eae7 100644 --- a/src/com/fsck/k9/mail/store/imap/ImapUtility.java +++ b/src/com/fsck/k9/mail/store/imap/ImapUtility.java @@ -54,11 +54,8 @@ public class ImapUtility { for (String item : setItems) { if (item.indexOf(':') == -1) { // simple item - try { - Integer.parseInt(item); // Don't need the value; just ensure it's valid + if (isNumberValid(item)) { list.add(item); - } catch (NumberFormatException e) { - Log.d(K9.LOG_TAG, "Invalid UID value", e); } } else { // range @@ -91,23 +88,46 @@ public class ImapUtility { if (range != null) { int colonPos = range.indexOf(':'); if (colonPos > 0) { - int first = Integer.parseInt(range.substring(0, colonPos)); - int second = Integer.parseInt(range.substring(colonPos + 1)); - if (first < second) { - for (int i = first; i <= second; i++) { - list.add(Integer.toString(i)); + long first = Long.parseLong(range.substring(0, colonPos)); + long second = Long.parseLong(range.substring(colonPos + 1)); + if (is32bitValue(first) && is32bitValue(second)) { + if (first < second) { + for (long i = first; i <= second; i++) { + list.add(Long.toString(i)); + } + } else { + for (long i = first; i >= second; i--) { + list.add(Long.toString(i)); + } } } else { - for (int i = first; i >= second; i--) { - list.add(Integer.toString(i)); - } + Log.d(K9.LOG_TAG, "Invalid range: " + range); } } } } catch (NumberFormatException e) { - Log.d(K9.LOG_TAG, "Invalid range value", e); + Log.d(K9.LOG_TAG, "Invalid range value: " + range, e); } return list; } + + private static boolean isNumberValid(String number) { + try { + long value = Long.parseLong(number); + if (is32bitValue(value)) { + return true; + } + } catch (NumberFormatException e) { + // do nothing + } + + Log.d(K9.LOG_TAG, "Invalid UID value: " + number); + + return false; + } + + private static boolean is32bitValue(long value) { + return ((value & ~0xFFFFFFFFL) == 0L); + } } diff --git a/tests/src/com/fsck/k9/mail/store/imap/ImapUtilityTest.java b/tests/src/com/fsck/k9/mail/store/imap/ImapUtilityTest.java index 496d6922e..feaef05e8 100644 --- a/tests/src/com/fsck/k9/mail/store/imap/ImapUtilityTest.java +++ b/tests/src/com/fsck/k9/mail/store/imap/ImapUtilityTest.java @@ -34,6 +34,14 @@ public class ImapUtilityTest extends TestCase { actual = ImapUtility.getImapSequenceValues("1"); MoreAsserts.assertEquals(expected, actual.toArray()); + expected = new String[] {"2147483648"}; // Integer.MAX_VALUE + 1 + actual = ImapUtility.getImapSequenceValues("2147483648"); + MoreAsserts.assertEquals(expected, actual.toArray()); + + expected = new String[] {"4294967295"}; // 2^32 - 1 + actual = ImapUtility.getImapSequenceValues("4294967295"); + MoreAsserts.assertEquals(expected, actual.toArray()); + expected = new String[] {"1", "3", "2"}; actual = ImapUtility.getImapSequenceValues("1,3,2"); MoreAsserts.assertEquals(expected, actual.toArray()); @@ -50,6 +58,11 @@ public class ImapUtilityTest extends TestCase { actual = ImapUtility.getImapSequenceValues("1,2:4,9:7"); MoreAsserts.assertEquals(expected, actual.toArray()); + // Test numbers larger than Integer.MAX_VALUE (2147483647) + expected = new String[] {"2147483646", "2147483647", "2147483648"}; + actual = ImapUtility.getImapSequenceValues("2147483646:2147483648"); + MoreAsserts.assertEquals(expected, actual.toArray()); + // Test partially invalid sets expected = new String[] { "1", "5" }; actual = ImapUtility.getImapSequenceValues("1,x,5"); @@ -75,6 +88,15 @@ public class ImapUtilityTest extends TestCase { expected = new String[0]; actual = ImapUtility.getImapSequenceValues("1:x"); MoreAsserts.assertEquals(expected, actual.toArray()); + + // Test values larger than 2^32 - 1 + expected = new String[0]; + actual = ImapUtility.getImapSequenceValues("4294967296:4294967297"); + MoreAsserts.assertEquals(expected, actual.toArray()); + + expected = new String[0]; + actual = ImapUtility.getImapSequenceValues("4294967296"); // 2^32 + MoreAsserts.assertEquals(expected, actual.toArray()); } /**