python / python/cpython

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

Đang mở
#153,854 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib topic-email type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu tại điểm vào imaplib.Time2Internaldate và xem xét nhánh chuỗi rỗng được báo cáo trong issue. Kiểm tra bài kiểm thử hồi quy và mục NEWS được đề cập trong issue; được xem là hoàn tất khi đầu vào rỗng gây ra ValueError, trong khi các đầu vào khác vẫn giữ nguyên hành vi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
networking
Loại issue
Lỗi
Độ khó
1/5
Thời gian dự kiến
Dưới một giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.