Bug#860927: Tk applications segmentation fault when ibus-daemon IME is restarted

2017-04-21 Thread Jonathan Nieder
debdiff attached.  Still untested.  Sorry for the broken partial patch before.

Thanks,
Jonathan
diff -Nru tk8.6-8.6.6/debian/changelog tk8.6-8.6.6/debian/changelog
--- tk8.6-8.6.6/debian/changelog2016-07-27 20:22:45.0 -0700
+++ tk8.6-8.6.6/debian/changelog2017-04-21 17:11:11.0 -0700
@@ -1,3 +1,10 @@
+tk8.6 (8.6.6-2) local; urgency=medium
+
+  * Added patch from upstream to fix crash when X input methods are
+restarted (closes: #860927).
+
+ -- Jonathan Nieder   Fri, 21 Apr 2017 17:00:08 -0700
+
 tk8.6 (8.6.6-1) unstable; urgency=medium
 
   * New upstream release.
diff -Nru tk8.6-8.6.6/debian/patches/series tk8.6-8.6.6/debian/patches/series
--- tk8.6-8.6.6/debian/patches/series   2014-08-27 09:25:53.0 -0700
+++ tk8.6-8.6.6/debian/patches/series   2017-04-21 17:10:25.0 -0700
@@ -4,3 +4,4 @@
 non-linux.diff
 manpages.diff
 xft.diff
+ximgeneration.diff
diff -Nru tk8.6-8.6.6/debian/patches/ximgeneration.diff 
tk8.6-8.6.6/debian/patches/ximgeneration.diff
--- tk8.6-8.6.6/debian/patches/ximgeneration.diff   1969-12-31 
16:00:00.0 -0800
+++ tk8.6-8.6.6/debian/patches/ximgeneration.diff   2017-04-21 
17:13:54.0 -0700
@@ -0,0 +1,186 @@
+Patch by Brad Larson to handle an input method being restarted.
+http://core.tcl.tk/tk/tktview?name=7d967c68a0
+
+--- a/generic/tkEvent.c
 b/generic/tkEvent.c
+@@ -356,6 +356,7 @@ CreateXIC(
+   /* XCreateIC failed. */
+   return;
+ }
++winPtr->ximGeneration = dispPtr->ximGeneration;
+ 
+ /*
+  * Adjust the window's event mask if the IM requires it.
+@@ -1288,6 +1289,14 @@ Tk_HandleEvent(
+  */
+ 
+ #ifdef TK_USE_INPUT_METHODS
++/*
++ * If the XIC has been invalidated, it must be recreated.
++ */
++if (winPtr->dispPtr->ximGeneration != winPtr->ximGeneration) {
++  winPtr->flags &= ~TK_CHECKED_IC;
++  winPtr->inputContext = NULL;
++}
++
+ if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)) {
+   if (!(winPtr->flags & (TK_CHECKED_IC|TK_ALREADY_DEAD))) {
+   winPtr->flags |= TK_CHECKED_IC;
+@@ -1295,7 +1304,9 @@ Tk_HandleEvent(
+   CreateXIC(winPtr);
+   }
+   }
+-  if (eventPtr->type == FocusIn && winPtr->inputContext != NULL) {
++  if ((eventPtr->type == FocusIn) &&
++  (winPtr->dispPtr->inputMethod != NULL) &&
++  (winPtr->inputContext != NULL)) {
+   XSetICFocus(winPtr->inputContext);
+   }
+ }
+--- a/generic/tkInt.h
 b/generic/tkInt.h
+@@ -508,6 +508,9 @@ typedef struct TkDisplay {
+ 
+ int iconDataSize; /* Size of default iconphoto image data. */
+ unsigned char *iconDataPtr;   /* Default iconphoto image data, if 
set. */
++#ifdef TK_USE_INPUT_METHODS
++int ximGeneration;  /* Used to invalidate XIC */
++#endif /* TK_USE_INPUT_METHODS */
+ } TkDisplay;
+ 
+ /*
+@@ -809,6 +812,9 @@ typedef struct TkWindow {
+ int minReqWidth;  /* Minimum requested width. */
+ int minReqHeight; /* Minimum requested height. */
+ char *geometryMaster;
++#ifdef TK_USE_INPUT_METHODS
++int ximGeneration;  /* Used to invalidate XIC */
++#endif /* TK_USE_INPUT_METHODS */
+ } TkWindow;
+ 
+ /*
+--- a/generic/tkWindow.c
 b/generic/tkWindow.c
+@@ -355,6 +355,9 @@ CreateTopLevelWindow(
+  * Set the flags specified in the call.
+  */
+ 
++#ifdef TK_USE_INPUT_METHODS
++winPtr->ximGeneration = 0;
++#endif /*TK_USE_INPUT_METHODS*/
+ winPtr->flags |= flags;
+ 
+ /*
+@@ -650,6 +653,7 @@ TkAllocWindow(
+ winPtr->flags = 0;
+ winPtr->handlerList = NULL;
+ #ifdef TK_USE_INPUT_METHODS
++winPtr->ximGeneration = 0;
+ winPtr->inputContext = NULL;
+ #endif /* TK_USE_INPUT_METHODS */
+ winPtr->tagPtr = NULL;
+@@ -1442,10 +1446,11 @@ Tk_DestroyWindow(
+ UnlinkWindow(winPtr);
+ TkEventDeadWindow(winPtr);
+ #ifdef TK_USE_INPUT_METHODS
+-if (winPtr->inputContext != NULL) {
++if (winPtr->inputContext != NULL &&
++  winPtr->ximGeneration == winPtr->dispPtr->ximGeneration) {
+   XDestroyIC(winPtr->inputContext);
+-  winPtr->inputContext = NULL;
+ }
++winPtr->inputContext = NULL;
+ #endif /* TK_USE_INPUT_METHODS */
+ if (winPtr->tagPtr != NULL) {
+   TkFreeBindingTags(winPtr);
+--- a/unix/tkUnixEvent.c
 b/unix/tkUnixEvent.c
+@@ -38,6 +38,8 @@ static void  DisplayFileProc(ClientData clientData, 
int flags);
+ static void   DisplaySetupProc(ClientData clientData, int flags);
+ static void   TransferXEventsToTcl(Display *display);
+ #ifdef TK_USE_INPUT_METHODS
++static void   InstantiateIMCallback(Display *, XPointer client_data, 
XPointer call_data);
++static void   DestroyIMCallback(XIM im, XPointer client_data, 
XPointer call_data);
+ static void   OpenIM(TkDisplay *dispPtr);
+ #endif
+ 
+@@ -179,6 +181,8 @@ TkpOpenDisplay(
+ dispPtr->flags |= use_xkb;
+ #ifdef 

Bug#860927: Tk applications segmentation fault when ibus-daemon IME is restarted

2017-04-21 Thread Jonathan Nieder
Package: tk8.6
Version: 8.6.1-3ubuntu2
Severity: important
Tags: upstream patch
Forwarded: http://core.tcl.tk/tk/tktview?name=7d967c68a0

Hi,

Steve Paik (cc-ed) reported that gitk is crashing periodically.  This
patch, from upstream tk, should fix it.

Thoughts?

Please forgive the whitespace damage.  Copy/paste was the simplest way
to get this here.

Thanks,
Jonathan

commit 0175bc1be685a5ce4a92f7c153eb12e28c28cb1d (origin/bug_7d967c68)
Author: jan.nijtmans 
Date:   Thu Dec 15 16:07:06 2016 +

Proposed fix for [7d967c68a09e07e355358af40f36dd5dd84c7022|7d967c68]: Tk 
applications segmentation fault when ibus-daemon IME is restarted

diff --git a/generic/tkEvent.c b/generic/tkEvent.c
index 95aeda1dd..d058e7cd6 100644
--- a/generic/tkEvent.c
+++ b/generic/tkEvent.c
@@ -356,6 +356,7 @@ CreateXIC(
/* XCreateIC failed. */
return;
 }
+winPtr->ximGeneration = dispPtr->ximGeneration;
 
 /*
  * Adjust the window's event mask if the IM requires it.
@@ -1288,6 +1289,14 @@ Tk_HandleEvent(
  */
 
 #ifdef TK_USE_INPUT_METHODS
+/*
+ * If the XIC has been invalidated, it must be recreated.
+ */
+if (winPtr->dispPtr->ximGeneration != winPtr->ximGeneration) {
+   winPtr->flags &= ~TK_CHECKED_IC;
+   winPtr->inputContext = NULL;
+}
+
 if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)) {
if (!(winPtr->flags & (TK_CHECKED_IC|TK_ALREADY_DEAD))) {
winPtr->flags |= TK_CHECKED_IC;
@@ -1295,7 +1304,9 @@ Tk_HandleEvent(
CreateXIC(winPtr);
}
}
-   if (eventPtr->type == FocusIn && winPtr->inputContext != NULL) {
+   if ((eventPtr->type == FocusIn) &&
+   (winPtr->dispPtr->inputMethod != NULL) &&
+   (winPtr->inputContext != NULL)) {
XSetICFocus(winPtr->inputContext);
}
 }

commit 596abb7b53897447dda6044725ea94a664dae64e
Author: jan.nijtmans 
Date:   Fri Feb 10 11:38:55 2017 +

Fix [7d967c68a09e07e355358af40f36dd5dd84c7022|7d967c68a0] follow-up: Tk 
applications segmentation fault when ibus-daemon IME is restarted. Patch by 
Brad Lanam.

diff --git a/generic/tkWindow.c b/generic/tkWindow.c
index e4d696bdd..690a8412d 100644
--- a/generic/tkWindow.c
+++ b/generic/tkWindow.c
@@ -475,9 +475,6 @@ GetScreen(
dispPtr->cursorFont = None;
dispPtr->warpWindow = NULL;
dispPtr->multipleAtom = None;
-#ifdef TK_USE_INPUT_METHODS
-   dispPtr->ximGeneration = 0;
-#endif /*TK_USE_INPUT_METHODS*/
 
/*
 * By default we do want to collapse motion events in
@@ -656,6 +653,7 @@ TkAllocWindow(
 winPtr->flags = 0;
 winPtr->handlerList = NULL;
 #ifdef TK_USE_INPUT_METHODS
+winPtr->ximGeneration = 0;
 winPtr->inputContext = NULL;
 #endif /* TK_USE_INPUT_METHODS */
 winPtr->tagPtr = NULL;