From 0be299fbd09713f4622ab5df476ea512693a7a87 Mon Sep 17 00:00:00 2001 From: Koji Arai Date: Tue, 20 Nov 2012 00:05:59 +0900 Subject: [PATCH] =?UTF-8?q?Avoid=20StringIndexOutOfBoundsException=20when?= =?UTF-8?q?=20the=20header=20is=20broken=20like=20"=3D=3Fus-ascii=3Fq=3Fab?= =?UTF-8?q?c=3F=3D=20=3D=3F"?= --- .../fsck/k9/mail/internet/DecoderUtil.java | 26 +++++++++++-------- .../k9/mail/internet/DecoderUtilTest.java | 6 ++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/com/fsck/k9/mail/internet/DecoderUtil.java b/src/com/fsck/k9/mail/internet/DecoderUtil.java index 137ba5e7e5..28abeed849 100644 --- a/src/com/fsck/k9/mail/internet/DecoderUtil.java +++ b/src/com/fsck/k9/mail/internet/DecoderUtil.java @@ -110,24 +110,28 @@ public class DecoderUtil { while (true) { int begin = body.indexOf("=?", previousEnd); + if (begin == -1) { + sb.append(body.substring(previousEnd)); + return sb.toString(); + } // ANDROID: The mime4j original version has an error here. It gets confused if // the encoded string begins with an '=' (just after "?Q?"). This patch seeks forward // to find the two '?' in the "header", before looking for the final "?=". - int endScan = begin + 2; - if (begin != -1) { - int qm1 = body.indexOf('?', endScan + 2); - int qm2 = body.indexOf('?', qm1 + 1); - if (qm2 != -1) { - endScan = qm2 + 1; - } + int qm1 = body.indexOf('?', begin + 2); + if (qm1 == -1) { + sb.append(body.substring(previousEnd)); + return sb.toString(); } - int end = begin == -1 ? -1 : body.indexOf("?=", endScan); - if (end == -1) { - if (previousEnd == 0) - return body; + int qm2 = body.indexOf('?', qm1 + 1); + if (qm2 == -1) { + sb.append(body.substring(previousEnd)); + return sb.toString(); + } + int end = body.indexOf("?=", qm2 + 1); + if (end == -1) { sb.append(body.substring(previousEnd)); return sb.toString(); } diff --git a/tests/src/com/fsck/k9/mail/internet/DecoderUtilTest.java b/tests/src/com/fsck/k9/mail/internet/DecoderUtilTest.java index e9c7d62c65..b1d25c873e 100644 --- a/tests/src/com/fsck/k9/mail/internet/DecoderUtilTest.java +++ b/tests/src/com/fsck/k9/mail/internet/DecoderUtilTest.java @@ -57,8 +57,7 @@ public class DecoderUtilTest extends TestCase { assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=??q?a?="; - expect = "=??q?a?="; - //expect = "a"; + expect = "a"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); @@ -88,8 +87,7 @@ public class DecoderUtilTest extends TestCase { assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); body = "=?x?q?X?="; - expect = "=?x?q?X?="; - //expect = "X"; + expect = "X"; message = null; assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));