X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fhash.c;h=374c22681534aee7b9cd009689e408d8a59b6a0f;hb=c60b43afa76cbc6ed19a4794eab0fe15f9b54300;hp=ad4599e4d5173e8a38c6a4e77bdae01eb0120f23;hpb=c31c5f30e8606260d4963b96a97b7dbce7607b67;p=gnulib.git diff --git a/lib/hash.c b/lib/hash.c index ad4599e4d..374c22681 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -1,7 +1,7 @@ /* hash - hashing table processing. - Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software - Foundation, Inc. + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free + Software Foundation, Inc. Written by Jim Meyering, 1992. @@ -29,6 +29,7 @@ #endif #include "hash.h" +#include "xalloc.h" #include #include @@ -399,9 +400,10 @@ hash_string (const char *string, size_t n_buckets) ((Byte) + ROTATE_LEFT (Value, 7)) size_t value = 0; + unsigned char ch; - for (; *string; string++) - value = HASH_ONE_CHAR (value, (unsigned char) *string); + for (; (ch = *string); string++) + value = HASH_ONE_CHAR (value, ch); return value % n_buckets; # undef ROTATE_LEFT @@ -419,9 +421,10 @@ size_t hash_string (const char *string, size_t n_buckets) { size_t value = 0; + unsigned char ch; - while (*string) - value = (value * 31 + (unsigned char) *string++) % n_buckets; + for (; (ch = *string); string++) + value = (value * 31 + ch) % n_buckets; return value; } @@ -568,10 +571,10 @@ hash_initialize (size_t candidate, const Hash_tuning *tuning, candidate = new_candidate; } - if (SIZE_MAX / sizeof *table->bucket < candidate) + if (xalloc_oversized (candidate, sizeof *table->bucket)) goto fail; table->n_buckets = next_prime (candidate); - if (SIZE_MAX / sizeof *table->bucket < table->n_buckets) + if (xalloc_oversized (table->n_buckets, sizeof *table->bucket)) goto fail; table->bucket = calloc (table->n_buckets, sizeof *table->bucket);