libselinux: use DJB2a string hash function

The hash table implementation uses `& (SIDTAB_SIZE - 1)` to truncate
generated hashes to the number of buckets.  This operation is equal to
`% SIDTAB_SIZE` if and only if the size is a power of two (which seems
to be always the case).  One property of the binary and with a power of
two (and probably a small one <=2048) is all higher bits are discarded.
Thus a hash function is needed with a good avalanche effect, which the
current one is not.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2023-08-16 14:38:44 +02:00 committed by James Carter
parent 08be63572e
commit f1178a13dc
1 changed files with 6 additions and 9 deletions

View File

@ -15,16 +15,13 @@
static inline unsigned sidtab_hash(const char * key)
{
const char *p;
unsigned int size;
unsigned int val;
unsigned int hash = 5381;
unsigned char c;
val = 0;
size = strlen(key);
for (p = key; (unsigned int)(p - key) < size; p++)
val =
(val << 4 | (val >> (8 * sizeof(unsigned int) - 4))) ^ (*p);
return val & (SIDTAB_SIZE - 1);
while ((c = *(unsigned const char *)key++))
hash = ((hash << 5) + hash) ^ c;
return hash & (SIDTAB_SIZE - 1);
}
int sidtab_init(struct sidtab *s)