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

Original file line number Diff line number Diff line change
@@ -2082,13 +2082,17 @@ dec_from_long(PyTypeObject *type, const PyObject *v,
2082 2082 /* Return a new PyDecObject from a PyLongObject. Use the context for
2083 2083 conversion. */
2084 2084 static PyObject *
2085 -PyDecType_FromLong(PyTypeObject *type, const PyObject *pylong,
2086 -PyObject *context)
2085 +PyDecType_FromLong(PyTypeObject *type, const PyObject *v, PyObject *context)
2087 2086 {
2088 2087 PyObject *dec;
2089 2088 uint32_t status = 0;
2090 2089
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);
2092 2096 if (dec == NULL) {
2093 2097 return NULL;
2094 2098 }
@@ -2104,15 +2108,20 @@ PyDecType_FromLong(PyTypeObject *type, const PyObject *pylong,
2104 2108 /* Return a new PyDecObject from a PyLongObject. Use a maximum context
2105 2109 for conversion. If the conversion is not exact, set InvalidOperation. */
2106 2110 static PyObject *
2107 -PyDecType_FromLongExact(PyTypeObject *type, const PyObject *pylong,
2111 +PyDecType_FromLongExact(PyTypeObject *type, const PyObject *v,
2108 2112 PyObject *context)
2109 2113 {
2110 2114 PyObject *dec;
2111 2115 uint32_t status = 0;
2112 2116 mpd_context_t maxctx;
2113 2117
2118 +if (!PyLong_Check(v)) {
2119 +PyErr_SetString(PyExc_TypeError, "argument must be an integer");
2120 +return NULL;
2121 + }
2122 +
2114 2123 mpd_maxcontext(&maxctx);
2115 -dec = dec_from_long(type, pylong, &maxctx, &status);
2124 +dec = dec_from_long(type, v, &maxctx, &status);
2116 2125 if (dec == NULL) {
2117 2126 return NULL;
2118 2127 }