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 8bba47aa09)
pull/15050/head
Philippe Antoine 2 months ago committed by Jason Ish
parent 44e75573ac
commit 7a670e9b7a

@ -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() {

Loading…
Cancel
Save