]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-117021: Fix integer overflow in PyLong_AsPid() on non-Windows 64-bit platfo...
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 21 Mar 2024 09:43:42 +0000 (11:43 +0200)
committerGitHub <noreply@github.com>
Thu, 21 Mar 2024 09:43:42 +0000 (09:43 +0000)
(cherry picked from commit da2f9d1417a7d28df6e1ced87d64ecf28acb0a5f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit 519b2ae22b54760475bbf62b9558d453c703f9c6)

Include/Python.h
Include/longobject.h
Misc/NEWS.d/next/C API/2024-03-20-13-13-22.gh-issue-117021.0Q5jBx.rst [new file with mode: 0644]
Modules/_testcapimodule.c

index 52a7aac6ba6cb6eb845512a033b94165ef3b4fa8..6ee3b0a2f9dc63c557349dcae97d1a03b6596305 100644 (file)
 #include "bytearrayobject.h"
 #include "bytesobject.h"
 #include "unicodeobject.h"
+#include "cpython/code.h"
+#include "cpython/initconfig.h"
+#include "pystate.h"
+#include "pyerrors.h"
 #include "longobject.h"
 #include "cpython/longintrepr.h"
 #include "boolobject.h"
 #include "cpython/classobject.h"
 #include "fileobject.h"
 #include "pycapsule.h"
-#include "cpython/code.h"
 #include "pyframe.h"
 #include "traceback.h"
 #include "sliceobject.h"
 #include "cpython/cellobject.h"
 #include "iterobject.h"
-#include "cpython/initconfig.h"
-#include "pystate.h"
 #include "cpython/genobject.h"
 #include "descrobject.h"
 #include "genericaliasobject.h"
@@ -85,7 +86,6 @@
 #include "cpython/picklebufobject.h"
 #include "cpython/pytime.h"
 #include "codecs.h"
-#include "pyerrors.h"
 #include "pythread.h"
 #include "cpython/context.h"
 #include "modsupport.h"
index 81ba1239a69ecf502965cd248a94ef8dafaabfb3..778cb6271903757194bf5c59e60493d631650a5c 100644 (file)
@@ -34,7 +34,24 @@ PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
 #define _Py_PARSE_PID "i"
 #define PyLong_FromPid PyLong_FromLong
-#define PyLong_AsPid PyLong_AsLong
+# ifndef Py_LIMITED_API
+#   define PyLong_AsPid _PyLong_AsInt
+# elif SIZEOF_INT == SIZEOF_LONG
+#   define PyLong_AsPid PyLong_AsLong
+# else
+static inline int
+PyLong_AsPid(PyObject *obj)
+{
+    int overflow;
+    long result = PyLong_AsLongAndOverflow(obj, &overflow);
+    if (overflow || result > INT_MAX || result < INT_MIN) {
+        PyErr_SetString(PyExc_OverflowError,
+                        "Python int too large to convert to C int");
+        return -1;
+    }
+    return (int)result;
+}
+# endif
 #elif SIZEOF_PID_T == SIZEOF_LONG
 #define _Py_PARSE_PID "l"
 #define PyLong_FromPid PyLong_FromLong
diff --git a/Misc/NEWS.d/next/C API/2024-03-20-13-13-22.gh-issue-117021.0Q5jBx.rst b/Misc/NEWS.d/next/C API/2024-03-20-13-13-22.gh-issue-117021.0Q5jBx.rst
new file mode 100644 (file)
index 0000000..2f93e1e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix integer overflow in :c:func:`PyLong_AsPid` on non-Windows 64-bit
+platforms.
index a4411d1ea1d0b75fa95568262ed8458566b957cb..becf203d95603c5088c71d920a8f9a3d86249857 100644 (file)
@@ -8221,6 +8221,7 @@ PyInit__testcapi(void)
     PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
     PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
     PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
+    PyModule_AddObject(m, "SIZEOF_PID_T", PyLong_FromSsize_t(sizeof(pid_t)));
     PyModule_AddObject(m, "Py_Version", PyLong_FromUnsignedLong(Py_Version));
     Py_INCREF(&PyInstanceMethod_Type);
     PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);