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
parent
17d4f8d699
commit
58529c1827
|
@ -814,30 +814,21 @@ 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) {
|
||||
retval++;
|
||||
}
|
||||
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) {
|
||||
retval++;
|
||||
}
|
||||
while (SDL_StepUTF8(&str, bytes)) {
|
||||
bytes -= (size_t) (str - strstart);
|
||||
strstart = str;
|
||||
retval++;
|
||||
}
|
||||
|
||||
return retval;
|
||||
|
|
Loading…
Reference in New Issue