Coverage Report

Created: 2026-12-01 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/hash.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#include <stddef.h> /* for offsetof() */
27
28
#include "hash.h"
29
#include "rand.h"
30
31
/* random patterns for API verification */
32
#ifdef DEBUGBUILD
33
71.1k
#define HASHINIT 0x7017e781
34
#define ITERINIT 0x5FEDCBA9
35
#endif
36
37
38
typedef size_t (*hash_function)(const void *key,
39
                                size_t key_length,
40
                                size_t slots_num);
41
42
typedef size_t (*comp_function)(const void *key1,
43
                                size_t key1_len,
44
                                const void *key2,
45
                                size_t key2_len);
46
47
/* Curl_hash stores its type in a uint8_t. */
48
typedef char hash_types_fit_uint8[
49
  ((CURL_HASH_TYPE_LAST - 1) <= UINT8_MAX) ? 1 : -1];
50
51
struct hash_functions {
52
  hash_function hash;
53
  comp_function compare;
54
};
55
56
/* set at global init, read-only afterwards */
57
static size_t hash_seed;
58
59
CURLcode Curl_hash_global_init(void)
60
2
{
61
2
  static bool seeded;
62
2
  unsigned char buf[sizeof(size_t)];
63
2
  size_t seed = 0;
64
2
  size_t i;
65
2
  CURLcode result;
66
67
2
  if(seeded)
68
0
    return CURLE_OK;
69
70
2
  result = Curl_rand_bytes(NULL,
71
2
#ifdef DEBUGBUILD
72
2
                           FALSE,
73
2
#endif
74
2
                           buf, sizeof(buf));
75
2
  if(result)
76
0
    return result;
77
78
18
  for(i = 0; i < sizeof(buf); i++)
79
16
    seed = (seed << 8) | buf[i];
80
81
2
  hash_seed = seed ? seed : 1;
82
2
  seeded = TRUE;
83
2
  return CURLE_OK;
84
2
}
85
86
/* @unittest 1603
87
 */
88
UNITTEST size_t hash_str_seeded(const void *key, size_t key_length,
89
                                size_t slots_num, size_t seed);
90
UNITTEST size_t hash_str_seeded(const void *key, size_t key_length,
91
                                size_t slots_num, size_t seed)
92
289k
{
93
289k
  const char *key_str = (const char *)key;
94
289k
  const char *end = key_str + key_length;
95
289k
  size_t h = 5381 ^ seed;
96
97
5.70M
  while(key_str < end) {
98
5.41M
    size_t j = (size_t)*key_str++;
99
5.41M
    h += h << 5;
100
5.41M
    h ^= j;
101
5.41M
  }
102
103
289k
  return (h % slots_num);
104
289k
}
105
106
/* @unittest 1603
107
 */
108
UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num);
109
UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num)
110
289k
{
111
289k
  return hash_str_seeded(key, key_length, slots_num, hash_seed);
112
289k
}
113
114
/* Avoid treating the variable-length key[1] as a one-byte object. */
115
static char *hash_elem_key(struct Curl_hash_element *he)
116
286k
{
117
286k
  return (char *)he + offsetof(struct Curl_hash_element, key);
118
286k
}
119
120
static size_t hash_socket(const void *key, size_t key_length,
121
                          size_t slots_num)
122
0
{
123
0
  curl_socket_t socket;
124
125
0
  DEBUGASSERT(key_length == sizeof(socket));
126
0
  if(key_length != sizeof(socket))
127
0
    return 0;
128
0
  memcpy(&socket, key, sizeof(socket));
129
0
  return (size_t)(socket % (curl_socket_t)slots_num);
130
0
}
131
132
static size_t compare_socket(const void *key1, size_t key1_len,
133
                             const void *key2, size_t key2_len)
134
0
{
135
0
  curl_socket_t socket1;
136
0
  curl_socket_t socket2;
137
138
0
  if((key1_len != sizeof(socket1)) || (key2_len != sizeof(socket2)))
139
0
    return 0;
140
0
  memcpy(&socket1, key1, sizeof(socket1));
141
0
  memcpy(&socket2, key2, sizeof(socket2));
142
0
  return socket1 == socket2;
143
0
}
144
145
static size_t compare_bytes(const void *key1, size_t key1_len,
146
                            const void *key2, size_t key2_len)
147
241k
{
148
241k
  return (key1_len == key2_len) && !memcmp(key1, key2, key1_len);
149
241k
}
150
151
static const struct hash_functions hash_functions[] = {
152
  { hash_str, compare_bytes },
153
  { hash_socket, compare_socket }
154
};
155
156
static const struct hash_functions *hash_get_functions(uint8_t type)
157
289k
{
158
289k
  DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST);
159
289k
  if((unsigned int)type >= CURL_HASH_TYPE_LAST)
160
0
    type = CURL_HASH_TYPE_BYTES;
161
289k
  return &hash_functions[type];
162
289k
}
163
164
static bool hash_key_is_valid(uint8_t type, size_t key_len)
165
348k
{
166
348k
  return (type == CURL_HASH_TYPE_BYTES) ||
167
14.4k
    ((type == CURL_HASH_TYPE_SOCKET) &&
168
14.4k
     (key_len == sizeof(curl_socket_t)));
169
348k
}
170
171
#if 0 /* useful function for debugging hashes and their contents */
172
static struct Curl_hash_element *hash_next_element(
173
  struct Curl_hash_iterator *iter)
174
{
175
  struct Curl_hash *h;
176
  DEBUGASSERT(iter->init == ITERINIT);
177
  h = iter->hash;
178
  if(!h->table)
179
    return NULL; /* empty hash, nothing to return */
180
181
  /* Get the next element in the current list, if any */
182
  if(iter->current)
183
    iter->current = iter->current->next;
184
185
  /* If we have reached the end of the list, find the next one */
186
  if(!iter->current) {
187
    size_t i;
188
    for(i = iter->slot_index; i < h->slots; i++) {
189
      if(h->table[i]) {
190
        iter->current = h->table[i];
191
        iter->slot_index = i + 1;
192
        break;
193
      }
194
    }
195
  }
196
197
  return iter->current;
198
}
199
200
static void hash_start_iterate(struct Curl_hash *hash,
201
                               struct Curl_hash_iterator *iter)
202
{
203
  DEBUGASSERT(hash->init == HASHINIT);
204
  iter->hash = hash;
205
  iter->slot_index = 0;
206
  iter->current = NULL;
207
#ifdef DEBUGBUILD
208
  iter->init = ITERINIT;
209
#endif
210
}
211
212
void Curl_hash_print(struct Curl_hash *h, void (*func)(void *))
213
{
214
  struct Curl_hash_iterator iter;
215
  struct Curl_hash_element *he;
216
  size_t last_index = UINT_MAX;
217
218
  if(!h)
219
    return;
220
221
  curl_mfprintf(stderr, "=Hash dump=\n");
222
223
  hash_start_iterate(h, &iter);
224
225
  he = hash_next_element(&iter);
226
  while(he) {
227
    if(iter.slot_index != last_index) {
228
      curl_mfprintf(stderr, "index %d:", (int)iter.slot_index);
229
      if(last_index != UINT_MAX) {
230
        curl_mfprintf(stderr, "\n");
231
      }
232
      last_index = iter.slot_index;
233
    }
234
235
    if(func)
236
      func(he->ptr);
237
    else
238
      curl_mfprintf(stderr, " [key=%.*s, he=%p, ptr=%p]",
239
                    (int)he->key_len, hash_elem_key(he),
240
                    (void *)he, (void *)he->ptr);
241
242
    he = hash_next_element(&iter);
243
  }
244
  curl_mfprintf(stderr, "\n");
245
}
246
#endif
247
248
/* Initializes a hash structure.
249
 *
250
 * @unittest: 1602
251
 * @unittest: 1603
252
 */
253
void Curl_hash_init(struct Curl_hash *h,
254
                    size_t slots,
255
                    Curl_hash_type type,
256
                    Curl_hash_dtor dtor)
257
71.1k
{
258
71.1k
  DEBUGASSERT(h);
259
71.1k
  DEBUGASSERT(slots);
260
71.1k
  DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST);
261
71.1k
  DEBUGASSERT(dtor);
262
263
71.1k
  h->table = NULL;
264
71.1k
  h->dtor = dtor;
265
71.1k
  h->size = 0;
266
71.1k
  h->slots = slots;
267
71.1k
  h->type = (uint8_t)type;
268
71.1k
#ifdef DEBUGBUILD
269
71.1k
  h->init = HASHINIT;
270
71.1k
#endif
271
71.1k
}
272
273
static struct Curl_hash_element *hash_elem_create(const void *key,
274
                                                  size_t key_len,
275
                                                  void *p,
276
                                                  Curl_hash_elem_dtor dtor)
277
33.1k
{
278
33.1k
  struct Curl_hash_element *he;
279
280
  /* allocate the struct plus memory after it to store the key */
281
33.1k
  he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len);
282
33.1k
  if(he) {
283
33.1k
    he->next = NULL;
284
    /* copy the key */
285
33.1k
    memcpy(hash_elem_key(he), key, key_len);
286
33.1k
    he->key_len = key_len;
287
33.1k
    he->ptr = p;
288
33.1k
    he->dtor = dtor;
289
33.1k
  }
290
33.1k
  return he;
291
33.1k
}
292
293
static void hash_elem_clear_ptr(struct Curl_hash *h,
294
                                struct Curl_hash_element *he)
295
35.1k
{
296
35.1k
  DEBUGASSERT(h);
297
35.1k
  DEBUGASSERT(he);
298
35.1k
  if(he->ptr) {
299
35.1k
    if(he->dtor)
300
11.5k
      he->dtor(hash_elem_key(he), he->key_len, he->ptr);
301
23.6k
    else
302
23.6k
      h->dtor(he->ptr);
303
35.1k
    he->ptr = NULL;
304
35.1k
  }
305
35.1k
}
306
307
static void hash_elem_destroy(struct Curl_hash *h,
308
                              struct Curl_hash_element *he)
309
33.1k
{
310
33.1k
  hash_elem_clear_ptr(h, he);
311
33.1k
  curlx_free(he);
312
33.1k
}
313
314
static void hash_elem_unlink(struct Curl_hash *h,
315
                             struct Curl_hash_element **he_anchor,
316
                             struct Curl_hash_element *he)
317
33.1k
{
318
33.1k
  *he_anchor = he->next;
319
33.1k
  --h->size;
320
33.1k
}
321
322
static void hash_elem_link(struct Curl_hash *h,
323
                           struct Curl_hash_element **he_anchor,
324
                           struct Curl_hash_element *he)
325
33.1k
{
326
33.1k
  he->next = *he_anchor;
327
33.1k
  *he_anchor = he;
328
33.1k
  ++h->size;
329
33.1k
}
330
331
void *Curl_hash_add2(struct Curl_hash *h,
332
                     const void *key, size_t key_len, void *p,
333
                     Curl_hash_elem_dtor dtor)
334
35.1k
{
335
35.1k
  const struct hash_functions *functions;
336
35.1k
  struct Curl_hash_element *he, **slot;
337
338
35.1k
  DEBUGASSERT(h);
339
35.1k
  DEBUGASSERT(h->slots);
340
35.1k
  DEBUGASSERT(h->init == HASHINIT);
341
35.1k
  if(!hash_key_is_valid(h->type, key_len))
342
0
    return NULL;
343
35.1k
  if(!h->table) {
344
28.5k
    h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *));
345
28.5k
    if(!h->table)
346
3
      return NULL; /* OOM */
347
28.5k
  }
348
349
35.1k
  functions = hash_get_functions(h->type);
350
35.1k
  slot = &h->table[functions->hash(key, key_len, h->slots)];
351
35.1k
  for(he = *slot; he; he = he->next) {
352
1.98k
    if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
353
      /* existing key entry, overwrite by clearing old pointer */
354
1.97k
      hash_elem_clear_ptr(h, he);
355
1.97k
      he->ptr = p;
356
1.97k
      he->dtor = dtor;
357
1.97k
      return p;
358
1.97k
    }
359
1.98k
  }
360
361
33.1k
  he = hash_elem_create(key, key_len, p, dtor);
362
33.1k
  if(!he)
363
7
    return NULL; /* OOM */
364
365
33.1k
  hash_elem_link(h, slot, he);
366
33.1k
  return p; /* return the new entry */
367
33.1k
}
368
369
/* Insert the data in the hash. If there already was a match in the hash, that
370
 * data is replaced. This function also "lazily" allocates the table if
371
 * needed, as it is not done in the _init function (anymore).
372
 *
373
 * @unittest: 1305
374
 * @unittest: 1602
375
 * @unittest: 1603
376
 */
377
void *Curl_hash_add(struct Curl_hash *h,
378
                    const void *key, size_t key_len, void *p)
379
23.6k
{
380
23.6k
  return Curl_hash_add2(h, key, key_len, p, NULL);
381
23.6k
}
382
383
/* Remove the identified hash entry.
384
 * Returns non-zero on failure.
385
 *
386
 * @unittest: 1603
387
 */
388
int Curl_hash_delete(struct Curl_hash *h, const void *key, size_t key_len)
389
29.2k
{
390
29.2k
  const struct hash_functions *functions;
391
392
29.2k
  DEBUGASSERT(h);
393
29.2k
  DEBUGASSERT(h->slots);
394
29.2k
  DEBUGASSERT(h->init == HASHINIT);
395
29.2k
  if(!hash_key_is_valid(h->type, key_len))
396
0
    return 1;
397
29.2k
  if(h->table) {
398
19.2k
    struct Curl_hash_element *he, **he_anchor;
399
400
19.2k
    functions = hash_get_functions(h->type);
401
19.2k
    he_anchor = &h->table[functions->hash(key, key_len, h->slots)];
402
19.2k
    while(*he_anchor) {
403
9.66k
      he = *he_anchor;
404
9.66k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
405
9.66k
        hash_elem_unlink(h, he_anchor, he);
406
9.66k
        hash_elem_destroy(h, he);
407
9.66k
        return 0;
408
9.66k
      }
409
0
      he_anchor = &he->next;
410
0
    }
411
19.2k
  }
412
19.5k
  return 1;
413
29.2k
}
414
415
/* Retrieves a hash element.
416
 *
417
 * @unittest: 1603
418
 */
419
void *Curl_hash_pick(struct Curl_hash *h, const void *key, size_t key_len)
420
284k
{
421
284k
  const struct hash_functions *functions;
422
423
284k
  DEBUGASSERT(h);
424
284k
  DEBUGASSERT(h->init == HASHINIT);
425
284k
  if(!hash_key_is_valid(h->type, key_len))
426
0
    return NULL;
427
284k
  if(h->table) {
428
234k
    struct Curl_hash_element *he;
429
234k
    DEBUGASSERT(h->slots);
430
234k
    functions = hash_get_functions(h->type);
431
234k
    he = h->table[functions->hash(key, key_len, h->slots)];
432
234k
    while(he) {
433
229k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
434
229k
        return he->ptr;
435
229k
      }
436
9
      he = he->next;
437
9
    }
438
234k
  }
439
54.2k
  return NULL;
440
284k
}
441
442
/* Destroys all the entries in the given hash and resets its attributes,
443
 * prepping the given hash for [static|dynamic] deallocation.
444
 *
445
 * @unittest: 1305
446
 * @unittest: 1602
447
 * @unittest: 1603
448
 */
449
void Curl_hash_destroy(struct Curl_hash *h)
450
71.1k
{
451
71.1k
  DEBUGASSERT(h->init == HASHINIT);
452
71.1k
  if(h->table) {
453
28.5k
    Curl_hash_clean(h);
454
28.5k
    curlx_safefree(h->table);
455
28.5k
  }
456
71.1k
  DEBUGASSERT(h->size == 0);
457
71.1k
  h->slots = 0;
458
71.1k
}
459
460
/* Removes all the entries in the given hash.
461
 *
462
 * @unittest: 1602
463
 */
464
void Curl_hash_clean(struct Curl_hash *h)
465
28.5k
{
466
28.5k
  if(h && h->table) {
467
28.5k
    struct Curl_hash_element *he, **he_anchor;
468
28.5k
    size_t i;
469
28.5k
    DEBUGASSERT(h->init == HASHINIT);
470
1.84M
    for(i = 0; i < h->slots; ++i) {
471
1.81M
      he_anchor = &h->table[i];
472
1.83M
      while(*he_anchor) {
473
23.5k
        he = *he_anchor;
474
23.5k
        hash_elem_unlink(h, he_anchor, he);
475
23.5k
        hash_elem_destroy(h, he);
476
23.5k
      }
477
1.81M
    }
478
28.5k
  }
479
28.5k
}
480
481
size_t Curl_hash_count(struct Curl_hash *h)
482
11.4k
{
483
11.4k
  DEBUGASSERT(h->init == HASHINIT);
484
11.4k
  return h->size;
485
11.4k
}
486
487
/* Cleans all entries that pass the comp function criteria. */
488
void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
489
                                    int (*comp)(void *, void *))
490
11.4k
{
491
11.4k
  size_t i;
492
493
11.4k
  if(!h || !h->table)
494
117
    return;
495
496
11.3k
  DEBUGASSERT(h->init == HASHINIT);
497
819k
  for(i = 0; i < h->slots; ++i) {
498
807k
    struct Curl_hash_element *he, **he_anchor = &h->table[i];
499
826k
    while(*he_anchor) {
500
      /* ask the callback function if we shall remove this entry or not */
501
18.0k
      if(!comp || comp(user, (*he_anchor)->ptr)) {
502
0
        he = *he_anchor;
503
0
        hash_elem_unlink(h, he_anchor, he);
504
0
        hash_elem_destroy(h, he);
505
0
      }
506
18.0k
      else
507
18.0k
        he_anchor = &(*he_anchor)->next;
508
18.0k
    }
509
807k
  }
510
11.3k
}