RFC: Small mod to fl_utf8len() to detect invalid single char UTF-8 bytes
Greg Ercolano
03:35 Aug 12
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.
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.
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'.