SDL: stdlib: SDL_utf8strlen and SDL_utf8strnlen now use SDL_StepUTF8 internally.

From 58529c182760ae8225a19da64d18db03e6694231 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 1 Apr 2024 22:43:19 -0400
Subject: [PATCH] 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.
---
 src/stdlib/SDL_string.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index cc092301604b4..06838b8753c4c 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -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;