mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Handle allocation failures gracefully. Found with failmalloc.
Many (all?) of these could be backported.
This commit is contained in:
		
							parent
							
								
									1adbb50701
								
							
						
					
					
						commit
						e1fdb32ff2
					
				
					 6 changed files with 52 additions and 41 deletions
				
			
		|  | @ -12,6 +12,8 @@ What's New in Python 2.5 release candidate 1? | ||||||
| Core and builtins | Core and builtins | ||||||
| ----------------- | ----------------- | ||||||
| 
 | 
 | ||||||
|  | - Fix some potential crashes found with failmalloc. | ||||||
|  | 
 | ||||||
| - Fix warnings reported by Klocwork's static analysis tool. | - Fix warnings reported by Klocwork's static analysis tool. | ||||||
| 
 | 
 | ||||||
| - Bug #1512814, Fix incorrect lineno's when code within a function | - Bug #1512814, Fix incorrect lineno's when code within a function | ||||||
|  |  | ||||||
|  | @ -3260,6 +3260,8 @@ PyType_Ready(PyTypeObject *type) | ||||||
| 	if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { | 	if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { | ||||||
| 		if (type->tp_doc != NULL) { | 		if (type->tp_doc != NULL) { | ||||||
| 			PyObject *doc = PyString_FromString(type->tp_doc); | 			PyObject *doc = PyString_FromString(type->tp_doc); | ||||||
|  | 			if (doc == NULL) | ||||||
|  | 				goto error; | ||||||
| 			PyDict_SetItemString(type->tp_dict, "__doc__", doc); | 			PyDict_SetItemString(type->tp_dict, "__doc__", doc); | ||||||
| 			Py_DECREF(doc); | 			Py_DECREF(doc); | ||||||
| 		} else { | 		} else { | ||||||
|  |  | ||||||
|  | @ -7918,6 +7918,9 @@ void _PyUnicode_Init(void) | ||||||
|     unicode_freelist = NULL; |     unicode_freelist = NULL; | ||||||
|     unicode_freelist_size = 0; |     unicode_freelist_size = 0; | ||||||
|     unicode_empty = _PyUnicode_New(0); |     unicode_empty = _PyUnicode_New(0); | ||||||
|  |     if (!unicode_empty) | ||||||
|  | 	return; | ||||||
|  | 
 | ||||||
|     strcpy(unicode_default_encoding, "ascii"); |     strcpy(unicode_default_encoding, "ascii"); | ||||||
|     for (i = 0; i < 256; i++) |     for (i = 0; i < 256; i++) | ||||||
| 	unicode_latin1[i] = NULL; | 	unicode_latin1[i] = NULL; | ||||||
|  |  | ||||||
|  | @ -116,6 +116,8 @@ _PyImport_Init(void) | ||||||
| 	for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan) | 	for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan) | ||||||
| 		++countS; | 		++countS; | ||||||
| 	filetab = PyMem_NEW(struct filedescr, countD + countS + 1); | 	filetab = PyMem_NEW(struct filedescr, countD + countS + 1); | ||||||
|  | 	if (filetab == NULL) | ||||||
|  | 		Py_FatalError("Can't intiialize import file table."); | ||||||
| 	memcpy(filetab, _PyImport_DynLoadFiletab, | 	memcpy(filetab, _PyImport_DynLoadFiletab, | ||||||
| 	       countD * sizeof(struct filedescr)); | 	       countD * sizeof(struct filedescr)); | ||||||
| 	memcpy(filetab + countD, _PyImport_StandardFiletab, | 	memcpy(filetab + countD, _PyImport_StandardFiletab, | ||||||
|  | @ -239,8 +241,11 @@ lock_import(void) | ||||||
| 	long me = PyThread_get_thread_ident(); | 	long me = PyThread_get_thread_ident(); | ||||||
| 	if (me == -1) | 	if (me == -1) | ||||||
| 		return; /* Too bad */ | 		return; /* Too bad */ | ||||||
| 	if (import_lock == NULL) | 	if (import_lock == NULL) { | ||||||
| 		import_lock = PyThread_allocate_lock(); | 		import_lock = PyThread_allocate_lock(); | ||||||
|  | 		if (import_lock == NULL) | ||||||
|  | 			return;  /* Nothing much we can do. */ | ||||||
|  | 	} | ||||||
| 	if (import_lock_thread == me) { | 	if (import_lock_thread == me) { | ||||||
| 		import_lock_level++; | 		import_lock_level++; | ||||||
| 		return; | 		return; | ||||||
|  | @ -259,7 +264,7 @@ static int | ||||||
| unlock_import(void) | unlock_import(void) | ||||||
| { | { | ||||||
| 	long me = PyThread_get_thread_ident(); | 	long me = PyThread_get_thread_ident(); | ||||||
| 	if (me == -1) | 	if (me == -1 || import_lock == NULL) | ||||||
| 		return 0; /* Too bad */ | 		return 0; /* Too bad */ | ||||||
| 	if (import_lock_thread != me) | 	if (import_lock_thread != me) | ||||||
| 		return -1; | 		return -1; | ||||||
|  |  | ||||||
|  | @ -63,6 +63,10 @@ PyInterpreterState_New(void) | ||||||
| 
 | 
 | ||||||
| 	if (interp != NULL) { | 	if (interp != NULL) { | ||||||
| 		HEAD_INIT(); | 		HEAD_INIT(); | ||||||
|  | #ifdef WITH_THREAD | ||||||
|  | 		if (head_mutex == NULL) | ||||||
|  | 			Py_FatalError("Can't initialize threads for interpreter"); | ||||||
|  | #endif | ||||||
| 		interp->modules = NULL; | 		interp->modules = NULL; | ||||||
| 		interp->sysdict = NULL; | 		interp->sysdict = NULL; | ||||||
| 		interp->builtins = NULL; | 		interp->builtins = NULL; | ||||||
|  |  | ||||||
|  | @ -1137,41 +1137,38 @@ _PySys_Init(void) | ||||||
| #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL | ||||||
| 	s = "final"; | 	s = "final"; | ||||||
| #endif | #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_MINOR_VERSION, | ||||||
| 					       PY_MICRO_VERSION, s, | 					       PY_MICRO_VERSION, s, | ||||||
| 					       PY_RELEASE_SERIAL)); | 					       PY_RELEASE_SERIAL)); | ||||||
| 	Py_XDECREF(v); | 	SET_SYS_FROM_STRING("api_version", | ||||||
| 	PyDict_SetItemString(sysdict, "api_version", | 			    PyInt_FromLong(PYTHON_API_VERSION)); | ||||||
| 			     v = PyInt_FromLong(PYTHON_API_VERSION)); | 	SET_SYS_FROM_STRING("copyright", | ||||||
| 	Py_XDECREF(v); | 			    PyString_FromString(Py_GetCopyright())); | ||||||
| 	PyDict_SetItemString(sysdict, "copyright", | 	SET_SYS_FROM_STRING("platform", | ||||||
| 			     v = PyString_FromString(Py_GetCopyright())); | 			    PyString_FromString(Py_GetPlatform())); | ||||||
| 	Py_XDECREF(v); | 	SET_SYS_FROM_STRING("executable", | ||||||
| 	PyDict_SetItemString(sysdict, "platform", | 			    PyString_FromString(Py_GetProgramFullPath())); | ||||||
| 			     v = PyString_FromString(Py_GetPlatform())); | 	SET_SYS_FROM_STRING("prefix", | ||||||
| 	Py_XDECREF(v); | 			    PyString_FromString(Py_GetPrefix())); | ||||||
| 	PyDict_SetItemString(sysdict, "executable", | 	SET_SYS_FROM_STRING("exec_prefix", | ||||||
| 			     v = PyString_FromString(Py_GetProgramFullPath())); | 		   	    PyString_FromString(Py_GetExecPrefix())); | ||||||
| 	Py_XDECREF(v); | 	SET_SYS_FROM_STRING("maxint", | ||||||
| 	PyDict_SetItemString(sysdict, "prefix", | 			    PyInt_FromLong(PyInt_GetMax())); | ||||||
| 			     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); |  | ||||||
| #ifdef Py_USING_UNICODE | #ifdef Py_USING_UNICODE | ||||||
| 	PyDict_SetItemString(sysdict, "maxunicode", | 	SET_SYS_FROM_STRING("maxunicode", | ||||||
| 			     v = PyInt_FromLong(PyUnicode_GetMax())); | 			    PyInt_FromLong(PyUnicode_GetMax())); | ||||||
| 	Py_XDECREF(v); |  | ||||||
| #endif | #endif | ||||||
| 	PyDict_SetItemString(sysdict, "builtin_module_names", | 	SET_SYS_FROM_STRING("builtin_module_names", | ||||||
| 		   v = list_builtin_module_names()); | 			    list_builtin_module_names()); | ||||||
| 	Py_XDECREF(v); |  | ||||||
| 	{ | 	{ | ||||||
| 		/* Assumes that longs are at least 2 bytes long.
 | 		/* Assumes that longs are at least 2 bytes long.
 | ||||||
| 		   Should be safe! */ | 		   Should be safe! */ | ||||||
|  | @ -1183,18 +1180,16 @@ _PySys_Init(void) | ||||||
| 			value = "big"; | 			value = "big"; | ||||||
| 		else | 		else | ||||||
| 			value = "little"; | 			value = "little"; | ||||||
| 		PyDict_SetItemString(sysdict, "byteorder", | 		SET_SYS_FROM_STRING("byteorder", | ||||||
| 				     v = PyString_FromString(value)); | 				    PyString_FromString(value)); | ||||||
| 		Py_XDECREF(v); |  | ||||||
| 	} | 	} | ||||||
| #ifdef MS_COREDLL | #ifdef MS_COREDLL | ||||||
| 	PyDict_SetItemString(sysdict, "dllhandle", | 	SET_SYS_FROM_STRING("dllhandle", | ||||||
| 			     v = PyLong_FromVoidPtr(PyWin_DLLhModule)); | 			    PyLong_FromVoidPtr(PyWin_DLLhModule)); | ||||||
| 	Py_XDECREF(v); | 	SET_SYS_FROM_STRING("winver", | ||||||
| 	PyDict_SetItemString(sysdict, "winver", | 			    PyString_FromString(PyWin_DLLVersionString)); | ||||||
| 			     v = PyString_FromString(PyWin_DLLVersionString)); |  | ||||||
| 	Py_XDECREF(v); |  | ||||||
| #endif | #endif | ||||||
|  | #undef SET_SYS_FROM_STRING | ||||||
| 	if (warnoptions == NULL) { | 	if (warnoptions == NULL) { | ||||||
| 		warnoptions = PyList_New(0); | 		warnoptions = PyList_New(0); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Neal Norwitz
						Neal Norwitz