stdlib: SDL_utf8strlen and SDL_utf8strnlen now use SDL_StepUTF8 internally.

Otherwise, they might find out strings with malformed UTF-8 sequences produce
a different amount of codepoints than the count returned here, overflowing
buffers that might be allocated based on the results.
main
Ryan C. Gordon 2024-04-01 22:43:19 -04:00
parent 17d4f8d699
commit 58529c1827
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 7 additions and 16 deletions

View File

@ -814,31 +814,22 @@ size_t SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size
size_t SDL_utf8strlen(const char *str)
{
size_t retval = 0;
const char *p = str;
unsigned char ch;
while ((ch = *(p++)) != 0) {
/* if top two bits are 1 and 0, it's a continuation byte. */
if ((ch & 0xc0) != 0x80) {
while (SDL_StepUTF8(&str, 4)) {
retval++;
}
}
return retval;
}
size_t SDL_utf8strnlen(const char *str, size_t bytes)
{
size_t retval = 0;
const char *p = str;
unsigned char ch;
const char *strstart = str;
while ((ch = *(p++)) != 0 && bytes-- > 0) {
/* if top two bits are 1 and 0, it's a continuation byte. */
if ((ch & 0xc0) != 0x80) {
while (SDL_StepUTF8(&str, bytes)) {
bytes -= (size_t) (str - strstart);
strstart = str;
retval++;
}
}
return retval;
}