]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Backport r50743 | neal.norwitz]
authorAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 18:08:58 +0000 (18:08 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 18:08:58 +0000 (18:08 +0000)
Handle allocation failures gracefully.  Found with failmalloc.
Many (all?) of these could be backported.

Misc/NEWS
Objects/typeobject.c
Objects/unicodeobject.c
Python/import.c
Python/pystate.c
Python/sysmodule.c

index d6eee1c4fdd83f7184ed863a4548e9ebc9e00b36..97619ab8a6983239716e9d2de13dd807fe7bd3f6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,8 @@ Core and builtins
 - Overflow checking code in integer division ran afoul of new gcc
   optimizations.  Changed to be more standard-conforming.
 
+- Fix some potential crashes found with failmalloc.
+
 - Fix warnings reported by the Coverity and Klocwork static analysis tools.
 
 - Patch #1541585: fix buffer overrun when performing repr() on
index 14d9c8e3fe3e7be53421daf1dc0bd1b1fcde2d07..0bed8cc692adb273864cdf693252e78a82478011 100644 (file)
@@ -3239,6 +3239,8 @@ PyType_Ready(PyTypeObject *type)
        if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
                if (type->tp_doc != NULL) {
                        PyObject *doc = PyString_FromString(type->tp_doc);
+                       if (doc == NULL)
+                               goto error;
                        PyDict_SetItemString(type->tp_dict, "__doc__", doc);
                        Py_DECREF(doc);
                } else {
index ddfeee2795918792c7a9a49ccbb3e69017624f15..880b9ac612db981d340f0cd5a69630d27efc407e 100644 (file)
@@ -7350,6 +7350,9 @@ void _PyUnicode_Init(void)
     unicode_freelist = NULL;
     unicode_freelist_size = 0;
     unicode_empty = _PyUnicode_New(0);
+    if (!unicode_empty)
+       return;
+
     strcpy(unicode_default_encoding, "ascii");
     for (i = 0; i < 256; i++)
        unicode_latin1[i] = NULL;
index 8776d9a34aecd5de51309b90eb18d7818f2c085d..84d429a81ab80ed51d2faef8ab7d74d30fed9a9c 100644 (file)
@@ -104,6 +104,8 @@ _PyImport_Init(void)
        for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
                ++countS;
        filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
+       if (filetab == NULL)
+               Py_FatalError("Can't intiialize import file table.");
        memcpy(filetab, _PyImport_DynLoadFiletab,
               countD * sizeof(struct filedescr));
        memcpy(filetab + countD, _PyImport_StandardFiletab,
@@ -227,8 +229,11 @@ lock_import(void)
        long me = PyThread_get_thread_ident();
        if (me == -1)
                return; /* Too bad */
-       if (import_lock == NULL)
+       if (import_lock == NULL) {
                import_lock = PyThread_allocate_lock();
+               if (import_lock == NULL)
+                       return;  /* Nothing much we can do. */
+       }
        if (import_lock_thread == me) {
                import_lock_level++;
                return;
@@ -247,7 +252,7 @@ static int
 unlock_import(void)
 {
        long me = PyThread_get_thread_ident();
-       if (me == -1)
+       if (me == -1 || import_lock == NULL)
                return 0; /* Too bad */
        if (import_lock_thread != me)
                return -1;
index cdb7176696840f7c42064c1077ee12eb4b93d21c..372874fed87e220d84288c08442691d5d2ba6709 100644 (file)
@@ -66,6 +66,10 @@ PyInterpreterState_New(void)
 
        if (interp != NULL) {
                HEAD_INIT();
+#ifdef WITH_THREAD
+               if (head_mutex == NULL)
+                       Py_FatalError("Can't initialize threads for interpreter");
+#endif
                interp->modules = NULL;
                interp->sysdict = NULL;
                interp->builtins = NULL;
index d48194b3b888f66137c2231db8f645737db48377..08831786fe73962bd623cd09ffabcb0cd5f3bb54 100644 (file)
@@ -1017,41 +1017,38 @@ _PySys_Init(void)
 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
        s = "final";
 #endif
-       PyDict_SetItemString(sysdict, "version_info",
-                            v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
+
+#define SET_SYS_FROM_STRING(key, value)                        \
+       v = value;                                      \
+       if (v != NULL)                                  \
+               PyDict_SetItemString(sysdict, key, v);  \
+       Py_XDECREF(v)
+
+       SET_SYS_FROM_STRING("version_info",
+                           Py_BuildValue("iiisi", PY_MAJOR_VERSION,
                                               PY_MINOR_VERSION,
                                               PY_MICRO_VERSION, s,
                                               PY_RELEASE_SERIAL));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "api_version",
-                            v = PyInt_FromLong(PYTHON_API_VERSION));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "copyright",
-                            v = PyString_FromString(Py_GetCopyright()));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "platform",
-                            v = PyString_FromString(Py_GetPlatform()));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "executable",
-                            v = PyString_FromString(Py_GetProgramFullPath()));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "prefix",
-                            v = PyString_FromString(Py_GetPrefix()));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "exec_prefix",
-                  v = PyString_FromString(Py_GetExecPrefix()));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "maxint",
-                            v = PyInt_FromLong(PyInt_GetMax()));
-       Py_XDECREF(v);
+       SET_SYS_FROM_STRING("api_version",
+                           PyInt_FromLong(PYTHON_API_VERSION));
+       SET_SYS_FROM_STRING("copyright",
+                           PyString_FromString(Py_GetCopyright()));
+       SET_SYS_FROM_STRING("platform",
+                           PyString_FromString(Py_GetPlatform()));
+       SET_SYS_FROM_STRING("executable",
+                           PyString_FromString(Py_GetProgramFullPath()));
+       SET_SYS_FROM_STRING("prefix",
+                           PyString_FromString(Py_GetPrefix()));
+       SET_SYS_FROM_STRING("exec_prefix",
+                           PyString_FromString(Py_GetExecPrefix()));
+       SET_SYS_FROM_STRING("maxint",
+                           PyInt_FromLong(PyInt_GetMax()));
 #ifdef Py_USING_UNICODE
-       PyDict_SetItemString(sysdict, "maxunicode",
-                            v = PyInt_FromLong(PyUnicode_GetMax()));
-       Py_XDECREF(v);
+       SET_SYS_FROM_STRING("maxunicode",
+                           PyInt_FromLong(PyUnicode_GetMax()));
 #endif
-       PyDict_SetItemString(sysdict, "builtin_module_names",
-                  v = list_builtin_module_names());
-       Py_XDECREF(v);
+       SET_SYS_FROM_STRING("builtin_module_names",
+                           list_builtin_module_names());
        {
                /* Assumes that longs are at least 2 bytes long.
                   Should be safe! */
@@ -1063,18 +1060,16 @@ _PySys_Init(void)
                        value = "big";
                else
                        value = "little";
-               PyDict_SetItemString(sysdict, "byteorder",
-                                    v = PyString_FromString(value));
-               Py_XDECREF(v);
+               SET_SYS_FROM_STRING("byteorder",
+                                   PyString_FromString(value));
        }
 #ifdef MS_COREDLL
-       PyDict_SetItemString(sysdict, "dllhandle",
-                            v = PyLong_FromVoidPtr(PyWin_DLLhModule));
-       Py_XDECREF(v);
-       PyDict_SetItemString(sysdict, "winver",
-                            v = PyString_FromString(PyWin_DLLVersionString));
-       Py_XDECREF(v);
+       SET_SYS_FROM_STRING("dllhandle",
+                           PyLong_FromVoidPtr(PyWin_DLLhModule));
+       SET_SYS_FROM_STRING("winver",
+                           PyString_FromString(PyWin_DLLVersionString));
 #endif
+#undef SET_SYS_FROM_STRING
        if (warnoptions == NULL) {
                warnoptions = PyList_New(0);
        }