FLTK logo

Re: [fltk.coredev] RFC: Small mod to fl_utf8len() to detect invalid single char UTF-8 bytes

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Library      Forums      Links      Apps     Login 
[  All Forums  |  Back to fltk.coredev  ]
 
[ Previous Message ] [ New Message | Reply ] [ Next Message ]

Re: RFC: Small mod to fl_utf8len() to detect invalid single char UTF-8 bytes "'Albrecht Schlosser' via fltk.coredev" 10:40 Aug 12  
 
On 8/12/26 05:35 Greg Ercolano wrote:
Suggesting this one line change to fl_utf8len() to detect the two invalid
single character UTF-8 chars 0xFE and 0xFF:
@@ -107,6 +107,7 @@ int fl_utf8len(char c)
       if (c & 0x10) {
         if (c & 0x08) {
           if (c & 0x04) {
+            if (c & 0x02) return -1;
             return 6;
           }
           return 5;
A similar change can be made to fl_utf8len1() for symmetry.

The change seems a good way to implement, as it /only/ does the extra test if the utf-8 string is the rare length of 6, so it won't even affect the speed of shorter lengths.

The problem with the current implementation is (and has "always" been) that proper UTF-8 encoding covers only 4 bytes (21 bits of Unicode code points, which is, AFAICT, the limit of current and future Unicode), and therefore returning 5 or 6 is faulty anyway.

Instead of adding this line we should return -1 instead of 5 or 6 anyway, to be Unicode compliant.

Wikipedia says (I copied the full reference, which may only be readable if you read the HTML encoding of this mail):
https://en.wikipedia.org/wiki/UTF-8#Description :

UTF-8 encodes code points in one to four bytes, depending on the value of the code point. In the following table, the letters u through z represent the hexadecimal digits of a character’s Unicode number (U+uvwxyz, or U+wxyz for a four-digit number). In the binary column, each hex digit is expanded into its 4-bit binary equivalent (uuuu to zzzz) to show how its bits are distributed across the UTF-8 bytes.

Code point ↔ UTF-8 conversion
First code point Last code point Byte 1 Byte 2 Byte 3 Byte 4
U+0000 U+007F 0yyyzzzz
U+0080 U+07FF 110xxxyy 10yyzzzz
U+0800 U+FFFF 1110wwww 10xxxxyy 10yyzzzz
U+010000 U+10FFFF 11110uvv 10vvwwww 10xxxxyy 10yyzzzz

(end of citation)

The tables (and the code) in our lib obviously predate the reduction of the Unicode range from full 32 bits (or 31 bits?) to only 21 bits which is the standard.

I carefully made a test app comparing old code to the new for all 256 possible input values to the function, and the only difference the change makes is it now returns -1 for the 0xFE and 0xFF cases.

Since this isn't my code, I thought it best to run it up the flagpole to see if there's any negative effects you all who know utf8 might foresee.

Why this came up:
I'm working on Fl_Terminal to make sure it properly detects invalid UTF-8 strings, and since the single characters 0xFF and 0xFE are invalid anywhere in UTF-8 strings, they make a convenient test case. However, I noticed our fl_utf8len() function wasn't catching them.

I'm not sure if it is now a good time (before the release of 1.5) to change such a function in the way I would suggest for correctness (handling length 5 and 6 as error and returning -1 instead), and I didn't check FLTK's code for any side effects, but I think it would be OK. It would definitely not violate the documentation because the documentation doesn't specify what valid and invalid encodings are.

I'm pretty sure, however, that our UTF-8 to Unicode Code Points (and vice versa) also treat such invalid Unicode values in the wrong way.

Regarding the tests in Fl_Terminal: you could test for `x < 0 || x > 4` (or equivalent) to catch all really invalid UTF-8 encodings - which would catch the mentioned byte values but doesn't mean that this would catch all invalid Unicode code points, BTW.

--
You received this message because you are subscribed to the Google Groups "fltk.coredev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkcoredev+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/fltkcoredev/3f399c3b-857d-45b3-b9c6-fd97dd5ff8a7%40aljus.de.
[ Direct Link to Message ]
 
     
[ Previous Message ] [ New Message | Reply ] [ Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2026 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.