1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Avoid StringIndexOutOfBoundsException when the header is broken like "=?us-ascii?q?abc?= =?"

This commit is contained in:
Koji Arai 2012-11-20 00:05:59 +09:00
parent 1c7d7af975
commit 0be299fbd0
2 changed files with 17 additions and 15 deletions

View File

@ -110,24 +110,28 @@ public class DecoderUtil {
while (true) { while (true) {
int begin = body.indexOf("=?", previousEnd); 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 // 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 // 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 "?=". // to find the two '?' in the "header", before looking for the final "?=".
int endScan = begin + 2; int qm1 = body.indexOf('?', begin + 2);
if (begin != -1) { if (qm1 == -1) {
int qm1 = body.indexOf('?', endScan + 2); sb.append(body.substring(previousEnd));
return sb.toString();
}
int qm2 = body.indexOf('?', qm1 + 1); int qm2 = body.indexOf('?', qm1 + 1);
if (qm2 != -1) { if (qm2 == -1) {
endScan = qm2 + 1; sb.append(body.substring(previousEnd));
} return sb.toString();
} }
int end = begin == -1 ? -1 : body.indexOf("?=", endScan); int end = body.indexOf("?=", qm2 + 1);
if (end == -1) { if (end == -1) {
if (previousEnd == 0)
return body;
sb.append(body.substring(previousEnd)); sb.append(body.substring(previousEnd));
return sb.toString(); return sb.toString();
} }

View File

@ -57,8 +57,7 @@ public class DecoderUtilTest extends TestCase {
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
body = "=??q?a?="; body = "=??q?a?=";
expect = "=??q?a?="; expect = "a";
//expect = "a";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
@ -88,8 +87,7 @@ public class DecoderUtilTest extends TestCase {
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
body = "=?x?q?X?="; body = "=?x?q?X?=";
expect = "=?x?q?X?="; expect = "X";
//expect = "X";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));