Tk Source Code

View Ticket
Login
Ticket UUID: 1908443
Title: Composed characters in UTF-8 locale
Type: Bug Version: obsolete: 8.5.1
Submitter: jenglish Created on: 2008-03-06 03:01:18
Subsystem: 99. Other Assigned To: jenglish
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2008-07-05 02:19:06
Resolution: Fixed Closed By: jenglish
    Closed on: 2008-07-04 19:19:06
Description:
Observed on Debian Sarge after installing UTF-8 locales: ISO8859-1 characters entered with compose key sequences end up wrong.

Setup: xmodmap -e 'keysym Super_L = Multi_key Super_L'
(this makes the Windows key into a Compose key).
export LC_ALL=en_US.UTF-8
export XMODIFIERS=@im=local

Run wish; verify that [encoding system] is utf-8
Press e.g., <Compose> <c> <comma>.  
This should turn into รง (c-cedilla, \UE7).  Instead, it shows up as \UFFE7.

I think I've narrowed this down to sometime between 8.4.12 and 8.4.13.  Problem appears to be in Tcl, not Tk.

This looks like improper sign extension.
User Comments: jenglish added on 2008-07-05 02:19:06:
Logged In: YES 
user_id=68433
Originator: YES

Backported tclEncoding.c fix to core-8-4 and core-8-5 branches.  Closing.

jenglish added on 2008-06-11 08:31:30:
Logged In: YES 
user_id=68433
Originator: YES

Patch#1986818 committed to Tk CVS HEAD and core-8-5 branch, which should fix the underlying problem.

Patch to generic/tclEncoding.c committed to Tcl CVS HEAD; will backport if nobody's compiler complains (gcc does not; I suspect MSVC might want another unnecessary cast or three).

flatworm added on 2008-06-07 21:21:20:
Logged In: YES 
user_id=1350198
Originator: NO

See also 1967075 -- may be they're related.

jenglish added on 2008-06-07 02:56:06:
Logged In: YES 
user_id=68433
Originator: YES

Proposed fix: Patch#1986818

jenglish added on 2008-03-06 11:16:12:
Logged In: YES 
user_id=68433
Originator: YES

Sorry, correction to initial report: test was run with LC_ALL=en_US.  Setting LC_ALL=en_US.UTF-8 yields different results, possibly correct.)

jenglish added on 2008-03-06 11:10:40:
Logged In: YES 
user_id=68433
Originator: YES

Quoth TFM: "The XmbLookupString and XwcLookupString functions return text in the encoding of the locale bound to the input method of the specified input context."

It appears that Xlib is using a different set of heuristics to determine the encoding of a locale than Tcl (and glibc) does.  Xlib apparently uses the table in /usr/lib/X11/locale/locale.alias, while Tcl uses nl_langinfo.

jenglish added on 2008-03-06 10:48:23:
Logged In: YES 
user_id=68433
Originator: YES

This is one of the nasty surprises in C89: when casting a (signed) char to an unsigned short, it gets widened to a signed short first, then converted to an unsigned short.  Sign extension happens in the first step.

Changing the line from:

    ch = (Tcl_UniChar) *src;

to 

    ch = (unsigned char) *src;

prevents sign extension and makes things behave as expected.  (You don't need to say "(Tcl_UniChar)(unsigned char)*src", since the usual integral promotions apply.  MSVC might complain though.)

This masks the problem, but does not fix it: the real problem is that UtfToUtfProc is getting called in the first place.

XmbLookupString() is apparently returning ISO8859-1 text, but Tk believes this is in the "system" encoding, which is utf-8.  IOW, Xlib's idea of "the system encoding" is different from Tcl's.

More research required.

jenglish added on 2008-03-06 10:40:50:
Logged In: YES 
user_id=68433
Originator: YES

Specifically, this part:

generic/tclEncoding.c r1.16.2.9 -> r1.16.2.10

@@ -2083,13 +2083,23 @@ UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
             */
            *dst++ = 0;
            src += 2;
+       } else if (!Tcl_UtfCharComplete(src, srcEnd - src)) {
+           /* Always check before using Tcl_UtfToUniChar. Not doing
+            * can so cause it run beyond the endof the buffer!  If we
+            * * happen such an incomplete char its byts are made to *
+            * represent themselves.
+            */
+
+           ch = (Tcl_UniChar) *src;
                 ^^^^^^^^^^^^^^^^^^^  here
+           src += 1;
+           dst += Tcl_UniCharToUtf(ch, dst);
        } else {
            src += Tcl_UtfToUniChar(src, &ch);
            dst += Tcl_UniCharToUtf(ch, dst);
        }
     }

jenglish added on 2008-03-06 10:18:25:
Logged In: YES 
user_id=68433
Originator: YES

`git bisect` narrows it down to this commit:

Author: andreas_kupries <andreas_kupries>
Date:   Wed Apr 5 00:05:53 2006 +0000

        * generic/tclIO.c (ReadChars): Added check and panic and
          commentary to a piece of code which relies on BUFFER_PADDING to
          create enough space at the beginning of each buffer forthe
          insertion of partial multi-byte data at the beginning of a
          buffer. To explain why this code is ok, and as precaution if
          someone twiddled the BUFFER_PADDING into uselessness.

        * generic/tclIO.c (ReadChars): [SF Tcl Bug 1462248]. Added code
          temporarily suppress the use of TCL_ENCODING_END set when eof
          was reached while the buffer we are converting is not truly the
          last buffer in the queue. together with the Utf bug below it was
          possible to completely bollox the buffer data structures,
          eventually crashing Tcl.

        * generic/tclEncoding.c (UtfToUtfProc): Fixed problem where the
          function accessed memory beyond the end of the input
          buffer. When TCL_ENCODING_END is set and the last bytes of the
          buffer start a multi-byte sequence. This bug contributed to [SF
          Tcl Bug 1462248].