8000 gh-134152: Fix UnboundLocalError in email._header_value_parser _get_p… · faster-cpython/cpython@a32ea45 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit a32ea45

Browse files
authored
pythongh-134152: Fix UnboundLocalError in email._header_value_parser _get_ptext_to_endchars (python#134233)
Fix an UnboundLocalError that can occur when parsing certain delimited constructs in headers (domain literals, quoted strings, comments). After the fix the _get_ptext_to_endchars returns an empty string if there is no content after the opening delimiter. The calling code is responsible for handling the lack of the trailing delimiter, which it already does; this edge case was the header ending immediately after the opening delimiter.
1 parent 7b1a700 commit a32ea45

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Lib/email/_header_value_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,8 @@ def _get_ptext_to_endchars(value, endchars):
10201020
a flag that is True iff there were any quoted printables decoded.
10211021
10221022
"""
1023+
if not value:
1024+
return '', '', False
10231025
fragment, *remainder = _wsp_splitter(value, 1)
10241026
vchars = []
10251027
escape = False

Lib/test/test_email/test__header_value_parser.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,19 @@ def test_get_qp_ctext_non_printables(self):
463463
[errors.NonPrintableDefect], ')')
464464
self.assertEqual(ptext.defects[0].non_printables[0], '\x00')
465465

466+
def test_get_qp_ctext_close_paren_only(self):
467+
self._test_get_x(parser.get_qp_ctext,
468+
')', '', ' ', [], ')')
469+
470+
def test_get_qp_ctext_open_paren_only(self):
471+
self._test_get_x(parser.get_qp_ctext,
472+
'(', '', ' ', [], '(')
473+
474+
def test_get_qp_ctext_no_end_char(self):
475+
self._test_get_x(parser.get_qp_ctext,
476+
'', '', ' ', [], '')
477+
478+
466479
# get_qcontent
467480

468481
def test_get_qcontent_only(self):
@@ -503,6 +516,14 @@ def test_get_qcontent_non_printables(self):
503516
[errors.NonPrintableDefect], '"')
504517
self.assertEqual(ptext.defects[0].non_printables[0], '\x00')
505518

519+
def test_get_qcontent_empty(self):
520+
self._test_get_x(parser.get_qcontent,
521+
'"', '', '', [], '"')
522+
523+
def test_get_qcontent_no_end_char(self):
524+
self._test_get_x(parser.get_qcontent,
525+
'', '', '', [], '')
526+
506527
# get_atext
507528

508529
def test_get_atext_only(self):
@@ -1283,6 +1304,18 @@ def test_get_dtext_open_bracket_mid_word(self):
12831304
self._test_get_x(parser.get_dtext,
12841305
'foo[bar', 'foo', 'foo', [], '[bar')
12851306

1307+
def test_get_dtext_open_bracket_only(self):
1308+
self._test_get_x(parser.get_dtext,
1309+
'[', '', '', [], '[')
1310+
1311+
def test_get_dtext_close_bracket_only(self):
1312+
self._test_get_x(parser.get_dtext,
1313+
']', '', '', [], ']')
1314+
1315+
def test_get_dtext_empty(self):
1316+
self._test_get_x(parser.get_dtext,
1317+
'', '', '', [], '')
1318+
12861319
# get_domain_literal
12871320

12881321
def test_get_domain_literal_only(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed :exc:`UnboundLocalError` that could occur during :mod:`email` header
2+
parsing if an expected trailing delimiter is missing in some contexts.

0 commit comments

Comments
 (0)
0