--- gtkimcontextxim.c	2003-01-24 08:26:22.000000000 +0700
+++ gtkimcontextxim.c.v2	2003-01-24 08:57:27.000000000 +0700
@@ -1053,10 +1053,12 @@
         case XIMForwardChar:
           for (i = conv_data->position; i > 0 && *p; --i)
             p = g_utf8_next_char (p);
-          if (i > 0) break;
+          if (i > 0)
+            break;
           for (i = conv_data->factor, q = p; i > 0 && *q; --i)
             q = g_utf8_next_char (q);
-          if (i > 0) break;
+          if (i > 0)
+            break;
           text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
           subst_offset = conv_data->position;
           subst_nchars = conv_data->factor;
@@ -1064,9 +1066,12 @@
         case XIMBackwardChar:
           for (i = conv_data->position; i > 0 && p > surrounding; --i)
             p = g_utf8_prev_char (p);
-          if (i > 0) break;
+          if (i > 0)
+            break;
           for (i = conv_data->factor, q = p; i > 0 && *q; --i)
             q = g_utf8_next_char (q);
+          if (i > 0)
+            break;
           text = g_locale_from_utf8 (p, q - p, NULL, &text_len, NULL);
           subst_offset = -conv_data->position;
           subst_nchars = conv_data->factor;
@@ -1084,15 +1089,25 @@
         default:
           break;
         }
+      /* block out any failure happenning to "text", including conversion */
       if (text)
         {
           conv_data->text = (XIMStringConversionText *)
                               malloc (sizeof (XIMStringConversionText));
-          conv_data->text->length = text_len;
-          conv_data->text->feedback = NULL;
-          conv_data->text->encoding_is_wchar = False;
-          conv_data->text->string.mbs = (char *)malloc (text_len);
-          memcpy (conv_data->text->string.mbs, text, text_len);
+          if (conv_data->text)
+            {
+              conv_data->text->length = text_len;
+              conv_data->text->feedback = NULL;
+              conv_data->text->encoding_is_wchar = False;
+              conv_data->text->string.mbs = (char *)malloc (text_len);
+              if (conv_data->text->string.mbs)
+                memcpy (conv_data->text->string.mbs, text, text_len);
+              else
+                {
+                  free (conv_data->text);
+                  conv_data->text = NULL;
+                }
+            }
 
           g_free (text);
         }
