From 2438a47a79d2c266ccc75865e483daa62ad4a2ca Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Fri, 4 Jan 2019 10:41:47 -0500 Subject: [PATCH] Bug 1517606 - Infinite loop in Bugzilla::Extension::BMO::_inject_headers_into_body() --- extensions/BMO/Extension.pm | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/extensions/BMO/Extension.pm b/extensions/BMO/Extension.pm index 03857b2b7..d19d9e4db 100644 --- a/extensions/BMO/Extension.pm +++ b/extensions/BMO/Extension.pm @@ -1790,20 +1790,18 @@ sub _inject_headers_into_body { # these aren't real fields, but exist in the headers push @fields, ('Comment Created', 'Attachment Created'); - @fields = sort { length($b) <=> length($a) } @fields; - while ($value ne '') { - foreach my $field (@fields) { - if ($value eq $field) { - push @headers, "$name: $field"; - $value = ''; - last; - } - if (substr($value, 0, length($field) + 1) eq $field . ' ') { - push @headers, "$name: $field"; - $value = substr($value, length($field) + 1); - last; - } - } + + # The fields should be in longest to shortest order + my @sorted_fields = sort { length($b) <=> length($a) } @fields; + + # A field matches if it ends with a space or the end-of-string. + my @regexp_fields = map { '(' . quotemeta($_) . ')(?:\s|$)' } @sorted_fields; + my $fields_regexp = join('|', @regexp_fields); + + # //g matches all patterns and the grep ensures we only get matches. + my @extracted_fields = grep { defined($_) } $value =~ /$fields_regexp/g; + foreach my $field (@extracted_fields) { + push @headers, "$name: $field"; } } else { @@ -1845,6 +1843,7 @@ sub _inject_headers_into_body { } elsif (!$email->content_type || $email->content_type =~ /^text\/(?:html|plain)/) { + # text-only email _replace_placeholder_in_part($email, $replacement); } -- 2.47.3