Skip to content

Comments

gh-145166: Fix crash in tzinfo.fromutc() when subclass __new__ returns non-datetime#145181

Open
HELLPUSYY666 wants to merge 1 commit intopython:mainfrom
HELLPUSYY666:fix-tzinfo-fromutc-type-confusion
Open

gh-145166: Fix crash in tzinfo.fromutc() when subclass __new__ returns non-datetime#145181
HELLPUSYY666 wants to merge 1 commit intopython:mainfrom
HELLPUSYY666:fix-tzinfo-fromutc-type-confusion

Conversation

@HELLPUSYY666
Copy link

Problem

In tzinfo_fromutc(), the result of add_datetime_timedelta() is cast
to PyDateTime_DateTime* without a type check. If a datetime subclass
__new__ returns a non-datetime object, the interpreter crashes with
SIGABRT due to out-of-bounds memory reads inside normalize_y_m_d().

Fix

  • Added PyDateTime_Check() after both calls to add_datetime_timedelta()
    in Modules/_datetimemodule.c - raises TypeError instead of crashing
  • Added equivalent isinstance() checks in Lib/_pydatetime.py
  • Added regression test in Lib/test/datetimetester.py

Testing

Regression test passes for both TestDateTimeTZ_Pure and TestDateTimeTZ_Fast.

@python-cla-bot
Copy link

python-cla-bot bot commented Feb 24, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant