python / python/cpython

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

Open
#153,854 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-email type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-153874
  • gh-153929

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the imaplib.Time2Internaldate entry point and review the reported empty-string path. Check the regression test and NEWS entry mentioned in the issue; done means empty input raises ValueError while other inputs retain their behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.