]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: Make sure mail_user_hash() won't return 0 as the hash.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 19 Jul 2017 13:49:18 +0000 (16:49 +0300)
committerTimo Sirainen <tss@dovecot.fi>
Tue, 5 Sep 2017 14:41:50 +0000 (17:41 +0300)
It doesn't seem to actually happen, but this makes sure of it.

src/lib-mail/mail-user-hash.c
src/lib-mail/mail-user-hash.h

index 6be6c92a450849c25ed2bda974d813f3cd32e5de..3ccfee213ec7c476568699c6fc2a4b6ff5e5ac7d 100644 (file)
@@ -41,6 +41,12 @@ bool mail_user_hash(const char *username, const char *format,
        } T_END;
        for (i = 0; i < sizeof(hash); i++)
                hash = (hash << CHAR_BIT) | md5[i];
+       if (hash == 0) {
+               /* Make sure we don't return the hash as 0, since it's often
+                  treated in a special way that won't work well. For example
+                  trying to insert it into a hash table will assert-crash. */
+               hash = 1;
+       }
        *hash_r = hash;
        *error_r = t_strdup(error_dup);
        i_free(error_dup);
index 5362389a69acebb83ee112ac3a2a99d73a38f36c..e3a9e593ee90433c4f8dc01f2b9e9c3ae2827dfb 100644 (file)
@@ -2,7 +2,8 @@
 #define MAIL_USER_HASH
 
 /* Get a hash for username, based on given format. The format can use
-   %n, %d and %u variables. Returns TRUE if ok, FALSE if format is invalid. */
+   %n, %d and %u variables. The returned hash is never 0.
+   Returns TRUE if ok, FALSE if format is invalid. */
 bool mail_user_hash(const char *username, const char *format,
                    unsigned int *hash_r, const char **error_r);