From: Stephan Bosch Date: Tue, 26 Apr 2022 00:14:08 +0000 (+0200) Subject: lib: unichar - Don't return 0 from uni_utf8_get_char_n() when NUL byte is encountered. X-Git-Tag: 2.4.0~3951 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=348edb82f2658d3199ccfa492dbdde8124416ddb;p=thirdparty%2Fdovecot%2Fcore.git lib: unichar - Don't return 0 from uni_utf8_get_char_n() when NUL byte is encountered. 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. --- diff --git a/src/lib/unichar.c b/src/lib/unichar.c index 85a2ae620a..1e76cc4d44 100644 --- a/src/lib/unichar.c +++ b/src/lib/unichar.c @@ -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;