Skip to content

gh-145831: email.quoprimime: decode() leaves stray \r when eol='\r\n'#145832

Open
stefanzetzsche wants to merge 5 commits intopython:mainfrom
stefanzetzsche:fix/email_quoprimime_crlf_stray_cr
Open

gh-145831: email.quoprimime: decode() leaves stray \r when eol='\r\n'#145832
stefanzetzsche wants to merge 5 commits intopython:mainfrom
stefanzetzsche:fix/email_quoprimime_crlf_stray_cr

Conversation

@stefanzetzsche
Copy link
Contributor

@stefanzetzsche stefanzetzsche commented Mar 11, 2026

Issue

email.quoprimime.decode() does decoded[:-1] to strip a trailing line ending, but this only removes one character. When eol='\r\n' (2 characters), a stray \r is left in the output.

Reproducer

import email.quoprimime as qp
print(repr(qp.decode('abc', eol='\r\n')))  # 'abc\r' — expected 'abc'

Fix

In Lib/email/quoprimime.py, change:

decoded = decoded[:-1]

to:

decoded = decoded[:-len(eol)]

Tests

Three test cases added to TestQuopri in Lib/test/test_email/test_email.py:

  • test_decode_crlf_eol_no_trailing_newline — verifies decode('abc', eol='\r\n') returns 'abc' (direct reproducer for the bug; without the fix, returns 'abc\r').
  • test_decode_crlf_eol_multiline_no_trailing_newline — verifies decode('a\r\nb', eol='\r\n') returns 'a\r\nb' (multi-line input without trailing newline).
  • test_decode_crlf_eol_with_trailing_newline — verifies decode('abc\r\n', eol='\r\n') returns 'abc\r\n' (trailing newline is legitimate and should be preserved).

stefanzetzsche and others added 3 commits February 22, 2026 14:54
decoded[:-1] only strips one character, leaving a stray \r when eol
is two characters. Fix: decoded[:-len(eol)].
@stefanzetzsche stefanzetzsche marked this pull request as ready for review March 11, 2026 15:10
@stefanzetzsche stefanzetzsche requested a review from a team as a code owner March 11, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant