Handle overflow in __hcreate_r
[glibc.git] / misc / hsearch_r.c
blob559df29cf7b124dbdf88f089f1ed5a784b364105
1 /* Copyright (C) 1993-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <malloc.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <search.h>
25 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
26 [Knuth] The Art of Computer Programming, part 3 (6.4) */
29 /* The reentrant version has no static variables to maintain the state.
30 Instead the interface of all functions is extended to take an argument
31 which describes the current status. */
32 typedef struct _ENTRY
34 unsigned int used;
35 ENTRY entry;
37 _ENTRY;
40 /* For the used double hash method the table size has to be a prime. To
41 correct the user given table size we need a prime test. This trivial
42 algorithm is adequate because
43 a) the code is (most probably) called a few times per program run and
44 b) the number is small because the table must fit in the core */
45 static int
46 isprime (unsigned int number)
48 /* no even number will be passed */
49 unsigned int div = 3;
51 while (div * div < number && number % div != 0)
52 div += 2;
54 return number % div != 0;
58 /* Before using the hash table we must allocate memory for it.
59 Test for an existing table are done. We allocate one element
60 more as the found prime number says. This is done for more effective
61 indexing as explained in the comment for the hsearch function.
62 The contents of the table is zeroed, especially the field used
63 becomes zero. */
64 int
65 __hcreate_r (nel, htab)
66 size_t nel;
67 struct hsearch_data *htab;
69 /* Test for correct arguments. */
70 if (htab == NULL)
72 __set_errno (EINVAL);
73 return 0;
76 if (nel >= SIZE_MAX / sizeof (_ENTRY))
78 __set_errno (ENOMEM);
79 return 0;
83 /* There is still another table active. Return with error. */
84 if (htab->table != NULL)
85 return 0;
87 /* We need a size of at least 3. Otherwise the hash functions we
88 use will not work. */
89 if (nel < 3)
90 nel = 3;
91 /* Change nel to the first prime number not smaller as nel. */
92 nel |= 1; /* make odd */
93 while (!isprime (nel))
94 nel += 2;
96 htab->size = nel;
97 htab->filled = 0;
99 /* allocate memory and zero out */
100 htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
101 if (htab->table == NULL)
102 return 0;
104 /* everything went alright */
105 return 1;
107 libc_hidden_def (__hcreate_r)
108 weak_alias (__hcreate_r, hcreate_r)
111 /* After using the hash table it has to be destroyed. The used memory can
112 be freed and the local static variable can be marked as not used. */
113 void
114 __hdestroy_r (htab)
115 struct hsearch_data *htab;
117 /* Test for correct arguments. */
118 if (htab == NULL)
120 __set_errno (EINVAL);
121 return;
124 /* Free used memory. */
125 free (htab->table);
127 /* the sign for an existing table is an value != NULL in htable */
128 htab->table = NULL;
130 libc_hidden_def (__hdestroy_r)
131 weak_alias (__hdestroy_r, hdestroy_r)
134 /* This is the search function. It uses double hashing with open addressing.
135 The argument item.key has to be a pointer to an zero terminated, most
136 probably strings of chars. The function for generating a number of the
137 strings is simple but fast. It can be replaced by a more complex function
138 like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
140 We use an trick to speed up the lookup. The table is created by hcreate
141 with one more element available. This enables us to use the index zero
142 special. This index will never be used because we store the first hash
143 index in the field used where zero means not used. Every other value
144 means used. The used field can be used as a first fast comparison for
145 equality of the stored and the parameter value. This helps to prevent
146 unnecessary expensive calls of strcmp. */
148 __hsearch_r (item, action, retval, htab)
149 ENTRY item;
150 ACTION action;
151 ENTRY **retval;
152 struct hsearch_data *htab;
154 unsigned int hval;
155 unsigned int count;
156 unsigned int len = strlen (item.key);
157 unsigned int idx;
159 /* Compute an value for the given string. Perhaps use a better method. */
160 hval = len;
161 count = len;
162 while (count-- > 0)
164 hval <<= 4;
165 hval += item.key[count];
167 if (hval == 0)
168 ++hval;
170 /* First hash function: simply take the modul but prevent zero. */
171 idx = hval % htab->size + 1;
173 if (htab->table[idx].used)
175 /* Further action might be required according to the action value. */
176 if (htab->table[idx].used == hval
177 && strcmp (item.key, htab->table[idx].entry.key) == 0)
179 *retval = &htab->table[idx].entry;
180 return 1;
183 /* Second hash function, as suggested in [Knuth] */
184 unsigned int hval2 = 1 + hval % (htab->size - 2);
185 unsigned int first_idx = idx;
189 /* Because SIZE is prime this guarantees to step through all
190 available indeces. */
191 if (idx <= hval2)
192 idx = htab->size + idx - hval2;
193 else
194 idx -= hval2;
196 /* If we visited all entries leave the loop unsuccessfully. */
197 if (idx == first_idx)
198 break;
200 /* If entry is found use it. */
201 if (htab->table[idx].used == hval
202 && strcmp (item.key, htab->table[idx].entry.key) == 0)
204 *retval = &htab->table[idx].entry;
205 return 1;
208 while (htab->table[idx].used);
211 /* An empty bucket has been found. */
212 if (action == ENTER)
214 /* If table is full and another entry should be entered return
215 with error. */
216 if (htab->filled == htab->size)
218 __set_errno (ENOMEM);
219 *retval = NULL;
220 return 0;
223 htab->table[idx].used = hval;
224 htab->table[idx].entry = item;
226 ++htab->filled;
228 *retval = &htab->table[idx].entry;
229 return 1;
232 __set_errno (ESRCH);
233 *retval = NULL;
234 return 0;
236 libc_hidden_def (__hsearch_r)
237 weak_alias (__hsearch_r, hsearch_r)