[PATCH:libX11] Plug additional _XDefaultOpenIM memory leaks in error paths

Alan Coopersmith alan.coopersmith at oracle.com
Fri Dec 4 00:25:05 PST 2015


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.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 src/xlibi18n/XDefaultIMIF.c | 58 +++++++++++++++++++--------------------------
 1 file changed, 25 insertions(+), 33 deletions(-)

diff --git a/src/xlibi18n/XDefaultIMIF.c b/src/xlibi18n/XDefaultIMIF.c
index e691e03..4e75fa0 100644
--- a/src/xlibi18n/XDefaultIMIF.c
+++ b/src/xlibi18n/XDefaultIMIF.c
@@ -168,30 +168,25 @@ _XDefaultOpenIM(
     char                *res_class)
 {
     StaticXIM im;
-    XIMStaticXIMRec *local_impart;
-    XlcConv ctom_conv, ctow_conv;
     int i;
     char *mod;
     char buf[BUFSIZ];
 
-    if (!(ctom_conv = _XlcOpenConverter(lcd,
-			XlcNCompoundText, lcd, XlcNMultiByte))) {
-	return((XIM)NULL);
-    }
+    if ((im = Xcalloc(1, sizeof(StaticXIMRec))) == NULL)
+        return NULL;
 
-    if (!(ctow_conv = _XlcOpenConverter(lcd,
-			XlcNCompoundText, lcd, XlcNWideChar))) {
-	return((XIM)NULL);
-    }
+    if ((im->private = Xcalloc(1, sizeof(XIMStaticXIMRec))) == NULL)
+        goto Error;
 
-    if ((im = Xcalloc(1, sizeof(StaticXIMRec))) == (StaticXIM)NULL) {
-	return((XIM)NULL);
-    }
-    if ((local_impart = Xcalloc(1, sizeof(XIMStaticXIMRec)))
-	== (XIMStaticXIMRec *)NULL) {
-	Xfree(im);
-	return((XIM)NULL);
-    }
+    if ((im->private->ctom_conv = _XlcOpenConverter(lcd, XlcNCompoundText,
+                                                    lcd, XlcNMultiByte))
+        == NULL)
+        goto Error;
+
+    if ((im->private->ctow_conv = _XlcOpenConverter(lcd, XlcNCompoundText,
+                                                    lcd, XlcNWideChar))
+        == NULL)
+        goto Error;
 
     buf[0] = '\0';
     i = 0;
@@ -208,10 +203,9 @@ _XDefaultOpenIM(
     }
 #undef MODIFIER
     if ((im->core.im_name = Xmalloc(i+1)) == NULL)
-	goto Error2;
+	goto Error;
     strcpy(im->core.im_name, buf);
 
-    im->private = local_impart;
     im->methods        = (XIMMethods)&local_im_methods;
     im->core.lcd       = lcd;
     im->core.ic_chain  = (XIC)NULL;
@@ -220,9 +214,6 @@ _XDefaultOpenIM(
     im->core.res_name  = 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')){
 	im->core.res_name  = strdup(res_name);
     }
@@ -231,12 +222,10 @@ _XDefaultOpenIM(
     }
 
     return (XIM)im;
-Error2 :
-    Xfree(im->core.im_name);
-    Xfree(local_impart);
+
+  Error:
+    _CloseIM((XIM)im);
     Xfree(im);
-    _XlcCloseConverter(ctom_conv);
-    _XlcCloseConverter(ctow_conv);
     return(NULL);
 }
 
@@ -244,13 +233,16 @@ static Status
 _CloseIM(XIM 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->core.im_name);
-    if (im->core.res_name) XFree(im->core.res_name);
-    if (im->core.res_class) XFree(im->core.res_class);
-    return 1; /*bugID 4163122*/
+    XFree(im->core.res_name);
+    XFree(im->core.res_class);
+    return 1;
 }
 
 static char *
-- 
2.6.1



More information about the xorg-devel mailing list