Skip to content

Commit 323a91b

Browse files
miss-islingtonAlexey Izbyshev
andauthored
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before dereferencing it. (GH-8930)
Reported by Svace static analyzer. (cherry picked from commit 5f79b50) Co-authored-by: Alexey Izbyshev <[email protected]>
1 parent 0433f8e commit 323a91b

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
@@ -2748,15 +2748,22 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
27482748
char *res_start = (char*)res;
27492749
PyType_Slot *slot;
27502750

2751+
if (res == NULL)
2752+
return NULL;
2753+
2754+
if (spec->name == NULL) {
2755+
PyErr_SetString(PyExc_SystemError,
2756+
"Type spec does not define the name field.");
2757+
goto fail;
2758+
}
2759+
27512760
/* Set the type name and qualname */
27522761
s = strrchr(spec->name, '.');
27532762
if (s == NULL)
27542763
s = (char*)spec->name;
27552764
else
27562765
s++;
27572766

2758-
if (res == NULL)
2759-
return NULL;
27602767
type = &res->ht_type;
27612768
/* The flags must be initialized early, before the GC traverses us */
27622769
type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
@@ -2766,8 +2773,6 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
27662773
res->ht_qualname = res->ht_name;
27672774
Py_INCREF(res->ht_qualname);
27682775
type->tp_name = spec->name;
2769-
if (!type->tp_name)
2770-
goto fail;
27712776

27722777
/* Adjust for empty tuple bases */
27732778
if (!bases) {

0 commit comments

Comments
 (0)