Skip to content

imaplib.Time2Internaldate raises IndexError instead of ValueError on an empty string #153854

Description

@IbrahimShaqqou

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions