X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fhash.c;h=7d76d45f71250c5d6b92a816fc69b1e23c4ce5a3;hb=fa3486514bd8f15f8eb4c49821d0356b52e5a335;hp=ad4599e4d5173e8a38c6a4e77bdae01eb0120f23;hpb=c31c5f30e8606260d4963b96a97b7dbce7607b67;p=gnulib.git diff --git a/lib/hash.c b/lib/hash.c index ad4599e4d..7d76d45f7 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -1,14 +1,14 @@ /* 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, 2006, 2007 Free + Software Foundation, Inc. Written by Jim Meyering, 1992. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -16,19 +16,17 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + along with this program. If not, see . */ /* A generic hash table package. */ /* Define USE_OBSTACK to 1 if you want the allocator to use obstacks instead of malloc. If you change USE_OBSTACK, you have to recompile! */ -#if HAVE_CONFIG_H -# include -#endif +#include #include "hash.h" +#include "xalloc.h" #include #include @@ -399,9 +397,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 +418,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,13 +568,15 @@ 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); + if (table->bucket == NULL) + goto fail; table->bucket_limit = table->bucket + table->n_buckets; table->n_buckets_used = 0; table->n_entries = 0;