Skip to content

Commit

Permalink
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before d…
Browse files Browse the repository at this point in the history
…ereferencing it. (GH-8930)

Reported by Svace static analyzer.
(cherry picked from commit 5f79b50)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
  • Loading branch information
miss-islington and izbyshev committed Aug 25, 2018
1 parent 0433f8e commit 323a91b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Objects/typeobject.c
Expand Up @@ -2748,15 +2748,22 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
char *res_start = (char*)res;
PyType_Slot *slot;

if (res == NULL)
return NULL;

if (spec->name == NULL) {
PyErr_SetString(PyExc_SystemError,
"Type spec does not define the name field.");
goto fail;
}

/* Set the type name and qualname */
s = strrchr(spec->name, '.');
if (s == NULL)
s = (char*)spec->name;
else
s++;

if (res == NULL)
return NULL;
type = &res->ht_type;
/* The flags must be initialized early, before the GC traverses us */
type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
Expand All @@ -2766,8 +2773,6 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
res->ht_qualname = res->ht_name;
Py_INCREF(res->ht_qualname);
type->tp_name = spec->name;
if (!type->tp_name)
goto fail;

/* Adjust for empty tuple bases */
if (!bases) {
Expand Down

0 comments on commit 323a91b

Please sign in to comment.