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
Undefined behavior in Py_ABS() macro with LLONG_MIN #141370
When the value passed to Py_ABS() macro is LLONG_MIN (or any min value of a signed integer), the macro suffers from undefined behavior. This was detected in a failure to build the OSS-Fuzz test suite.
I don't think it's an issue. Same happens for abs/labs/llabs from the C stdlib, when the absolute value can't be represented in two’s complement:
$ cat a.c
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
int main()
{
long long lmin = LLONG_MIN, lmax = LLONG_MAX;
printf("%lld %lld %lld\n", lmin, llabs(lmin), lmax);
return 0;
}
$ cc a.c && ./a.out
-9223372036854775808 -9223372036854775808 9223372036854775807
I suggest either close this or reclassify as a documentation issue. We could add remark like this: "If the result cannot be represented, the behavior is undefined."
float_as_integer_ratio_impl() in Objects/floatobject.c: here we are safe, because the exponent range will not hit INT_MIN (even for quadruple precision it fits into 16bit int).
_PyLong_GCD() in Objects/longobject.c: here we just can't have LONG_MIN or LLONG_MIN value in PyLong with <= 2 digits.
w_PyLong() in Python/marshal.c: here we cast result to unsigned type.
r_PyLong() in Python/marshal.c: here we are safe, because -SIZE32_MAX <= n <= SIZE32_MAX
_Py_strhex_impl() in Python/pystrhex.c: again cast to unsigned
pytime_divide() in Python/pytime.c: here is just integer division with divisor k>=2.
If you think all is ok with above, I'll reclassify this as a documentation issue.
Activity
skirpichev commentedon Nov 10, 2025
I don't think it's an issue. Same happens for abs/labs/llabs from the C stdlib, when the absolute value can't be represented in two’s complement:
I suggest either close this or reclassify as a documentation issue. We could add remark like this: "If the result cannot be represented, the behavior is undefined."
serhiy-storchaka commentedon Nov 11, 2025
This is partially a documentation issue, but we should also analyze every use of
Py_ABSin the C code.skirpichev commentedon Nov 11, 2025
I see few instances:
float_as_integer_ratio_impl()in Objects/floatobject.c: here we are safe, because theexponentrange will not hit INT_MIN (even for quadruple precision it fits into 16bit int)._PyLong_GCD()in Objects/longobject.c: here we just can't have LONG_MIN or LLONG_MIN value in PyLong with <= 2 digits.w_PyLong()in Python/marshal.c: here we cast result to unsigned type.r_PyLong()in Python/marshal.c: here we are safe, because -SIZE32_MAX <= n <= SIZE32_MAX_Py_strhex_impl()in Python/pystrhex.c: again cast to unsignedpytime_divide()in Python/pytime.c: here is just integer division with divisor k>=2.If you think all is ok with above, I'll reclassify this as a documentation issue.
Details
pythongh-141370: document undefined behavior of Py_ABS()
serhiy-storchaka commentedon Nov 12, 2025
Technically, negation of INT_MIN is an undefined behavior.
serhiy-storchaka commentedon Nov 12, 2025
To avoid an undefined behavior we should handle the special case separately using unsigned arithmetic.
skirpichev commentedon Nov 12, 2025
Yes, as an example (though, I think it's only corner case; ditto for other signed integer types).
Probably, this is possible, but it will be too costly just for one special case.
7 remaining items
skirpichev commentedon Nov 14, 2025
@serhiy-storchaka, @sethmlarson, as documentation change was merged - I'm closing this.
Please reopen if you think that any from few usage cases in the CPython misuse this macro.
pythongh-141370: Fix undefined behavior when using Py_ABS()
serhiy-storchaka commentedon Nov 14, 2025
There is an undefined behavior in two of these places.
gh-141370: Fix undefined behavior when using Py_ABS() (GH-141548)
pythongh-141370: Fix undefined behavior when using Py_ABS() (pythonGH…
[3.13] pythongh-141370: Fix undefined behavior when using Py_ABS() (p…
[3.13] gh-141370: Fix undefined behavior when using Py_ABS() (GH-141548…
pythongh-141370: document undefined behavior of Py_ABS() (pythonGH-14…
pythongh-141370: Fix undefined behavior when using Py_ABS() (pythonGH…
[3.14] gh-141370: Fix undefined behavior when using Py_ABS() (GH-141548…