Issue #21951: Use attemptckalloc() instead of ckalloc() in Tkinter.

ckalloc() causes the Tcl interpreter to panic, attemptckalloc() returns NULL
if the memory allocation fails.
This commit is contained in:
Serhiy Storchaka 2014-09-11 10:40:44 +03:00
commit 979f80b8da
2 changed files with 29 additions and 10 deletions

View file

@ -132,6 +132,9 @@ Core and Builtins
Library Library
------- -------
- Issue #21951: Tkinter now most likely raises MemoryError instead of crash
if the memory allocation fails.
- Issue #22338: Fix a crash in the json module on memory allocation failure. - Issue #22338: Fix a crash in the json module on memory allocation failure.
- Issue #12410: imaplib.IMAP4 now supports the context management protocol. - Issue #12410: imaplib.IMAP4 now supports the context management protocol.

View file

@ -605,7 +605,7 @@ Tkapp_New(const char *screenName, const char *className,
Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
/* This is used to get the application class for Tk 4.1 and up */ /* This is used to get the application class for Tk 4.1 and up */
argv0 = (char*)ckalloc(strlen(className) + 1); argv0 = (char*)attemptckalloc(strlen(className) + 1);
if (!argv0) { if (!argv0) {
PyErr_NoMemory(); PyErr_NoMemory();
Py_DECREF(v); Py_DECREF(v);
@ -639,7 +639,7 @@ Tkapp_New(const char *screenName, const char *className,
if (use) if (use)
len += strlen(use) + sizeof "-use "; len += strlen(use) + sizeof "-use ";
args = (char*)ckalloc(len); args = (char*)attemptckalloc(len);
if (!args) { if (!args) {
PyErr_NoMemory(); PyErr_NoMemory();
Py_DECREF(v); Py_DECREF(v);
@ -912,7 +912,7 @@ AsObj(PyObject *value)
"list is too long"); "list is too long");
return NULL; return NULL;
} }
argv = (Tcl_Obj **) ckalloc(((size_t)size) * sizeof(Tcl_Obj *)); argv = (Tcl_Obj **) attemptckalloc(((size_t)size) * sizeof(Tcl_Obj *));
if(!argv) { if(!argv) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
@ -944,7 +944,7 @@ AsObj(PyObject *value)
if (kind == sizeof(Tcl_UniChar)) if (kind == sizeof(Tcl_UniChar))
return Tcl_NewUnicodeObj(inbuf, size); return Tcl_NewUnicodeObj(inbuf, size);
allocsize = ((size_t)size) * sizeof(Tcl_UniChar); allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
outbuf = (Tcl_UniChar*)ckalloc(allocsize); outbuf = (Tcl_UniChar*)attemptckalloc(allocsize);
/* Else overflow occurred, and we take the next exit */ /* Else overflow occurred, and we take the next exit */
if (!outbuf) { if (!outbuf) {
PyErr_NoMemory(); PyErr_NoMemory();
@ -1111,7 +1111,7 @@ Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc)
"list is too long"); "list is too long");
return NULL; return NULL;
} }
objv = (Tcl_Obj **)ckalloc(((size_t)objc) * sizeof(Tcl_Obj *)); objv = (Tcl_Obj **)attemptckalloc(((size_t)objc) * sizeof(Tcl_Obj *));
if (objv == NULL) { if (objv == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
objc = 0; objc = 0;
@ -1247,7 +1247,11 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
PyObject *exc_type, *exc_value, *exc_tb; PyObject *exc_type, *exc_value, *exc_tb;
if (!WaitForMainloop(self)) if (!WaitForMainloop(self))
return NULL; return NULL;
ev = (Tkapp_CallEvent*)ckalloc(sizeof(Tkapp_CallEvent)); ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent));
if (ev == NULL) {
PyErr_NoMemory();
return NULL;
}
ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc; ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc;
ev->self = self; ev->self = self;
ev->args = args; ev->args = args;
@ -1498,8 +1502,11 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
if (!WaitForMainloop(self)) if (!WaitForMainloop(self))
return NULL; return NULL;
ev = (VarEvent*)ckalloc(sizeof(VarEvent)); ev = (VarEvent*)attemptckalloc(sizeof(VarEvent));
if (ev == NULL) {
PyErr_NoMemory();
return NULL;
}
ev->self = selfptr; ev->self = selfptr;
ev->args = args; ev->args = args;
ev->flags = flags; ev->flags = flags;
@ -2098,7 +2105,12 @@ Tkapp_CreateCommand(PyObject *selfptr, PyObject *args)
#ifdef WITH_THREAD #ifdef WITH_THREAD
if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
Tcl_Condition cond = NULL; Tcl_Condition cond = NULL;
CommandEvent *ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
if (ev == NULL) {
PyErr_NoMemory();
PyMem_DEL(data);
return NULL;
}
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
ev->interp = self->interp; ev->interp = self->interp;
ev->create = 1; ev->create = 1;
@ -2144,7 +2156,11 @@ Tkapp_DeleteCommand(PyObject *selfptr, PyObject *args)
if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
Tcl_Condition cond = NULL; Tcl_Condition cond = NULL;
CommandEvent *ev; CommandEvent *ev;
ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
if (ev == NULL) {
PyErr_NoMemory();
return NULL;
}
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
ev->interp = self->interp; ev->interp = self->interp;
ev->create = 0; ev->create = 0;