Bug report
Bug description:
imaplib.Time2Internaldate is documented to raise ValueError for input that is not of a known type, and its own final branch is raise ValueError("date_time not of a known type"). However, the string branch subscripts the value before that check:
elif isinstance(date_time, str) and (date_time[0],date_time[-1]) == ('"','"'):
so an empty string escapes as a bare IndexError instead:
>>> import imaplib
>>> imaplib.Time2Internaldate('')
Traceback (most recent call last):
...
IndexError: string index out of range
>>> imaplib.Time2Internaldate('x') # non-empty unquoted string is fine
Traceback (most recent call last):
...
ValueError: date_time not of a known type
The fix is to use slices so the comparison is simply false for an empty string and control falls through to the intended ValueError:
elif isinstance(date_time, str) and (date_time[:1], date_time[-1:]) == ('"', '"'):
Behavior for all other inputs is unchanged. Related: gh-86165 recently fixed a different crash in this same function. I have a PR ready with the fix, a regression test, and a NEWS entry.
CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
No response
Linked PRs
Bug report
Bug description:
imaplib.Time2Internaldateis documented to raiseValueErrorfor input that is not of a known type, and its own final branch israise ValueError("date_time not of a known type"). However, the string branch subscripts the value before that check:so an empty string escapes as a bare
IndexErrorinstead:The fix is to use slices so the comparison is simply false for an empty string and control falls through to the intended
ValueError:Behavior for all other inputs is unchanged. Related: gh-86165 recently fixed a different crash in this same function. I have a PR ready with the fix, a regression test, and a NEWS entry.
CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
No response
Linked PRs