Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7323c4d

Browse files
[3.11] gh-117021: Fix integer overflow in PyLong_AsPid() on non-Windows 64-bit platforms (GH-117064) (GH-117070) (GH-117075)
(cherry picked from commit da2f9d1) Co-authored-by: Serhiy Storchaka <[email protected]> (cherry picked from commit 519b2ae)
1 parent 2f1806e commit 7323c4d

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

Include/Python.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
#include "bytearrayobject.h"
5050
#include "bytesobject.h"
5151
#include "unicodeobject.h"
52+
#include "cpython/code.h"
53+
#include "cpython/initconfig.h"
54+
#include "pystate.h"
55+
#include "pyerrors.h"
5256
#include "longobject.h"
5357
#include "cpython/longintrepr.h"
5458
#include "boolobject.h"
@@ -68,14 +72,11 @@
6872
#include "cpython/classobject.h"
6973
#include "fileobject.h"
7074
#include "pycapsule.h"
71-
#include "cpython/code.h"
7275
#include "pyframe.h"
7376
#include "traceback.h"
7477
#include "sliceobject.h"
7578
#include "cpython/cellobject.h"
7679
#include "iterobject.h"
77-
#include "cpython/initconfig.h"
78-
#include "pystate.h"
7980
#include "cpython/genobject.h"
8081
#include "descrobject.h"
8182
#include "genericaliasobject.h"
@@ -85,7 +86,6 @@
8586
#include "cpython/picklebufobject.h"
8687
#include "cpython/pytime.h"
8788
#include "codecs.h"
88-
#include "pyerrors.h"
8989
#include "pythread.h"
9090
#include "cpython/context.h"
9191
#include "modsupport.h"

Include/longobject.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,24 @@ PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
3434
#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
3535
#define _Py_PARSE_PID "i"
3636
#define PyLong_FromPid PyLong_FromLong
37-
#define PyLong_AsPid PyLong_AsLong
37+
# ifndef Py_LIMITED_API
38+
# define PyLong_AsPid _PyLong_AsInt
39+
# elif SIZEOF_INT == SIZEOF_LONG
40+
# define PyLong_AsPid PyLong_AsLong
41+
# else
42+
static inline int
43+
PyLong_AsPid(PyObject *obj)
44+
{
45+
int overflow;
46+
long result = PyLong_AsLongAndOverflow(obj, &overflow);
47+
if (overflow || result > INT_MAX || result < INT_MIN) {
48+
PyErr_SetString(PyExc_OverflowError,
49+
"Python int too large to convert to C int");
50+
return -1;
51+
}
52+
return (int)result;
53+
}
54+
# endif
3855
#elif SIZEOF_PID_T == SIZEOF_LONG
3956
#define _Py_PARSE_PID "l"
4057
#define PyLong_FromPid PyLong_FromLong
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix integer overflow in :c:func:`PyLong_AsPid` on non-Windows 64-bit
2+
platforms.

Modules/_testcapimodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8221,6 +8221,7 @@ PyInit__testcapi(void)
82218221
PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
82228222
PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
82238223
PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
8224+
PyModule_AddObject(m, "SIZEOF_PID_T", PyLong_FromSsize_t(sizeof(pid_t)));
82248225
PyModule_AddObject(m, "Py_Version", PyLong_FromUnsignedLong(Py_Version));
82258226
Py_INCREF(&PyInstanceMethod_Type);
82268227
PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);

0 commit comments

Comments
 (0)