From xemacs-m  Sat Mar 15 02:24:10 1997
Received: from gol1.gol.com (gol1.gol.com [202.243.48.4])
	by xemacs.org (8.8.5/8.8.5) with ESMTP id CAA15766
	for <xemacs-beta@xemacs.org>; Sat, 15 Mar 1997 02:24:09 -0600 (CST)
Received: from Pentagana.sonic.jp (jhod@pm-5-182.tokyo.gol.com [202.243.51.182])
	by gol1.gol.com (8.8.5/8.8.5) with ESMTP id RAA22928
	for <xemacs-beta@xemacs.org>; Sat, 15 Mar 1997 17:24:05 +0900 (JST)
Received: (from jhod@localhost) by Pentagana.sonic.jp (8.7.2/8.7.2) id RAA25465; Sat, 15 Mar 1997 17:21:44 +0900
Date: Sat, 15 Mar 1997 17:21:44 +0900
Message-Id: <199703150821.RAA25465@Pentagana.sonic.jp>
From: P E Jareth Hein <jhod@po.iijnet.or.jp>
To: XEmacs Beta Mailing List <xemacs-beta@xemacs.org>
Subject: XLib patches
Mime-Version: 1.0 (generated by tm-edit 7.105)
Content-Type: multipart/mixed;
 boundary="Multipart_Sat_Mar_15_17:21:44_1997-1"
Content-Transfer-Encoding: 7bit

--Multipart_Sat_Mar_15_17:21:44_1997-1
Content-Type: text/plain; charset=US-ASCII

Here are a few patches to be applied inside the Xlib directory of
X11R6.3 that correct some boneheaded malloc problems, and some logic
problems with string conversions. The conversion stuff needs quite a
bit of re-work, and I'm talking with X/Open about this. In other
words, that stuff is more than a little unstable, so if it breaks for
you, just back out those patches, and leave the imDef stuff in (and
let me know what broke!)

--Jareth

--Multipart_Sat_Mar_15_17:21:44_1997-1
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="xlibdiff"
Content-Transfer-Encoding: 7bit

--- imDefIc.c.old	Mon Feb 17 16:03:43 1997
+++ imDefIc.c	Mon Feb 17 16:08:39 1997
@@ -296,7 +296,7 @@
     INT16		 len;
     CARD32		 reply32[BUFSIZE/4];
     char		*reply = (char *)reply32;
-    XPointer		 preply = NULL;
+    XPointer		 preply;
     int			 buf_size;
     int			 ret_code;
     char		*makeid_name;
@@ -399,6 +399,9 @@
 	}
 	data = &buf_s[4];
 	data_len = buf_s[2];
+
+	if (reply != preply)
+	    Xfree(preply);
     }
     else {
 	return arg->name;
@@ -407,9 +410,6 @@
     decode_name = _XimDecodeICATTRIBUTE(ic, ic->private.proto.ic_resources,
 			ic->private.proto.ic_num_resources, data, data_len,
 			arg, XIM_GETICVALUES);
-    if (reply != preply)
-	Xfree(preply);
-
     if (decode_name)
 	return decode_name;
     else
@@ -680,7 +680,7 @@
     XIMArg		*arg_ret;
     CARD32		 reply32[BUFSIZE/4];
     char		*reply = (char *)reply32;
-    XPointer		 preply = NULL;
+    XPointer		 preply;
     int			 ret_code;
     BITMASK32		 flag = 0L;
     char		*name;
@@ -1172,7 +1172,7 @@
     if (*((CARD8 *)preply) == XIM_ERROR) {
 	_XimProcError(im, 0, (XPointer)&buf_s[3]);
     	if(reply != preply)
-    	    free(preply);
+    	    Xfree(preply);
 	return NULL;
     }
 
@@ -1293,13 +1293,15 @@
     if (*((CARD8 *)preply) == XIM_ERROR) {
 	_XimProcError(im, 0, (XPointer)&buf_s[3]);
     	if(reply != preply)
-    	    free(preply);
+    	    Xfree(preply);
 	return NULL;
     }
 
     commit = _XimCommitedWcString(im, ic, (XPointer)&buf_s[2]);
 
-    Xfree(preply);
+    if(reply != preply)
+        Xfree(preply);
+    
     return commit;
 }
 
@@ -1574,7 +1576,8 @@
 
     ic->private.proto.icid = buf_s[1];		/* icid */
     if(reply != preply)
-	    Xfree(preply);
+	Xfree(preply);
+    
     MARK_IC_CONNECTED(ic);
     return (XIC)ic;
 
--- imDefIm.c.old	Mon Feb 17 15:55:28 1997
+++ imDefIm.c	Mon Feb 17 15:59:09 1997
@@ -1515,13 +1515,13 @@
 	}
 	data = &buf_s[2];
 	data_len = buf_s[1];
+	
+	if (reply != preply)
+	    Xfree(preply);
     }
     decode_name = _XimDecodeIMATTRIBUTE(im, im->core.im_resources,
 			im->core.im_num_resources, data, data_len,
 			arg, XIM_GETIMVALUES);
-    if (reply != preply)
-	Xfree(preply);
-
     if (decode_name)
 	return decode_name;
     else
--- imDefLkup.c.old	Thu Dec  7 01:24:13 1995
+++ imDefLkup.c	Wed Feb 26 07:37:43 1997
@@ -1090,8 +1090,16 @@
 
 	ret = im->methods->ctstombs((XIM)im, info->string,
 			 	info->string_len, buffer, bytes, state);
+#if 0
+	/* 97/2/26 jhod@po.iijnet.or.jp
+	 * this violates the stated policy of returning the number of chars needed
+	 */
 	if (*state == XBufferOverflow)
 	    return 0;
+#else
+	if (*state == XBufferOverflow)
+	    return ret;
+#endif
 	if (keysym && (info->keysym && *(info->keysym))) {
 	    *keysym = *(info->keysym);
 	    if (*state == XLookupChars)
@@ -1153,8 +1161,16 @@
 
 	ret = im->methods->ctstowcs((XIM)im, info->string,
 			 	info->string_len, buffer, bytes, state);
+#if 0
+	/* 97/2/26 jhod@po.iijnet.or.jp
+	 * this violates the stated policy of returning the number of chars needed
+	 */
 	if (*state == XBufferOverflow)
 	    return 0;
+#else
+	if (*state == XBufferOverflow)
+	    return ret;
+#endif
 	if (keysym && (info->keysym && *(info->keysym))) {
 	    *keysym = *(info->keysym);
 	    if (*state == XLookupChars)
--- lcGenConv.c.old	Fri Dec  6 00:40:30 1996
+++ lcGenConv.c	Wed Feb 26 07:40:13 1997
@@ -778,8 +778,14 @@
     ParseInfo *mb_parse_list      = XLC_GENERIC(lcd, mb_parse_list);
 
 
+#if 0
+    /* 97/2/27 jhod@po.iijnet.or.jp
+     * Why?
+     */
+
     if (*from_left > *to_left)
         *from_left = *to_left;
+#endif
 
     while (*from_left && *to_left) {
 
@@ -856,7 +862,12 @@
     }
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = (XPointer)outbufptr;
 
     return unconv_num;
@@ -939,10 +950,14 @@
     char *default_string = XLC_PUBLIC(lcd, default_string);
     int defstr_len = strlen(default_string);
 
-
+#if 0
+    /* 97/2/27 jhod@po.iijnet.or.jp
+     * Why?
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
         wc = *inbufptr++;
@@ -1000,7 +1015,12 @@
     } /* end of while */
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = outbufptr;
 
     return unconv_num;
@@ -1079,10 +1099,14 @@
     XPointer outbufptr = *to;
     int from_size = *from_left;
 
-
+#if 0
+    /* 97/2/27 jhod@po.iijnet.or.jp
+     * Why?
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
         wc = *inbufptr++;
@@ -1185,7 +1209,12 @@
     } /* end of while */
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = outbufptr;
 
     return unconv_num;
@@ -1262,9 +1291,15 @@
         return( 0 );
     }
 
+#if 0
+   /* 97/2/26 jhod@po.iijnet.or.jp
+    * What the heck is this? It seems to guarantee that
+    * any string with escape codes will get truncated
+    */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
 	ch = *inbufptr++;
@@ -1408,7 +1443,12 @@
     }
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = (XPointer)outbufptr;
 
     return unconv_num;
@@ -1519,9 +1559,14 @@
     ParseInfo *mb_parse_list      = XLC_GENERIC(lcd, mb_parse_list);
 
 
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * This is logically wrong! Truncation city
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
 	ch = *inbufptr++;
@@ -1600,7 +1645,12 @@
     }
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = (XPointer)outbufptr;
 
     return unconv_num;
@@ -1642,9 +1692,14 @@
     ParseInfo *mb_parse_list      = XLC_GENERIC(lcd, mb_parse_list);
 
 
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * Again, truncation is not an option...
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
 	ch = *inbufptr++;
@@ -1824,9 +1879,14 @@
     int defstr_len = strlen(default_string);
 
 
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * Truncation is evil!
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
         wc = *inbufptr++;
@@ -1889,7 +1949,12 @@
     } /* end of while */
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = outbufptr;
 
     return unconv_num;
@@ -1960,10 +2025,14 @@
     int from_size = *from_left;
 
 
-
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * ??????????????????????
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     if (*from_left && *to_left) {
 
         wc = *inbufptr++;
@@ -2239,9 +2308,14 @@
     XPointer outbufptr = *to;
     int from_size = *from_left;
 
+#if 0
+    /* 97/2/27 jhod@po.iijnet.or.jp
+     * Why?
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+    
     while (*from_left && *to_left) {
 
         ch = *inbufptr++;
@@ -2298,7 +2372,12 @@
     } /* end of while */
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = outbufptr;
 
     return unconv_num;
@@ -2328,9 +2407,14 @@
     wchar_t *outbufptr = (wchar_t *)*to;
     int from_size = *from_left;
 
+#if 0
+    /* 97/2/27 jhod@po.iijnet.or.jp
+     * Why?
+     */
     if (*from_left > *to_left)
         *from_left = *to_left;
-
+#endif
+  
     while (*from_left && *to_left) {
 
         ch = *inbufptr++;
@@ -2365,7 +2449,12 @@
     } /* end of while */
 
     *from = *from + from_size;
+#if 0
+    /* 97/2/26 jhod@po.iijnet.or.jp
+     * this prevents detection of overflows
+     */
     *from_left = 0;
+#endif
     *to = (XPointer)outbufptr;
 
     return unconv_num;

--Multipart_Sat_Mar_15_17:21:44_1997-1--

