You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Crash: NULL deref in _datetime when a static type outlives its module #151039
NULL pointer dereference in the _datetime C module → SIGSEGV. Reproducer:
importsys, gcimport_datetimetd=_datetime.timedelta# static C type, survives the moduledelsys.modules['_datetime']
del_datetimesys.modules['_datetime'] =None# block re-importgc.collect() # module object is collectedtd(seconds=2) # -> Segmentation fault
gdb at the crash: st == (datetime_state *) 0x0, with an ImportError already pending.
Root cause
The datetime types are static and can outlive the module, so per-module state (gh-117398) is fetched via _get_current_state(), which can return NULL when the module weakref is dead and re-import fails (blocked in sys.modules):
Callers dereference the returned state without a NULL check. At the crash site CONST_US_PER_SECOND(st) is st->us_per_second, so st == NULL → NULL deref:
changed the title [-]Crash: NULL deref in _datetime when a static datetime type outlives its module (GET_CURRENT_STATE callers don't check for NULL)[/-][+]Crash: NULL deref in _datetime when a static type outlives its module[/+]on Jun 7, 2026
What happened?
NULL pointer dereference in the
_datetimeC module →SIGSEGV. Reproducer:gdbat the crash:st == (datetime_state *) 0x0, with anImportErroralready pending.Root cause
The datetime types are static and can outlive the module, so per-module state (gh-117398) is fetched via
_get_current_state(), which can returnNULLwhen the module weakref is dead and re-import fails (blocked insys.modules):cpython/Modules/_datetimemodule.c
Lines 160 to 180 in ab93017
Callers dereference the returned state without a NULL check. At the crash site
CONST_US_PER_SECOND(st)isst->us_per_second, sost == NULL→ NULL deref:cpython/Modules/_datetimemodule.c
Lines 2819 to 2844 in ab93017
Originally hit via
./python -X lazy_imports=all -m test test_datetime(segfault intest_strptime;test_datetimeis also listed in #149640).Versions
ab930175e7e). Present since 3.14 (Isolate the _datetime extension module #117398) → 3.14, 3.15, main. Linux.CC=/opt/llvm/bin/clang CXX=/opt/llvm/bin/clang++ LDFLAGS='-fuse-ld=lld' ./configure; clang/lld 23.0.0git (4695c84).Linked PRs
_datetimetypes outlive_datetimemodule #151044_datetimetypes outlive_datetimemodule (GH-151044) #151143_datetimetypes outlive_datetimemodule (GH-151044) #151144