]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
handle zero terminated strings better in UTF8 checks.
authorAlan T. DeKok <aland@freeradius.org>
Sun, 28 Sep 2025 13:42:44 +0000 (09:42 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 2 Oct 2025 21:07:59 +0000 (17:07 -0400)
the final zero is not valid UTF8.
Control characters are not valid UTF8.
Limit the allowed UTF8 length to where the trailing zero is located

src/lib/util/print.c

index 374756dd3ab609e935ea99b8b898bab8a4b7e982..a330feb5d1b1b959df09a68bd46f90192ce74af4 100644 (file)
@@ -40,7 +40,17 @@ inline size_t fr_utf8_char(uint8_t const *str, ssize_t inlen)
 {
        if (inlen == 0) return 0;
 
-       if (inlen < 0) inlen = 4;       /* longest char */
+       if (inlen < 0) {
+               if (*str < 0x20) return 0; /* end of string, or control characters. */
+
+               /*
+                *      The trailing zero can occur at any point in
+                *      the next 4 characters.
+                */
+               for (inlen = 1; inlen <= 4; inlen++) {
+                       if (!str[inlen]) break;
+               }
+       }
 
        if (*str <= 0x7f) return 1;     /* 1 */