From xemacs-m  Mon Jul 28 18:09:39 1997
Received: from turnbull.sk.tsukuba.ac.jp (root@turnbull.sk.tsukuba.ac.jp [130.158.99.4])
	by xemacs.org (8.8.5/8.8.5) with SMTP id SAA24327
	for <xemacs-beta@xemacs.org>; Mon, 28 Jul 1997 18:09:38 -0500 (CDT)
Received: from turnbull.sk.tsukuba.ac.jp(really [127.0.0.1]) by turnbull.sk.tsukuba.ac.jp
	via smtpd with esmtp
	id <m0wsnGE-00006qC@turnbull.sk.tsukuba.ac.jp>
	for <xemacs-beta@xemacs.org>; Mon, 28 Jul 1997 19:42:42 +0900 (JST)
	(Smail-3.2 1996-Jul-4 #3 built 1997-Jun-24)
Message-Id: <m0wsnGE-00006qC@turnbull.sk.tsukuba.ac.jp>
To: xemacs-beta@xemacs.org
Subject: 20.3-b14 input-method-xlib.c: null XIM status area crashes XEmacs
Date: Mon, 28 Jul 1997 19:42:41 +0900
From: "Stephen J. Turnbull" <turnbull@turnbull.sk.tsukuba.ac.jp>

(For Martin Buchholz, mostly, I guess.)

XEmacs 20.3-b14 (configure --with-mule --with-xim=xlib) crashes on the
first keystroke input when using 
  o kinput2 (version 2.0 fix 3 (1997/02/03) options: [Canna2] [DEBUG]),
  o running on Debian Linux 1.3, kernel 2.0.29,
  o using a TCP connection to a remote cannaserver,
  o locale services are provided by "liblocale.so".
Full configuration from Installation is at the end, followed by the
C source for liblocale.so (originally a patch for Netscape
internationalization).  XEmacs's version string is

XEmacs 20.3 "Vienna" [Lucid] (i586-pc-linux, Mule) of Tue Jul 29 1997 on tanko

Typical sequence to reproduce the crash:

tanko# kinput2 -cannaserver turnbull -trace
tanko# export XMODIFIERS='@im=kinput2'
tanko# export LD_PRELOAD=/usr/local/lib/liblocale.so
tanko# cd /usr/local/xemacs-20.3-b14/src; xemacs &

and type Ctrl-X in the XEmacs window (intending to continue with
Ctrl-F filename, but I don't get that far).

This is due to a NULL xic being passed to XmbLookupString, when
XCreateIC fails because kinput2 receives null StatusArea and
PreeditArea rectangles from XEmacs.  The following patch fixes the
problem in this case, but cannot be relied upon if XCreateIC (or
XOpenIM, possibly) fails for some other reason.  This patch makes
kinput2 usable in most circumstances.  However, any failure of
XCreateIC will still result in a crash on keyboard input.

--- src/DIST/input-method-xlib.c	Tue Jul 29 01:57:53 1997
+++ src/input-method-xlib.c	Tue Jul 29 02:04:25 1997
@@ -154,7 +154,12 @@
   XIC xic;
   Widget w = FRAME_X_TEXT_WIDGET (f);
   Window win = XtWindow (w);
+#ifdef KINPUT2_IC_ERROR_FIXED
   XRectangle p_area = {0,0,0,0}, s_area={0,0,0,0};
+#else
+  /* kinput2, at least, insists on these being non-null */
+  XRectangle p_area = {0,0,1,1}, s_area={0,0,1,1};
+#endif
   XPoint spot = {0,0};
   XIMStyle style;
   XVaNestedList p_list, s_list;

ChangeLog entry:

Mon Jul 28 19:28:08 1997  Stephen J Turnbull  <turnbull@sk.tsukuba.ac.jp>

	* input-method-xlib.c (XIM_init_frame): partially fix XIM crash
	for IMs that insist on non-null Status/PreeditArea (eg, kinput2)

Recompilation with the above patch and with the same configuration (in
fact, I just did 'make' again without doing any cleaning or
dependencies) produced an XEmacs that happily uses "kinput2 -xim".

Kinput2 does not accept null status and preedit areas, and so returns
a "protocol error" when XEmacs provides null status and preedit areas
in the XOpenIC call.  XEmacs then proceeds to accept the null IC
returned by Xlib.  This seems to be handled in input-method-xlib.c.
It seems that the current version of input-method-xlib.c does check
for a bad IC pretty much everywhere.  The problem is when it passes it
to functions like XmbLookupString.  (XmbLookupString occurs only in
event-Xt.c---I think, but I don't know whether there may be other
problem functions.)  This results in an immediate crash on any
keystroke input:

#0  0x40238159 in __kill ()
#1  0x808327d in fatal_error_signal (sig=11) at emacs.c:211
#2  0xbfffe7d8 in ?? ()
#3  0x816af08 in x_to_emacs_keysym (event=0xbfffed98, simple_p=0)
    at event-Xt.c:644
#4  0x816bafd in x_event_to_emacs_event (x_event=0xbfffed98, 
    emacs_event=0x853fde8) at event-Xt.c:874
#5  0x816d576 in emacs_Xt_event_handler (wid=0x843d200, closure=0x0, 
    event=0xbfffed98, continue_to_dispatch=0xbfffecd7 "\001\235,At(B\017@")
    at event-Xt.c:2165
#6  0x4008206f in XtDispatchEventToWidget ()
#7  0x40082a1d in _XtOnGrabList ()
#8  0x40082c53 in XtDispatchEvent ()
#9  0x4008bc83 in XtAppProcessEvent ()

(There's about 30 lines more to the backtrace, but I don't think it's
relevant.)

The patch is controlled by a (by default undefined) macro,
KINPUT2_IC_ERROR_FIXED.  This problem is possibly a bug in kinput2
(the status and preedit areas are not requested by XEmacs; on the
other hand, kinput2's debug mode claims that giving null rectangles
there is a protocol error).  Until this issue is resolved this
probably ought to be conditionally compiled.  Since kinput2 doesn't
work at all without it, it should be in by default.

What really seems to need to be done is to make sure that the proper
*LookupString function gets installed.  This is currently done in a
macro in event-Xt.c (around line 639).  I don't understand the code
well enough to know how to fix that.  One possibility would be to use
a function pointer (with the function installed in input-method-*.c)
rather than a macro there.  This might also make it possible to handle
the case where the conversion server goes up and down on you (or one
server is up but another is down).  The alternative is to check for a
properly opened input context every time you use *xic, which seems
error-prone.

I've tried to subscribe to xemacs-beta and to look at www.xemacs.org,
but we currently seem to be unable to reach overseas sites from my
domain.  Since XEmacs is now working, I don't know when I'll get back
to this, and thought I would send the report now.  I've tried to
follow the format given in Info: XEmacs/Bugs, and apologize if
anything is missing or improperly done.

uname -a: Linux tanko 2.0.29 #3 Fri Jul 25 02:43:07 JST 1997 i586 unknown

./configure  '--with-sound=native' '--with-mule' '--with-xim'

Configured for `i586-pc-linux'.

  Where should the build process find the source code?    /usr/local/xemacs-20.3-b14
  What installation prefix should install use?		  /usr/local
  What operating system and machine description files should XEmacs use?
        `s/linux.h' and `m/intel386.h'
  What compiler should XEmacs be built with?              gcc -g -O2 -Wall -Wno-switch -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2
  Should XEmacs use the GNU version of malloc?            yes
  Should XEmacs use the relocating allocator for buffers? yes
  What window system should XEmacs use?                   x11
  Where do we find X Windows header files?                /usr/X11R6/include
  Where do we find X Windows libraries?                   /usr/X11R6/lib
  Compiling in support for XAUTH.
  Compiling in support for GIF image conversion.
  Compiling in support for XPM images.
  Compiling in support for X-Face message headers.
  Compiling in support for JPEG image conversion.
  Compiling in support for PNG image conversion.
  Compiling in native sound support.
  Compiling in support for Berkeley DB.
  Compiling in support for GNU DBM.
  Compiling in support for ncurses.
  Compiling in Mule (multi-lingual) support.
  Compiling in XIM (X11R5+ I18N input method) support.
    Using raw Xlib to provide XIM support.
  Using Lucid menubars.
  Using Lucid scrollbars.
  Using Athena dialog boxes.
  movemail will use "dot-locking" for locking mail spool files.
  WARNING: ---------------------------------------------------------
  WARNING: Compiling in support for runtime error checking.
  WARNING: XEmacs will run noticeably more slowly as a result.
  WARNING: Error checking is on by default for XEmacs beta releases.
  WARNING: ---------------------------------------------------------

locale.c (source for liblocale.so):

/* locale.c for Linux
   gcc -fPIC -shared -Wl,-rpath=/usr/X11R6/lib,-soname,liblocale.so \
   -o liblocale.so locale.c -L/usr/X11R6/lib -lX11 -lXt

   LD_PRELOAD=/lib/liblocale.so netscape */

#include <locale.h>
#include <X11/Intrinsic.h>

char *_Xsetlocale (int, const char *);

#undef setlocale
char *setlocale (int c, const char *l)
{
  if ((c == LC_ALL || c == LC_CTYPE) && l != NULL
      && !(*l == 'C' && l[1] == '\0')) {
	XtSetLanguageProc(NULL, NULL, NULL);
	l = NULL;
  }
  return _Xsetlocale(((c == LC_ALL || c == LC_CTYPE) ? 2 : 3), l);
}


-- 
                            Stephen J. Turnbull
Institute of Policy and Planning Sciences                    Yaseppochi-Gumi
University of Tsukuba                      http://turnbull.sk.tsukuba.ac.jp/
Tel: +81 (298) 53-5091;  Fax: 55-3849              turnbull@sk.tsukuba.ac.jp

