[New-bugs-announce] [issue45239] email.utils.parsedate_tz raises UnboundLocalError if time has more than 2 dots in it

Ben Hoyt report at bugs.python.org
Sat Sep 18 18:35:53 EDT 2021


New submission from Ben Hoyt <benhoyt at gmail.com>:

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).

----------
components: Library (Lib)
messages: 402140
nosy: barry, benhoyt
priority: normal
severity: normal
status: open
title: email.utils.parsedate_tz raises UnboundLocalError if time has more than 2 dots in it
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45239>
_______________________________________


More information about the New-bugs-announce mailing list