bpo-31406: Fix crash due to lack of type checking in subclassing. (#3… · python/cpython@3cedf46

Original file line numberDiff line numberDiff line change

@@ -2082,13 +2082,17 @@ dec_from_long(PyTypeObject *type, const PyObject *v,

20822082

/* Return a new PyDecObject from a PyLongObject. Use the context for

20832083

conversion. */

20842084

static PyObject *

2085-

PyDecType_FromLong(PyTypeObject *type, const PyObject *pylong,

2086-

PyObject *context)

2085+

PyDecType_FromLong(PyTypeObject *type, const PyObject *v, PyObject *context)

20872086

{

20882087

PyObject *dec;

20892088

uint32_t status = 0;

20902089
2091-

dec = dec_from_long(type, pylong, CTX(context), &status);

2090+

if (!PyLong_Check(v)) {

2091+

PyErr_SetString(PyExc_TypeError, "argument must be an integer");

2092+

return NULL;

2093+

}

2094+
2095+

dec = dec_from_long(type, v, CTX(context), &status);

20922096

if (dec == NULL) {

20932097

return NULL;

20942098

}

@@ -2104,15 +2108,20 @@ PyDecType_FromLong(PyTypeObject *type, const PyObject *pylong,

21042108

/* Return a new PyDecObject from a PyLongObject. Use a maximum context

21052109

for conversion. If the conversion is not exact, set InvalidOperation. */

21062110

static PyObject *

2107-

PyDecType_FromLongExact(PyTypeObject *type, const PyObject *pylong,

2111+

PyDecType_FromLongExact(PyTypeObject *type, const PyObject *v,

21082112

PyObject *context)

21092113

{

21102114

PyObject *dec;

21112115

uint32_t status = 0;

21122116

mpd_context_t maxctx;

21132117
2118+

if (!PyLong_Check(v)) {

2119+

PyErr_SetString(PyExc_TypeError, "argument must be an integer");

2120+

return NULL;

2121+

}

2122+
21142123

mpd_maxcontext(&maxctx);

2115-

dec = dec_from_long(type, pylong, &maxctx, &status);

2124+

dec = dec_from_long(type, v, &maxctx, &status);

21162125

if (dec == NULL) {

21172126

return NULL;

21182127

}