]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38698: Prevent UnboundLocalError to pop up in parse_message_id (GH-17277)
authorClaudiu Popa <pcmanticore@gmail.com>
Thu, 5 Dec 2019 03:14:26 +0000 (04:14 +0100)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 5 Dec 2019 03:14:26 +0000 (19:14 -0800)
parse_message_id() was improperly using a token defined inside an exception
handler, which was raising `UnboundLocalError` on parsing an invalid value.

https://bugs.python.org/issue38698

Lib/email/_header_value_parser.py
Lib/test/test_email/test__header_value_parser.py
Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst [new file with mode: 0644]

index 1668b4a14e9b91c0359f854f171bf3294bb00f8c..abdef8189ca6fb078e8815bf6607eaa308a669a9 100644 (file)
@@ -2113,7 +2113,8 @@ def parse_message_id(value):
     except errors.HeaderParseError:
         message_id.defects.append(errors.InvalidHeaderDefect(
             "Expected msg-id but found {!r}".format(value)))
-    message_id.append(token)
+    else:
+        message_id.append(token)
     return message_id
 
 #
index 46d90b38e91e1ce25e7e9a8d23b8579c625252d2..71168f3183d23c706afff1ad5663d5a76a054306 100644 (file)
@@ -2638,6 +2638,12 @@ class TestParser(TestParserMixin, TestEmailBase):
         )
         self.assertEqual(msg_id.token_type, 'msg-id')
 
+    def test_get_msg_id_invalid_expected_msg_id_not_found(self):
+        text = "Message-Id: 935-XPB-567:0:86089:180874:0:45327:9:90305:17843586-40@example.com"
+        msg_id = parser.parse_message_id(text)
+        self.assertDefectsEqual(msg_id.all_defects,
+                                [errors.InvalidHeaderDefect])
+
     def test_get_msg_id_no_angle_start(self):
         with self.assertRaises(errors.HeaderParseError):
             parser.get_msg_id("msgwithnoankle")
diff --git a/Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst b/Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst
new file mode 100644 (file)
index 0000000..e606acb
--- /dev/null
@@ -0,0 +1,5 @@
+Prevent UnboundLocalError to pop up in parse_message_id
+
+parse_message_id() was improperly using a token defined inside an exception
+handler, which was raising `UnboundLocalError` on parsing an invalid value.
+Patch by Claudiu Popa.