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
{
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 */