Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-141370: Fix undefined behavior when using Py_ABS()#141548
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
serhiy-storchaka commented Nov 14, 2025 • edited by bedevere-app bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-app bot
Uh oh!
There was an error while loading. Please reload this page.
Python/marshal.c Outdated
| uint64_tabs_value=long_export.value<-INT64_MAX | ||
| ? (uint64_t)INT64_MAX+ (uint64_t)-(long_export.value+INT64_MAX) | ||
| : (uint64_t)Py_ABS(long_export.value); |
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.
Can't we assume two's complement representation of integers and use simpler workaround? As this fix span several places, I would prefer if it could be refactored to a separate macro.
I.e. something like:
#defineMy_ABS(x, MAX) \ ((x) < 0 ? ((x) >= -MAX ? -(x) : (U##MAX >> 1) + 1) : (x))Nowadays two's complement is only case permitted by the C23 and is a de-facto standard. Do you have some system in mind on which we should care? I'm pretty sure all Tier 1-3 platforms fit to this picture.
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.
All Tier 1-3 platforms perhaps fine with the current code. We change the code because we cannot be sure that it work on all exotic platforms and that future versions of the C compiler will not interpret an undefined behavior in interesting way.
The current gcc does not generate additions and substractions for this PR. It generates something more smart, although not so smart as for Py_ABS(). Although for your proposition it generates more complex code.
Details
#include<limits.h>#definePy_ABS(x) ((x) < 0 ? -(x) : (x)) #defineMy_ABS(x, MAX) \ ((x) < 0 ? ((x) >= -MAX ? -(x) : (U##MAX >> 1) + 1) : (x)) unsigned intintabs0(intx){return (unsigned int)Py_ABS(x)} unsigned intintabs(intx){returnx<-INT_MAX ? (unsigned int)INT_MAX+ (unsigned int)-(x+INT_MAX) : (unsigned int)Py_ABS(x)} unsigned intintabs2(intx){returnMy_ABS(x, INT_MAX)} unsigned longlongabs0(longx){return (unsigned long)Py_ABS(x)} unsigned longlongabs(longx){returnx<-LONG_MAX ? (unsigned long)LONG_MAX+ (unsigned long)-(x+LONG_MAX) : (unsigned long)Py_ABS(x)} unsigned longlongabs2(longx){returnMy_ABS(x, LONG_MAX)}Anyway, the performance of this code is not critical (if there is any difference).
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.
All Tier 1-3 platforms perhaps fine with the current code.
(Yes, and I suspect tests might be redundant in fact.)
Although for your proposition it generates more complex code.
A different version:
#define__GMP_CAST(type, expr) ((type) (expr)) #defineNEG_CAST(T,x) (- (__GMP_CAST (T, (x) + 1) - 1)) #defineABS_CAST(T,x) ((x) >= 0 ? __GMP_CAST (T, x) : NEG_CAST (T, x))I found same approach in the GNU GMP, so just copied NIH code here.
Anyway, the performance of this code is not critical (if there is any difference).
Your solution looks ok for me. But in any case we should factor it to some macro.
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.
For this version gcc generates exactly the same code as for the current code. But it is now free from undefined behavior.
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev left a comment
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.
LGTM
This version triggers a warning on M$ compiler, but this is probably ok.
Include/pymacro.h Outdated
| #definePy_MAX(x, y) (((x) > (y)) ? (x) : (y)) | ||
| /* Absolute value of the number x */ | ||
| #define_Py_ABS_CAST(T,x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1))) |
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.
| #define_Py_ABS_CAST(T,x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1))) | |
| #define_Py_ABS_CAST(T,x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1))) |
picnixzNov 18, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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.
For posterity:
_Py_ABS_CAST(uint8_t, (int8_t)-128) ==128The (T)((x) + 1) - 1 is:
( ( (uint8_t) ( (-128) +1// -127 (still int8_t) ) // 129 (2's complement on 8 bits) ) -1// 128 (as an uint8_t) )Since -5 we have:
( ( (uint8_t) ( (-5) +1// -4 (still int8_t) ) // 252 (2's complement on 8 bits) ) -1// 251 (as an uint8_t) )And now
Uh oh!
There was an error while loading. Please reload this page.
706fdda into python:mainUh oh!
There was an error while loading. Please reload this page.
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
GH-142301 is a backport of this pull request to the 3.14 branch. |
…ythonGH-141548) (cherry picked from commit 706fdda) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
GH-142304 is a backport of this pull request to the 3.13 branch. |
…-141548) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Py_ABS()macro withLLONG_MIN#141370