Re: RFC: Small mod to fl_utf8len() to detect invalid single char UTF-8 bytes
Greg Ercolano
15:33 Aug 12
On 8/12/26 03:40, 'Albrecht Schlosser'
via fltk.coredev wrote:
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.
Ya, at least for UTF-8 encoding of Unicode.
I caught that 4 byte Unicode limit too, which is why I use a 4
byte buffer in Fl_Terminal for saving "chars".
Instead
of adding this line we should return -1 instead of 5 or 6 anyway,
to be Unicode compliant.
I guess it depends on whether our fl_utf8 functions should allow
for full utf-8 encoding, perhaps to support utf-8 encoding of other
types of data that might allow 6 bytes, or if we want to limit it to
Unicode purposes only.
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 think improving invalidation detection is always good, and it
would help my terminal tests for sure, as I need a reliable way to
test for invalid utf-8 chars.
This has to do with me working on an issue you (Albrecht) opened
against Fl_Terminal, issue #950.
I've only now been able to get around to it, because I needed to
find related changes I'd made years ago but not committed, and lost
track of them because it was floating around on the local filesystem
of a workstation I had to decomission, and I only recently tracked
down and restored.
I have the mods reapplied, and expect a commit today after some
final testing which should close that old issue.
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.
Yes, in fact there's a few other bytes that are not allowed;
bytes above 0xF4 are apparently invalid,
and there are two others, I think 0xC0 or some such.
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'.