Commit 9dec6c18 authored by Alan Coopersmith's avatar Alan Coopersmith Committed by Ulrich Sibiller

Bug 93183: _XDefaultOpenIM memory leaks in out-of-memory error paths

Rework code to store allocations directly into XIM struct instead of temporary local variables, so we can use _XCloseIM to unwind instead of duplicating it, and consistently jump to error handler on failure, instead of sometimes leaking and sometimes freeing. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=93183Signed-off-by: 's avatarAlan Coopersmith <alan.coopersmith@oracle.com> Backported-to-NX-by: 's avatarUlrich Sibiller <uli42@gmx.de>
parent 8d6d95d7
...@@ -168,30 +168,25 @@ _XDefaultOpenIM( ...@@ -168,30 +168,25 @@ _XDefaultOpenIM(
char *res_class) char *res_class)
{ {
StaticXIM im; StaticXIM im;
XIMStaticXIMRec *local_impart;
XlcConv ctom_conv, ctow_conv;
int i; int i;
char *mod; char *mod;
char buf[BUFSIZ]; char buf[BUFSIZ];
if (!(ctom_conv = _XlcOpenConverter(lcd, if ((im = Xcalloc(1, sizeof(StaticXIMRec))) == NULL)
XlcNCompoundText, lcd, XlcNMultiByte))) { return NULL;
return((XIM)NULL);
}
if (!(ctow_conv = _XlcOpenConverter(lcd, if ((im->private = Xcalloc(1, sizeof(XIMStaticXIMRec))) == NULL)
XlcNCompoundText, lcd, XlcNWideChar))) { goto Error;
return((XIM)NULL);
}
if ((im = Xcalloc(1, sizeof(StaticXIMRec))) == (StaticXIM)NULL) { if ((im->private->ctom_conv = _XlcOpenConverter(lcd, XlcNCompoundText,
return((XIM)NULL); lcd, XlcNMultiByte))
} == NULL)
if ((local_impart = Xcalloc(1, sizeof(XIMStaticXIMRec))) goto Error;
== (XIMStaticXIMRec *)NULL) {
Xfree(im); if ((im->private->ctow_conv = _XlcOpenConverter(lcd, XlcNCompoundText,
return((XIM)NULL); lcd, XlcNWideChar))
} == NULL)
goto Error;
buf[0] = '\0'; buf[0] = '\0';
i = 0; i = 0;
...@@ -208,10 +203,9 @@ _XDefaultOpenIM( ...@@ -208,10 +203,9 @@ _XDefaultOpenIM(
} }
#undef MODIFIER #undef MODIFIER
if ((im->core.im_name = Xmalloc(i+1)) == NULL) if ((im->core.im_name = Xmalloc(i+1)) == NULL)
goto Error2; goto Error;
strcpy(im->core.im_name, buf); strcpy(im->core.im_name, buf);
im->private = local_impart;
im->methods = (XIMMethods)&local_im_methods; im->methods = (XIMMethods)&local_im_methods;
im->core.lcd = lcd; im->core.lcd = lcd;
im->core.ic_chain = (XIC)NULL; im->core.ic_chain = (XIC)NULL;
...@@ -220,9 +214,6 @@ _XDefaultOpenIM( ...@@ -220,9 +214,6 @@ _XDefaultOpenIM(
im->core.res_name = NULL; im->core.res_name = NULL;
im->core.res_class = NULL; im->core.res_class = NULL;
local_impart->ctom_conv = ctom_conv;
local_impart->ctow_conv = ctow_conv;
if ((res_name != NULL) && (*res_name != '\0')){ if ((res_name != NULL) && (*res_name != '\0')){
im->core.res_name = strdup(res_name); im->core.res_name = strdup(res_name);
} }
...@@ -231,12 +222,10 @@ _XDefaultOpenIM( ...@@ -231,12 +222,10 @@ _XDefaultOpenIM(
} }
return (XIM)im; return (XIM)im;
Error2 :
Xfree(im->private); Error:
Xfree(im->core.im_name); _CloseIM((XIM)im);
Xfree(im); Xfree(im);
_XlcCloseConverter(ctom_conv);
_XlcCloseConverter(ctow_conv);
return(NULL); return(NULL);
} }
...@@ -244,13 +233,16 @@ static Status ...@@ -244,13 +233,16 @@ static Status
_CloseIM(XIM xim) _CloseIM(XIM xim)
{ {
StaticXIM im = (StaticXIM)xim; StaticXIM im = (StaticXIM)xim;
_XlcCloseConverter(im->private->ctom_conv);
_XlcCloseConverter(im->private->ctow_conv); if (im->private->ctom_conv != NULL)
_XlcCloseConverter(im->private->ctom_conv);
if (im->private->ctow_conv != NULL)
_XlcCloseConverter(im->private->ctow_conv);
XFree(im->private); XFree(im->private);
XFree(im->core.im_name); XFree(im->core.im_name);
if (im->core.res_name) XFree(im->core.res_name); XFree(im->core.res_name);
if (im->core.res_class) XFree(im->core.res_class); XFree(im->core.res_class);
return 1; /*bugID 4163122*/ return 1;
} }
static char * static char *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment