From 7518dcdf50149dc864314a6cc7f14488016ba1bb Mon Sep 17 00:00:00 2001 From: TommyDL-Infomaniak Date: Mon, 21 Oct 2024 14:11:00 +0200 Subject: [PATCH 1/2] fix: Truncate messages at 104_448 to prevent OOM error on SplitBody --- .../java/com/infomaniak/mail/utils/MessageBodyUtils.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt b/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt index 62abb46e05..4f91dbcbbb 100644 --- a/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt +++ b/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt @@ -38,6 +38,8 @@ object MessageBodyUtils { private const val QUOTE_DETECTION_TIMEOUT = 1_500L + private const val MESSAGE_LENGTH_LIMIT = 104_448 + private val quoteDescriptors = arrayOf( "blockquote[type=cite]", // macOS and iOS mail client // The reply and forward #divRplyFwdMsg div only contains the header, the previous message body is written right next to @@ -70,6 +72,12 @@ object MessageBodyUtils { block = { // Do not nest jsoupParseWithLog and measureAndLogMemoryUsage so logs are independent from one another val htmlDocument = jsoupParseWithLog(bodyContent) + + if (htmlDocument.text().length > MESSAGE_LENGTH_LIMIT) { + val content = bodyContent.substring(0, MESSAGE_LENGTH_LIMIT) + return@executeWithTimeoutOrDefault SplitBody(content) + } + val (content, quotes) = measureAndLogMemoryUsage( tag = "Split signature and quote memory usage", actionName = "splitting", From b2bf4d41e658f08b69b98ad3b431f34a36d6fdf4 Mon Sep 17 00:00:00 2001 From: TommyDL-Infomaniak Date: Tue, 22 Oct 2024 08:58:42 +0200 Subject: [PATCH 2/2] Apply suggestion --- app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt b/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt index 4f91dbcbbb..7af4d952d4 100644 --- a/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt +++ b/app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt @@ -38,6 +38,8 @@ object MessageBodyUtils { private const val QUOTE_DETECTION_TIMEOUT = 1_500L + // Arbitrary maximum length that a Message's body can reach without provoking + // a crash when opened in a Thread with other equally long Messages. private const val MESSAGE_LENGTH_LIMIT = 104_448 private val quoteDescriptors = arrayOf(