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
undefined behaviour: signed integer overflow in threadmodule.c #77813
Comments
Modules/_threadmodule.c:52:47: runtime error: signed integer overflow: 2387971499048 + 9223372036000000000 cannot be represented in type 'long' |
Looks like this is what my thread.patch was fixing in <https://bugs.python.org/issue1621#msg271057\>. You’re welcome to use my patch, but I won’t have time to work on it myself. |
How do you reproduce the issue? The thread module should limit the maximum timeout to PY_TIMEOUT_MAX. Maybe PY_TIMEOUT_MAX is too big? |
I've created a PR based on Martin Panter's patch. |
Victor, if you run the test suite, one of the test cases should trigger the overflow. I used to compile with Undefined Behaviour Sanitizer to print messages when these errors occur; see <https://bugs.python.org/issue1621#msg271118\> for my setup at the time. I presume Antoine did something similar. I do not remember, but suspect the test case might be the following lines of “BaseLockTests.test_timeout” in Lib/test/lock_tests.py, testing a fraction of a second less than PY_TIMEOUT_MAX: # TIMEOUT_MAX is ok
lock.acquire(timeout=TIMEOUT_MAX) Perhaps reducing PY_TIMEOUT_MAX by a few centuries would be one way to avoid the problem. In my patch I avoided the problem by rearranging the arithmetic, so that the timeout value is only compared and reduced, never added. |
In short, a+b can overflow, but a-b cannot? |
I think it's more that by always checking the elapsed time against |
I think PR #28674 has resolved this issue. |
You're right. _threadmodule.c now uses _PyDeadline_Init() which calls _PyTime_Add(), and _PyTime_Add() prevents integer overflows; Extract of its implementation: // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
static inline int
pytime_add(_PyTime_t *t1, _PyTime_t t2)
{
if (t2 > 0 && *t1 > _PyTime_MAX - t2) {
*t1 = _PyTime_MAX;
return -1;
}
else if (t2 < 0 && *t1 < _PyTime_MIN - t2) {
*t1 = _PyTime_MIN;
return -1;
}
else {
*t1 += t2;
return 0;
}
} |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: