rdebath/PuTTY

Type conflict using DIRECT_CHAR macro

Closed this issue · 0 comments

@matbech commented on fd97538

I believe setting the CSET_ASCII value outside of the wchar_t range (16-bit) breaks (always returns 0) the DIRECT_CHAR macro when a wchar_t value is passed. This is the case for Windows:

if (DIRECT_CHAR(ucsdata->unitab_line[i]))

and here:
if (DIRECT_CHAR(text[0]) &&

You're quite correct. However, I think that this means that the conditions are incorrect.

Both of these in effect check to see if unitab_line contains CSET_ASCII characters. The first on is a direct check of that array. This means that the first one has been upgraded from "Should not happen" to "Cannot happen" and that's fine.

The one in window.c is translated via the array in do_paint in terminal.c

            switch (tchar & CSET_MASK) {
              case CSET_ASCII:
                tchar = term->ucsdata->unitab_line[tchar & 0xFF];
                break;
              case CSET_LINEDRW:
                tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
                break;
              case CSET_SCOACS:
                tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
                break;
            }

So the second has gone from a "should not happen" check that I used for debugging in the early days (making sure that they didn't sneak in) to a default translation of ISO8859-1. This is also okay (see "should not happen"), but it might be good to recreate the check directly in the UCS4 to UTF-16 translation in do_paint

#ifdef PLATFORM_IS_UTF16
            if (tchar >= 0x10000 && tchar < 0x110000) {
                ch[ccount++] = (wchar_t) HIGH_SURROGATE_OF(tchar);
                ch[ccount++] = (wchar_t) LOW_SURROGATE_OF(tchar);
            } else
#endif /* PLATFORM_IS_UTF16 */
            ch[ccount++] = (wchar_t) tchar;

eg: set it to U+FFFD if the character is out of range.

#ifdef PLATFORM_IS_UTF16
            if (tchar >= 0x10000 && tchar < 0x110000) {
                ch[ccount++] = (wchar_t) HIGH_SURROGATE_OF(tchar);
                ch[ccount++] = (wchar_t) LOW_SURROGATE_OF(tchar);
            } else if (tchar >= 0x110000)
                ch[ccount++] = 0xFFFD;
            else
#endif /* PLATFORM_IS_UTF16 */
            ch[ccount++] = (wchar_t) tchar;