diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-06-18 02:13:58 +0200 |
---|---|---|
committer | Ned Deily <nad@python.org> | 2019-06-17 20:13:57 -0400 |
commit | 516a6a254814d2bc6a90290dfc44d77fdfb4050b (patch) | |
tree | c8011e1b47da209cc7dd7424de1b23234f28ec0b /Lib/email | |
parent | ecafe8e42464b2c91a507fd26de06ce1203dd654 (diff) | |
download | cpython-git-516a6a254814d2bc6a90290dfc44d77fdfb4050b.tar.gz |
bpo-33529, email: Fix infinite loop in email header encoding (GH-12020) (GH-14162)
(cherry picked from commit c1f5667be1e3ec5871560c677402c1252c6018a6)
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/_header_value_parser.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index 1fb8cb448a..f42cde203c 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -2725,16 +2725,19 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset): lines.append(' ') # XXX We'll get an infinite loop here if maxlen is <= 7 continue - first_part = to_encode[:text_space] - ew = _ew.encode(first_part, charset=encode_as) - excess = len(ew) - remaining_space - if excess > 0: - # encode always chooses the shortest encoding, so this - # is guaranteed to fit at this point. - first_part = first_part[:-excess] - ew = _ew.encode(first_part) - lines[-1] += ew - to_encode = to_encode[len(first_part):] + + to_encode_word = to_encode[:text_space] + encoded_word = _ew.encode(to_encode_word, charset=encode_as) + excess = len(encoded_word) - remaining_space + while excess > 0: + # Since the chunk to encode is guaranteed to fit into less than 100 characters, + # shrinking it by one at a time shouldn't take long. + to_encode_word = to_encode_word[:-1] + encoded_word = _ew.encode(to_encode_word, charset=encode_as) + excess = len(encoded_word) - remaining_space + lines[-1] += encoded_word + to_encode = to_encode[len(to_encode_word):] + if to_encode: lines.append(' ') new_last_ew = len(lines[-1]) |