GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing github.com — BUG: Fix signed integer overflow in datetime by AnkitAhlawat7742 · Pull Request #32218 · numpy/numpy Conversation
Contributor
FixPR summaryFollow-up to #31688. Fixes all signed-integer overflow undefined behaviour in datetime.c identified in the review by @ngoldbaum . new First time committer introductionNA
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
jorenham
reviewed
Uh oh!
There was an error while loading. Please reload this page. Co-authored-by: Joren Hammudoglu <[email protected]>
ngoldbaum
reviewed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. This is happening because of missed overflow checking for weeks. This OverflowError here is new and maybe shouldn't be happening? I'm not sure. The same error happens for 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. All reactions
Uh oh!
There was an error while loading. Please reload this page.
Contributor
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
|
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 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 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." |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.