diff --git a/src/common/saslprep.c b/src/common/saslprep.c index e7e909a0c87..80b9edb3968 100644 --- a/src/common/saslprep.c +++ b/src/common/saslprep.c @@ -1009,15 +1009,17 @@ pg_utf8_string_len(const char *source) const unsigned char *p = (const unsigned char *) source; int l; int num_chars = 0; + size_t len = strlen(source); - while (*p) + while (len) { l = pg_utf_mblen(p); - if (!pg_utf8_islegal(p, l)) + if (len < l || !pg_utf8_islegal(p, l)) return -1; p += l; + len -= l; num_chars++; }