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
Unchecked signed integer overflow in PyLong_AsPid() #117021
If pid_t has the same size as int, PyLong_AsPid is defined as PyLong_AsLong. if the size of int is less than the size of long, there are values out of the C int range but in the C long range. Calling PyLong_AsPid() with such argument will not raise an exception, but casting the result out of the C int range to pid_t has undefined behavior.
Most non-Windows 64-bit platforms are affected.
The simplest solution is to define PyLong_AsPid as PyLong_AsInt. It only applicable in 3.13, because PyLong_AsInt is new in 3.13. In older versions there is _PyLong_AsInt, but it is declared in Include/cpython/longobject.h, not in Include/longobject.h.
Not sure what you mean by "define" PyLong_AsPid, but if you mean #define, then I think a real function should be exposed instead, for the benefits of non-C/C++ users.
Still not good if Py_LIMITED_API < 0x030d0000, but it is the best that can be done with the limited C API.
On other hand, the status of PyLong_AsPid is unclear. It is not documented, not even mentioned in changelogs, but de-facto it is always available, independently from the Py_LIMITED_API setting. It was added by @gpshead in fb94c5f. Perhaps it should be treated as a private C API, but its name is not underscored.
Similar functions PyLong_FromSocket_t and PyLong_AsSocket_t were made public, but than were made private again.
Activity
pitrou commentedon Mar 19, 2024
Not sure what you mean by "define" PyLong_AsPid, but if you mean
#define, then I think a real function should be exposed instead, for the benefits of non-C/C++ users.serhiy-storchaka commentedon Mar 19, 2024
Currently it is defined by
#define.vstinner commentedon Mar 19, 2024
I don't get thr context of this issue. Which functions are you modifying? In which code? Is it related to Argument Clinic?
serhiy-storchaka commentedon Mar 19, 2024
There are also other definitions in different branches, but they are okay.
It could be defined as
Still not good if Py_LIMITED_API < 0x030d0000, but it is the best that can be done with the limited C API.
On other hand, the status of
PyLong_AsPidis unclear. It is not documented, not even mentioned in changelogs, but de-facto it is always available, independently from the Py_LIMITED_API setting. It was added by @gpshead in fb94c5f. Perhaps it should be treated as a private C API, but its name is not underscored.Similar functions
PyLong_FromSocket_tandPyLong_AsSocket_twere made public, but than were made private again.serhiy-storchaka commentedon Mar 19, 2024
Opened a discussion about status of
PyLong_FromPidandPyLong_AsPid: https://discuss.python.org/t/status-of-pylong-frompid-and-pylong-aspid/48988.pythongh-117021: Fix integer overflow in PyLong_AsPid() on non-Window…
gh-117021: Fix integer overflow in PyLong_AsPid() on non-Windows 64-b…
pythongh-117021: Fix integer overflow in PyLong_AsPid() on non-Window…
[3.12] pythongh-117021: Fix integer overflow in PyLong_AsPid() on non…
[3.12] gh-117021: Fix integer overflow in PyLong_AsPid() on non-Windo…
[3.11] [3.12] pythongh-117021: Fix integer overflow in PyLong_AsPid()…
[3.11] gh-117021: Fix integer overflow in PyLong_AsPid() on non-Windo…
pythongh-117021: Fix integer overflow in PyLong_AsPid() on non-Window…
Squashed commit of the following:
Squashed commit of the following:
pythongh-117021: Fix integer overflow in PyLong_AsPid() on non-Window…