Skip to content

Commit 5f79b50

Browse files
Alexey Izbyshevbenjaminp
authored andcommitted
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before dereferencing it. (GH-8930)
Reported by Svace static analyzer.
1 parent 44838be commit 5f79b50

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Objects/typeobject.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,15 +2847,22 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
28472847
char *res_start = (char*)res;
28482848
PyType_Slot *slot;
28492849

2850+
if (res == NULL)
2851+
return NULL;
2852+
2853+
if (spec->name == NULL) {
2854+
PyErr_SetString(PyExc_SystemError,
2855+
"Type spec does not define the name field.");
2856+
goto fail;
2857+
}
2858+
28502859
/* Set the type name and qualname */
28512860
s = strrchr(spec->name, '.');
28522861
if (s == NULL)
28532862
s = (char*)spec->name;
28542863
else
28552864
s++;
28562865

2857-
if (res == NULL)
2858-
return NULL;
28592866
type = &res->ht_type;
28602867
/* The flags must be initialized early, before the GC traverses us */
28612868
type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
@@ -2865,8 +2872,6 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
28652872
res->ht_qualname = res->ht_name;
28662873
Py_INCREF(res->ht_qualname);
28672874
type->tp_name = spec->name;
2868-
if (!type->tp_name)
2869-
goto fail;
28702875

28712876
/* Adjust for empty tuple bases */
28722877
if (!bases) {

0 commit comments

Comments
 (0)