More robust checking to prevent fatal IndexOutOfBoundsException

Even with the fix in the parent commit,  the X-K9mail-Identity header can
become invalid if, for example, a user creates a draft in K-9 Mail, then
edits the draft outside of K-9 Mail, then opens the draft again in K-9
Mail.

This commit assures that an invalid X-K9mail-Identity header will not
result in an IndexOutOfBoundsException.
This commit is contained in:
Joe Steele 2013-10-08 19:15:43 -04:00
parent bfb0316583
commit 114be7a15d
1 changed files with 10 additions and 2 deletions

View File

@ -3024,6 +3024,12 @@ public class MessageCompose extends K9Activity implements OnClickListener,
Log.d(K9.LOG_TAG, "Loading message with offset " + bodyOffset + ", length " + bodyLength + ". Text length is " + text.length() + ".");
}
if (bodyOffset + bodyLength > text.length()) {
// The draft was edited outside of K-9 Mail?
Log.d(K9.LOG_TAG, "The identity field from the draft contains an invalid LENGTH/OFFSET");
bodyOffset = 0;
bodyLength = 0;
}
// Grab our reply text.
String bodyText = text.substring(bodyOffset, bodyOffset + bodyLength);
mMessageContentView.setCharacters(HtmlConverter.htmlToText(bodyText));
@ -3085,7 +3091,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
// If we had a body length (and it was valid), separate the composition from the quoted text
// and put them in their respective places in the UI.
if (bodyLength != null && bodyLength + 1 < text.length()) { // + 1 to get rid of the newline we added when saving the draft
try {
String bodyText = text.substring(bodyOffset, bodyOffset + bodyLength);
// Regenerate the quoted text without our user content in it nor added newlines.
@ -3107,7 +3113,9 @@ public class MessageCompose extends K9Activity implements OnClickListener,
}
mQuotedText.setCharacters(quotedText);
} else {
} catch (IndexOutOfBoundsException e) {
// Invalid bodyOffset or bodyLength. The draft was edited outside of K-9 Mail?
Log.d(K9.LOG_TAG, "The identity field from the draft contains an invalid bodyOffset/bodyLength");
if (viewMessageContent) {
mMessageContentView.setCharacters(text);
}