Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug due to UB in conversion from python int to ZZ (python 3.11, 32 bit, gcc12) #34997

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/sage/arith/long.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ cdef inline bint integer_check_long_py(x, long* value, int* err):
sage: L += [-x for x in L] + [0, long_min()]
sage: for v in L:
....: assert check_long_py(int(v)) == v
sage: check_long_py(int(2^60))
1152921504606846976 # 64-bit
'Overflow (...)' # 32-bit
sage: check_long_py(int(2^61))
2305843009213693952 # 64-bit
'Overflow (...)' # 32-bit
sage: check_long_py(int(2^62))
4611686018427387904 # 64-bit
'Overflow (...)' # 32-bit
sage: check_long_py(int(2^63))
'Overflow (...)'
sage: check_long_py(int(2^100))
'Overflow (...)'
sage: check_long_py(int(long_max() + 1))
Expand Down Expand Up @@ -309,7 +320,12 @@ cdef inline bint integer_check_long_py(x, long* value, int* err):

cdef long lead
cdef long lead_2_overflow = (<long>1) << (BITS_IN_LONG - PyLong_SHIFT)
cdef long lead_3_overflow = (<long>1) << (BITS_IN_LONG - 2 * PyLong_SHIFT)
cdef long lead_3_overflow
if BITS_IN_LONG < 2 * PyLong_SHIFT:
tornaria marked this conversation as resolved.
Show resolved Hide resolved
# in this case 3 digit is always overflow
lead_3_overflow = 0
else:
lead_3_overflow = (<long>1) << (BITS_IN_LONG - 2 * PyLong_SHIFT)
if size == 0:
value[0] = 0
err[0] = 0
Expand Down