]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: unichar - Don't return 0 from uni_utf8_get_char_n() when NUL byte is encountered.
authorStephan Bosch <stephan.bosch@open-xchange.com>
Tue, 26 Apr 2022 00:14:08 +0000 (02:14 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Fri, 17 Jun 2022 07:58:23 +0000 (07:58 +0000)
This caused problems for code that uses this function to read from a
size-limited buffer that can contain NUL bytes; i.e. lib-smtp. In that case,
hitting a NUL byte yields the same result as hitting the end of the buffered
data, which is unacceptable. No other code relies on the function returning 0,
so this can be changed safely to returning -1 instead.

src/lib/unichar.c

index 85a2ae620ace9387959ec91c69a690478c870f64..1e76cc4d44b50301d0108e8f585c73aab9aae481 100644 (file)
@@ -89,8 +89,10 @@ int uni_utf8_get_char_n(const void *_input, size_t max_len, unichar_t *chr_r)
 
        /* the following bytes must all be 10xxxxxx */
        for (i = 1; i < len; i++) {
-               if ((input[i] & 0xc0) != 0x80)
-                       return input[i] == '\0' ? 0 : -1;
+               if ((input[i] & 0xc0) != 0x80) {
+                       return (max_len == SIZE_MAX && input[i] == '\0' ?
+                               0 : -1);
+               }
 
                chr <<= 6;
                chr |= input[i] & 0x3f;