gh-117021: Fix integer overflow in PyLong_AsPid() on non-Windows 64-bit platforms (GH-117064)

This commit is contained in:
Serhiy Storchaka 2024-03-20 15:39:53 +02:00 committed by GitHub
parent 8182319de3
commit 519b2ae22b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 74 additions and 2 deletions

View file

@ -92,12 +92,24 @@ pylong_fromnativebytes(PyObject *module, PyObject *args)
return res;
}
static PyObject *
pylong_aspid(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
pid_t value = PyLong_AsPid(arg);
if (value == -1 && PyErr_Occurred()) {
return NULL;
}
return PyLong_FromPid(value);
}
static PyMethodDef test_methods[] = {
_TESTCAPI_CALL_LONG_COMPACT_API_METHODDEF
{"pylong_fromunicodeobject", pylong_fromunicodeobject, METH_VARARGS},
{"pylong_asnativebytes", pylong_asnativebytes, METH_VARARGS},
{"pylong_fromnativebytes", pylong_fromnativebytes, METH_VARARGS},
{"pylong_aspid", pylong_aspid, METH_O},
{NULL},
};

View file

@ -3975,6 +3975,7 @@ PyInit__testcapi(void)
PyModule_AddObject(m, "SIZEOF_WCHAR_T", PyLong_FromSsize_t(sizeof(wchar_t)));
PyModule_AddObject(m, "SIZEOF_VOID_P", PyLong_FromSsize_t(sizeof(void*)));
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);

View file

@ -746,6 +746,17 @@ pylong_asvoidptr(PyObject *module, PyObject *arg)
return Py_NewRef((PyObject *)value);
}
static PyObject *
pylong_aspid(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
pid_t value = PyLong_AsPid(arg);
if (value == -1 && PyErr_Occurred()) {
return NULL;
}
return PyLong_FromPid(value);
}
static PyMethodDef test_methods[] = {
_TESTLIMITEDCAPI_TEST_LONG_AND_OVERFLOW_METHODDEF
@ -773,6 +784,7 @@ static PyMethodDef test_methods[] = {
{"pylong_as_size_t", pylong_as_size_t, METH_O},
{"pylong_asdouble", pylong_asdouble, METH_O},
{"pylong_asvoidptr", pylong_asvoidptr, METH_O},
{"pylong_aspid", pylong_aspid, METH_O},
{NULL},
};