gh-145166: Fix crash in tzinfo.fromutc() when subclass __new__ returns non-datetime#145181
gh-145166: Fix crash in tzinfo.fromutc() when subclass __new__ returns non-datetime#145181HELLPUSYY666 wants to merge 1 commit intopython:mainfrom
Conversation
Misc/NEWS.d/next/Library/2026-02-24-19-14-43.gh-issue-145166.bG_Rp2.rst
Outdated
Show resolved
Hide resolved
f339b73 to
32d0457
Compare
|
This fix in Could you add a test for def test_utctimetuple_subclass_new_returns_non_datetime(self):
call_count = 0
class EvilDatetime(self.theclass):
def __new__(cls, *args, **kwargs):
nonlocal call_count
call_count += 1
if call_count > 1:
return bytearray(b'\x00' * 200)
return super().__new__(cls, *args, **kwargs)
class SimpleTZ(tzinfo):
def utcoffset(self, dt): return timedelta(hours=5)
def dst(self, dt): return timedelta(0)
def tzname(self, dt): return "Test"
tz = SimpleTZ()
dt = EvilDatetime(2000, 6, 15, 12, 0, 0, tzinfo=tz)
with self.assertRaises(TypeError):
dt.utctimetuple() |
32d0457 to
afa85ee
Compare
@raminfp Thanks for good test, i added this to my PR |
|
It was great, but I think this |
…returns non-datetime
afa85ee to
6efe847
Compare
@raminfp Done! |
|
@StanFromIreland Hi, could you please review this PR and merge it if everything looks good? |
|
Please don't force push, see the devguide for more information. Also, please note that this is a volunteer project, I try to review within ~48 hours, please don't ping me (unless it has been a long time, i.e. a month, since my review). |
Problem
In
tzinfo_fromutc(), the result ofadd_datetime_timedelta()is castto
PyDateTime_DateTime*without a type check. If adatetimesubclass__new__returns a non-datetime object, the interpreter crashes withSIGABRTdue to out-of-bounds memory reads insidenormalize_y_m_d().Fix
PyDateTime_Check()after both calls toadd_datetime_timedelta()in
Modules/_datetimemodule.c- raisesTypeErrorinstead of crashingisinstance()checks inLib/_pydatetime.pyLib/test/datetimetester.pyTesting
Regression test passes for both
TestDateTimeTZ_PureandTestDateTimeTZ_Fast.