Skip to content

Commit be5e0dc

Browse files
authored
gh-142595: add type check for namedtuple call during decimal initialization (GH-142608)
1 parent af2b3e9 commit be5e0dc

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added type check during initialization of the :mod:`decimal` module to
2+
prevent a crash in case of broken stdlib. Patch by Sergey B Kirpichev.

Modules/_decimal/_decimal.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7753,10 +7753,14 @@ _decimal_exec(PyObject *m)
77537753

77547754
/* DecimalTuple */
77557755
ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
7756-
ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
7757-
"namedtuple", "(ss)", "DecimalTuple",
7758-
"sign digits exponent"));
7759-
7756+
obj = PyObject_CallMethod(collections, "namedtuple", "(ss)", "DecimalTuple",
7757+
"sign digits exponent");
7758+
if (!PyType_Check(obj)) {
7759+
PyErr_SetString(PyExc_TypeError,
7760+
"type is expected from namedtuple call");
7761+
goto error;
7762+
}
7763+
ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)obj);
77607764
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
77617765
CHECK_INT(PyDict_SetItemString(state->DecimalTuple->tp_dict, "__module__", obj));
77627766
Py_CLEAR(obj);

0 commit comments

Comments
 (0)