]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before dereferencing...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 25 Aug 2018 19:17:13 +0000 (15:17 -0400)
committerGitHub <noreply@github.com>
Sat, 25 Aug 2018 19:17:13 +0000 (15:17 -0400)
Reported by Svace static analyzer.
(cherry picked from commit 5f79b50763d687aeeed8edcb4efcc7ac9f8fa186)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
Objects/typeobject.c

index bbc383a695b8d0eb29954d8469e65aaae595f220..3468c64060dfe2f12303b68a36ecce26bdc7ad69 100644 (file)
@@ -2748,6 +2748,15 @@ 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)
@@ -2755,8 +2764,6 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
     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;
@@ -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) {