This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.utils.parsedate_tz raises UnboundLocalError if time has more than 2 dots in it
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, benhoyt, lukasz.langa, miss-islington
Priority: normal Keywords: patch

Created on 2021-09-18 22:35 by benhoyt, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28452 merged benhoyt, 2021-09-18 23:36
PR 28928 merged miss-islington, 2021-10-13 16:21
PR 28930 merged lukasz.langa, 2021-10-13 16:27
Messages (6)
msg402140 - (view) Author: Ben Hoyt (benhoyt) * Date: 2021-09-18 22:35
In going through some standard library code, I found that the email.utils.parsedate_tz() function (https://docs.python.org/3/library/email.utils.html#email.utils.parsedate_tz) has a bug if the time value is in dotted format and has more than 2 dots in it, for example: "12.34.56.78". Per the docs, it should return None in the case of invalid input.

This is happening because in the case that handles the '.' separator (instead of the normal ':'), there's no else clause to return None when there's not 2 or 3 segments.

From https://github.com/python/cpython/blob/dea59cf88adf5d20812edda330e085a4695baba4/Lib/email/_parseaddr.py#L118-L132:

    if len(tm) == 2:
        [thh, tmm] = tm
        tss = '0'
    elif len(tm) == 3:
        [thh, tmm, tss] = tm
    elif len(tm) == 1 and '.' in tm[0]:
        # Some non-compliant MUAs use '.' to separate time elements.
        tm = tm[0].split('.')
        if len(tm) == 2:
            [thh, tmm] = tm
            tss = 0
        elif len(tm) == 3:
            [thh, tmm, tss] = tm
        # HERE: need "else: return None"
    else:
        return None

We simply need to include that additional "else: return None" block in the '.' handling case.

I'll submit a pull request that fixes this soon (and adds a test for this case).
msg402141 - (view) Author: Ben Hoyt (benhoyt) * Date: 2021-09-18 22:44
For reference, here's a repro case:

$ python3.10 -c 'import email.utils; \
    email.utils.parsedate_tz("Wed, 3 Apr 2002 12.34.56.78+0800")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.10/email/_parseaddr.py", line 50, in parsedate_tz
    res = _parsedate_tz(data)
  File "/usr/local/lib/python3.10/email/_parseaddr.py", line 134, in _parsedate_tz
    thh = int(thh)
UnboundLocalError: local variable 'thh' referenced before assignment
msg403847 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 16:21
New changeset b9e687618d3489944f29adbd2be50b46940c9e70 by Ben Hoyt in branch 'main':
bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452)
https://github.com/python/cpython/commit/b9e687618d3489944f29adbd2be50b46940c9e70
msg403852 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 16:58
New changeset 5638618845752f713c00d69dbe705fed16761948 by Miss Islington (bot) in branch '3.9':
bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452) (GH-28928)
https://github.com/python/cpython/commit/5638618845752f713c00d69dbe705fed16761948
msg403856 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 17:12
New changeset f8473f6f7603f8cccccc3307d4cb853587be41b3 by Łukasz Langa in branch '3.10':
[3.10] bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452) (GH-28930)
https://github.com/python/cpython/commit/f8473f6f7603f8cccccc3307d4cb853587be41b3
msg403857 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 17:12
Thanks, Ben! ✨ 🍰 ✨
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89402
2021-10-13 17:12:48lukasz.langasetstatus: open -> closed
versions: + Python 3.11
type: behavior
messages: + msg403857

resolution: fixed
stage: patch review -> resolved
2021-10-13 17:12:26lukasz.langasetmessages: + msg403856
2021-10-13 16:58:41lukasz.langasetmessages: + msg403852
2021-10-13 16:27:22lukasz.langasetpull_requests: + pull_request27219
2021-10-13 16:21:38miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27217
2021-10-13 16:21:31lukasz.langasetnosy: + lukasz.langa
messages: + msg403847
2021-09-18 23:36:10benhoytsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26853
2021-09-18 22:44:33benhoytsetmessages: + msg402141
2021-09-18 22:35:53benhoytcreate