From 7a670e9b7a1d55ed170940da1777b32ff42edad2 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Mon, 16 Feb 2026 09:17:36 +0100 Subject: [PATCH] smtp/mime: avoid quadratic complexity in mime_smtp_find_url_strings Ticket: 8292 When we have buffered something in ctx.decoded_line, we already looked for '\n' in it, so we do not need to run it again Otherwise, callers that supply mime_smtp_find_url_strings with a few bytes at a time without "\n", have a quadratic complexity (cherry picked from commit 8bba47aa09cba87f9cb5d34a897b4bcca9a5ad38) --- rust/src/mime/smtp.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/src/mime/smtp.rs b/rust/src/mime/smtp.rs index 10e98d2ff8..ad0ea46d48 100644 --- a/rust/src/mime/smtp.rs +++ b/rust/src/mime/smtp.rs @@ -332,7 +332,8 @@ fn mime_smtp_find_url_strings(ctx: &mut MimeStateSMTP, input_new: &[u8]) { } let mut input = input_new; - // use previosly buffered beginning of line if any + let new_len = input.len(); + // use previously buffered beginning of line if any if !ctx.decoded_line.is_empty() { ctx.decoded_line.extend_from_slice(input_new); input = &ctx.decoded_line; @@ -348,7 +349,7 @@ fn mime_smtp_find_url_strings(ctx: &mut MimeStateSMTP, input_new: &[u8]) { if !ctx.decoded_line.is_empty() { ctx.decoded_line.clear() } - } else if let Some(x) = input.iter().rev().position(|&x| x == b'\n') { + } else if let Some(x) = input.iter().rev().take(new_len).position(|&x| x == b'\n') { input = &input[..x]; mime_smtp_extract_urls(&mut ctx.urls, input); if !ctx.decoded_line.is_empty() {