Skip to content

Commit 1a0edb1

Browse files
gh-145831: email.quoprimime: decode() leaves stray \r when eol='\r\n' (#145832)
decoded[:-1] only strips one character, leaving a stray \r when eol is two characters. Fix: decoded[:-len(eol)].
1 parent 0f49232 commit 1a0edb1

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Lib/email/quoprimime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def decode(encoded, eol=NL):
272272
decoded += eol
273273
# Special case if original string did not end with eol
274274
if encoded[-1] not in '\r\n' and decoded.endswith(eol):
275-
decoded = decoded[:-1]
275+
decoded = decoded[:-len(eol)]
276276
return decoded
277277

278278

Lib/test/test_email/test_email.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4828,6 +4828,15 @@ def test_decode_soft_line_break(self):
48284828
def test_decode_false_quoting(self):
48294829
self._test_decode('A=1,B=A ==> A+B==2', 'A=1,B=A ==> A+B==2')
48304830

4831+
def test_decode_crlf_eol_no_trailing_newline(self):
4832+
self._test_decode('abc', 'abc', eol='\r\n')
4833+
4834+
def test_decode_crlf_eol_multiline_no_trailing_newline(self):
4835+
self._test_decode('a\r\nb', 'a\r\nb', eol='\r\n')
4836+
4837+
def test_decode_crlf_eol_with_trailing_newline(self):
4838+
self._test_decode('abc\r\n', 'abc\r\n', eol='\r\n')
4839+
48314840
def _test_encode(self, body, expected_encoded_body, maxlinelen=None, eol=None):
48324841
kwargs = {}
48334842
if maxlinelen is None:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`!email.quoprimime.decode` leaving a stray ``\r`` when
2+
``eol='\r\n'`` by stripping the full *eol* string instead of one character.

0 commit comments

Comments
 (0)