Repository navigation
Closed
Labels
Description
Describe the issue:
There is a signed integer overflow in numpy/_core/src/multiarray/datetime.c when converting a datetime64 object near INT64_MIN to a Python object via .item().
Specifically, on this line:
numpy/numpy/_core/src/multiarray/datetime.c
Line 171 in 7dfe67e
| npy_int64 days = (*days_) - (365*30 + 7); |
Reproduce the code example:
import numpy as np
info = np.iinfo(np.int64)
dt64 = np.datetime64(info.min + 1, "D")
dt64.item()
# ../../numpy/_core/src/multiarray/datetime.c:171:31: runtime error: signed integer overflow:
# -9223372036854775807 - 10957 cannot be represented in type 'npy_int64' (aka 'long')Error message:
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../numpy/_core/src/common/cblasfuncs.c:548:9
../../numpy/_core/src/multiarray/datetime.c:171:31: runtime error: signed integer overflow: -9223372036854775807 - 10957 cannot be represented in type 'npy_int64' (aka 'long')
#0 0x7b5de65e29e7 in days_to_yearsdays /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/datetime.c:171:31
#1 0x7b5de65a9411 in set_datetimestruct_days /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/datetime.c:265:17
#2 0x7b5de65adefc in NpyDatetime_ConvertDatetime64ToDatetimeStruct /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/datetime.c:486:13
#3 0x7b5de65ccaf1 in convert_datetime_to_pyobject /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/datetime.c:2863:9
#4 0x7b5de5afd18d in DATETIME_getitem /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/arraytypes.c.src:1182:12
#5 0x7b5de672bf39 in PyArray_GETITEM /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/dtypemeta.h:304:12
#6 0x7b5de672bdd7 in PyArray_MultiIndexGetItem /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/item_selection.c:3161:12
#7 0x7b5de67c9f15 in array_toscalar /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/methods.c:748:12
#8 0x00000181878e in cfunction_call (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x181878e)
#9 0x000001851a76 in _PyObject_Call (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1851a76)
#10 0x7b5de61ef314 in gentype_generic_method /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/scalartypes.c.src:143:15
#11 0x7b5de61eae2f in gentype_item /home/alvaro/numpy/build/cp313/../../numpy/_core/src/multiarray/scalartypes.c.src:2123:12
#12 0x000001ad27c1 in method_vectorcall_VARARGS.llvm.16033483530146946230.warm (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1ad27c1)
#13 0x000001823185 in _PyEval_EvalFrameDefault (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1823185)
#14 0x0000018aa113 in PyEval_EvalCode (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x18aa113)
#15 0x0000018a5239 in run_eval_code_obj (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x18a5239)
#16 0x0000018dc182 in run_mod.llvm.12468226955389376524 (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x18dc182)
#17 0x000001a08446 in pyrun_file (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1a08446)
#18 0x000001a07b36 in _PyRun_SimpleFileObject (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1a07b36)
#19 0x000001a0764f in _PyRun_AnyFileObject (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1a0764f)
#20 0x000001a075f5 in pymain_run_file_obj (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1a075f5)
#21 0x000001a074d9 in pymain_run_file (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1a074d9)
#22 0x000001997e91 in Py_RunMain (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1997e91)
#23 0x000001997b44 in pymain_main.llvm.12760885333831575200 (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x1997b44)
#24 0x00000199792c in main (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x199792c)
#25 0x7f5dfbef3680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: 18472003bbf1c5f098a09b5016b9b8bd4c7c59f0)
#26 0x7f5dfbef3797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: 18472003bbf1c5f098a09b5016b9b8bd4c7c59f0)
#27 0x0000019fff68 in _start (/home/alvaro/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/bin/python3.13+0x19fff68)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../numpy/_core/src/multiarray/datetime.c:171:31Python and NumPy Versions:
2.6.0.dev0+git20260618.7dfe67e
3.13.9 (main, Oct 28 2025, 12:10:42) [Clang 20.1.4 ]
Runtime Environment:
No response
How does this issue affect you or how did you find it:
This was originally found in pandas-dev/pandas#65831.
Reacted by AnkitAhlawat
Activity
AnkitAhlawat7742 commentedon Jun 19, 2026
@Alvaro-Kothe , can you please try this solution for pandas ?
Alvaro-Kothe commentedon Jun 22, 2026
The idea from the patch in pandas-dev/pandas#65831 is similar to yours.
ngoldbaum commentedon Jun 23, 2026
Linking my PR comment: #31688 (review)
This pattern of failing to check for overflow in signed integer operations happens all over those conversions. Rather than fixing just this one case we should have a more general fix, IMO.
AnkitAhlawat7742 commentedon Jun 23, 2026
Thank you for the feedback, @ngoldbaum. You're absolutely right that there are other potential overflow locations in the datetime conversion code.
can we take this fix for the issue which is raised by @Alvaro-Kothe ? and I can create a follow-up PR for a comprehensive overflow
what you say ?