GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: Fix signed integer overflow in datetime by AnkitAhlawat7742 · Pull Request #32218 · numpy/numpy

Conversation

@AnkitAhlawat7742

@AnkitAhlawat7742 AnkitAhlawat7742 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Fix

#31926

PR summary

Follow-up to #31688. Fixes all signed-integer overflow undefined behaviour in datetime.c identified in the review by @ngoldbaum . new ACCUM128 macro that keeps intermediate products in 128-bit and narrows to int64 only at the final step, avoiding false overflow on expressions like days*24 where the intermediate transiently exceeds INT64_MAX but the final result fits.

First time committer introduction

NA

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
AnkitAhlawat7742 added 2 commits August 7, 2026 14:56
@AnkitAhlawat7742
Fix signed integer overflow
297ebae
@AnkitAhlawat7742
fix correctly raises OverflowError
b789a53
jorenham
jorenham reviewed Aug 7, 2026
View reviewed changes
Comment thread numpy/typing/tests/data/pass/scalars.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AnkitAhlawat7742 @jorenham
Update numpy/typing/tests/data/pass/scalars.py
ead3b5c
Co-authored-by: Joren Hammudoglu <[email protected]>
@ngoldbaum ngoldbaum changed the title Fix signed integer overflow BUG: Fix signed integer overflow Aug 7, 2026
@ngoldbaum ngoldbaum changed the title BUG: Fix signed integer overflow BUG: Fix signed integer overflow in datetime Aug 7, 2026
ngoldbaum
ngoldbaum reviewed Aug 7, 2026
View reviewed changes

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked an AI model to look this over and try to find bugs. Here are some scripts that crash or produce incorrect results on this PR:

import numpy as np
np.full(1000, 2**62, dtype='i8').view('M8[Y]').astype('M8[M]')  # segfault
import numpy as np
np.array([b'-0001-01-01'] * 1000, dtype='S').astype('M8[as]')  # segfault

These are happening because you're trying to use the CPython C API in a loop that doesn't have an attached thread state.

>>> import numpy as np
... # true result 280730621764244204390 can't fit int64; era * 146097 wraps
... print(np.datetime64(2**63 - 1, 'M').astype('M8[D]').view(np.int64))
4029460658600930150
>>> np.datetime64('90000000000000000-01-01', 'D')
np.datetime64('-11010939711066219-09-18')

This is happening because you missed overflow checking for days.

>>> import numpy as np
... print(np.datetime64(2**63 - 1, 'W'))
...
25252734927768524-07-21

This is happening because of missed overflow checking for weeks.

>>> import numpy as np
... dt = np.datetime64(2**63 - 1, 'Y')  # legal to construct
... dt.item()
...
OverflowError: Overflow in datetime conversion: datetime64 value is out of range for the requested unit

This OverflowError here is new and maybe shouldn't be happening? I'm not sure. The same error happens for repr(dt) and hash(dt), which is suboptimal.

import numpy as np
w = np.datetime64('-25252734927764585-06-07', 'W')  # date is exactly INT64_MIN days from epoch
print(w.view(np.int64))  # -1317624576693539401, silently one week off
# correct: -2**63 // 7 = -1317624576693539402

I think this is an off-by-one error?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AnkitAhlawat7742 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread numpy/_core/src/multiarray/datetime.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@charris charris added the 00 - Bug label Aug 7, 2026
@AnkitAhlawat7742
fix: Segfault scenario's fixed
403698a
@AnkitAhlawat7742

AnkitAhlawat7742 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

I asked an AI model to look this over and try to find bugs. Here are some scripts that crash or produce incorrect results on this PR:

import numpy as np
np.full(1000, 2**62, dtype='i8').view('M8[Y]').astype('M8[M]')  # segfault
import numpy as np
np.array([b'-0001-01-01'] * 1000, dtype='S').astype('M8[as]')  # segfault

These are happening because you're trying to use the CPython C API in a loop that doesn't have an attached thread state.

>>> import numpy as np
... # true result 280730621764244204390 can't fit int64; era * 146097 wraps
... print(np.datetime64(2**63 - 1, 'M').astype('M8[D]').view(np.int64))
4029460658600930150
>>> np.datetime64('90000000000000000-01-01', 'D')
np.datetime64('-11010939711066219-09-18')

This is happening because you missed overflow checking for days.

>>> import numpy as np
... print(np.datetime64(2**63 - 1, 'W'))
...
25252734927768524-07-21

This is happening because of missed overflow checking for weeks.

>>> import numpy as np
... dt = np.datetime64(2**63 - 1, 'Y')  # legal to construct
... dt.item()
...
OverflowError: Overflow in datetime conversion: datetime64 value is out of range for the requested unit

This OverflowError here is new and maybe shouldn't be happening? I'm not sure. The same error happens for repr(dt) and hash(dt), which is suboptimal.

import numpy as np
w = np.datetime64('-25252734927764585-06-07', 'W')  # date is exactly INT64_MIN days from epoch
print(w.view(np.int64))  # -1317624576693539401, silently one week off
# correct: -2**63 // 7 = -1317624576693539402

I think this is an off-by-one error?

@ngoldbaum ,
Thanks for the thorough review , These were valid bugs.
I am done the code changes and tested ...please have a look

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum

ngoldbaum commented Aug 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Here are some more scripts demonstrating issues:

import numpy as np
np.datetime64(np.datetime64(2000, 'Y'), 'as')
# SystemError: <class 'numpy.datetime64'> returned NULL without setting an exception
import numpy as np
a = np.zeros(1, 'M8[as]')
a[0] = np.datetime64(2000, 'Y')   # SystemError: error return without exception set
import numpy as np
np.array(['-0001-01-01'], dtype='T').astype('M8[as]')
# SystemError: <method 'astype' of 'numpy.ndarray' objects> returned NULL without setting an exception

All of these are happening because you need to explicitly set an exception if an underlying CPython C API or NumPy C API / internal API call doesn't do that for you. Maybe you want the npy_gil_error helper?

import numpy as np
w = np.datetime64('-25252734927764585-06-08', 'W')  # days == INT64_MIN + 1, divisible by 7
print(w.view(np.int64))  # prints -1317624576693539402; correct is -1317624576693539401

June 9-12 are wrong too; June 7 is now correct. Maybe in your testing you could make sure that all the dates within a week of the MIN/MAX cutoffs are correct for the week conversions, within a month for the month conversions and so on. Don't make your tests over-parameterized, just make a single test that probes the edge cases.

import numpy as np
print(np.datetime64(2**63 - 1, 'Y'))  # 1970 + INT64_MAX overflows int64 (UB)
# prints -9223372036854773839 — a wrapped garbage year, no error

The revert restored out->year = 1970 + dt verbatim.

import numpy as np
a = np.array(['1969-12-31T23:59:50.776627963145224192'], dtype='M8[300as]')
print(a.view(np.int64)[0])  # exactly INT64_MIN attoseconds
# prints +30744573456182585 (positive, pre-epoch!); correct: -2**63 // 300 = -30744573456182587

This is leftover from the last round.

import numpy as np
np.array(['-0001-01-01'], dtype='M8[as]') # OverflowError
print('U astype:', np.array(['-0001-01-01']).astype('M8[as]')[0])  # NaT, silently

It looks like there's a corner case in the string-to-datetime casts.

Are you using an AI model to work on this? All I'm doing is prompting it to "carefully review PR 32218 for correct and idiomatic use of the C language and any APIs used."

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This branch has not been deployed

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

Reviewers

@ngoldbaum ngoldbaum ngoldbaum left review comments
@jorenham jorenham jorenham left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@AnkitAhlawat7742 @ngoldbaum @jorenham @charris
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.