Coverage Report

Created: 2026-10-15 06:19

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
163k
#define HASHINIT 0x7017e781
34
0
#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
1
{
61
1
  static bool seeded;
62
1
  unsigned char buf[sizeof(size_t)];
63
1
  size_t seed = 0;
64
1
  size_t i;
65
1
  CURLcode result;
66
67
1
  if(seeded)
68
0
    return CURLE_OK;
69
70
1
  result = Curl_rand_bytes(NULL,
71
1
#ifdef DEBUGBUILD
72
1
                           FALSE,
73
1
#endif
74
1
                           buf, sizeof(buf));
75
1
  if(result)
76
0
    return result;
77
78
9
  for(i = 0; i < sizeof(buf); i++)
79
8
    seed = (seed << 8) | buf[i];
80
81
1
  hash_seed = seed ? seed : 1;
82
1
  seeded = TRUE;
83
1
  return CURLE_OK;
84
1
}
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
94.3k
{
93
94.3k
  const char *key_str = (const char *)key;
94
94.3k
  const char *end = key_str + key_length;
95
94.3k
  size_t h = 5381 ^ seed;
96
97
4.68M
  while(key_str < end) {
98
4.59M
    size_t j = (size_t)*key_str++;
99
4.59M
    h += h << 5;
100
4.59M
    h ^= j;
101
4.59M
  }
102
103
94.3k
  return (h % slots_num);
104
94.3k
}
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
94.3k
{
111
94.3k
  return hash_str_seeded(key, key_length, slots_num, hash_seed);
112
94.3k
}
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
90.5k
{
117
90.5k
  return (char *)he + offsetof(struct Curl_hash_element, key);
118
90.5k
}
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
48.7k
{
148
48.7k
  return (key1_len == key2_len) && !memcmp(key1, key2, key1_len);
149
48.7k
}
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
94.3k
{
158
94.3k
  DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST);
159
94.3k
  if((unsigned int)type >= CURL_HASH_TYPE_LAST)
160
0
    type = CURL_HASH_TYPE_BYTES;
161
94.3k
  return &hash_functions[type];
162
94.3k
}
163
164
static bool hash_key_is_valid(uint8_t type, size_t key_len)
165
203k
{
166
203k
  return (type == CURL_HASH_TYPE_BYTES) ||
167
22.2k
    ((type == CURL_HASH_TYPE_SOCKET) &&
168
22.2k
     (key_len == sizeof(curl_socket_t)));
169
203k
}
170
171
#if 0 /* useful function for debugging hashes and their contents */
172
void Curl_hash_print(struct Curl_hash *h, void (*func)(void *))
173
{
174
  struct Curl_hash_iterator iter;
175
  struct Curl_hash_element *he;
176
  size_t last_index = UINT_MAX;
177
178
  if(!h)
179
    return;
180
181
  curl_mfprintf(stderr, "=Hash dump=\n");
182
183
  Curl_hash_start_iterate(h, &iter);
184
185
  he = Curl_hash_next_element(&iter);
186
  while(he) {
187
    if(iter.slot_index != last_index) {
188
      curl_mfprintf(stderr, "index %d:", (int)iter.slot_index);
189
      if(last_index != UINT_MAX) {
190
        curl_mfprintf(stderr, "\n");
191
      }
192
      last_index = iter.slot_index;
193
    }
194
195
    if(func)
196
      func(he->ptr);
197
    else
198
      curl_mfprintf(stderr, " [key=%.*s, he=%p, ptr=%p]",
199
                    (int)he->key_len, hash_elem_key(he),
200
                    (void *)he, (void *)he->ptr);
201
202
    he = Curl_hash_next_element(&iter);
203
  }
204
  curl_mfprintf(stderr, "\n");
205
}
206
#endif
207
208
/* Initializes a hash structure.
209
 *
210
 * @unittest: 1602
211
 * @unittest: 1603
212
 */
213
void Curl_hash_init(struct Curl_hash *h,
214
                    size_t slots,
215
                    Curl_hash_type type,
216
                    Curl_hash_dtor dtor)
217
163k
{
218
163k
  DEBUGASSERT(h);
219
163k
  DEBUGASSERT(slots);
220
163k
  DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST);
221
163k
  DEBUGASSERT(dtor);
222
223
163k
  h->table = NULL;
224
163k
  h->dtor = dtor;
225
163k
  h->size = 0;
226
163k
  h->slots = slots;
227
163k
  h->type = (uint8_t)type;
228
163k
#ifdef DEBUGBUILD
229
163k
  h->init = HASHINIT;
230
163k
#endif
231
163k
}
232
233
static struct Curl_hash_element *hash_elem_create(const void *key,
234
                                                  size_t key_len,
235
                                                  void *p,
236
                                                  Curl_hash_elem_dtor dtor)
237
41.6k
{
238
41.6k
  struct Curl_hash_element *he;
239
240
  /* allocate the struct plus memory after it to store the key */
241
41.6k
  he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len);
242
41.6k
  if(he) {
243
41.6k
    he->next = NULL;
244
    /* copy the key */
245
41.6k
    memcpy(hash_elem_key(he), key, key_len);
246
41.6k
    he->key_len = key_len;
247
41.6k
    he->ptr = p;
248
41.6k
    he->dtor = dtor;
249
41.6k
  }
250
41.6k
  return he;
251
41.6k
}
252
253
static void hash_elem_clear_ptr(struct Curl_hash *h,
254
                                struct Curl_hash_element *he)
255
41.6k
{
256
41.6k
  DEBUGASSERT(h);
257
41.6k
  DEBUGASSERT(he);
258
41.6k
  if(he->ptr) {
259
41.6k
    if(he->dtor)
260
119
      he->dtor(hash_elem_key(he), he->key_len, he->ptr);
261
41.5k
    else
262
41.5k
      h->dtor(he->ptr);
263
41.6k
    he->ptr = NULL;
264
41.6k
  }
265
41.6k
}
266
267
static void hash_elem_destroy(struct Curl_hash *h,
268
                              struct Curl_hash_element *he)
269
41.6k
{
270
41.6k
  hash_elem_clear_ptr(h, he);
271
41.6k
  curlx_free(he);
272
41.6k
}
273
274
static void hash_elem_unlink(struct Curl_hash *h,
275
                             struct Curl_hash_element **he_anchor,
276
                             struct Curl_hash_element *he)
277
41.6k
{
278
41.6k
  *he_anchor = he->next;
279
41.6k
  --h->size;
280
41.6k
}
281
282
static void hash_elem_link(struct Curl_hash *h,
283
                           struct Curl_hash_element **he_anchor,
284
                           struct Curl_hash_element *he)
285
41.6k
{
286
41.6k
  he->next = *he_anchor;
287
41.6k
  *he_anchor = he;
288
41.6k
  ++h->size;
289
41.6k
}
290
291
void *Curl_hash_add2(struct Curl_hash *h,
292
                     const void *key, size_t key_len, void *p,
293
                     Curl_hash_elem_dtor dtor)
294
41.6k
{
295
41.6k
  const struct hash_functions *functions;
296
41.6k
  struct Curl_hash_element *he, **slot;
297
298
41.6k
  DEBUGASSERT(h);
299
41.6k
  DEBUGASSERT(h->slots);
300
41.6k
  DEBUGASSERT(h->init == HASHINIT);
301
41.6k
  if(!hash_key_is_valid(h->type, key_len))
302
0
    return NULL;
303
41.6k
  if(!h->table) {
304
38.4k
    h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *));
305
38.4k
    if(!h->table)
306
0
      return NULL; /* OOM */
307
38.4k
  }
308
309
41.6k
  functions = hash_get_functions(h->type);
310
41.6k
  slot = &h->table[functions->hash(key, key_len, h->slots)];
311
41.6k
  for(he = *slot; he; he = he->next) {
312
0
    if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
313
      /* existing key entry, overwrite by clearing old pointer */
314
0
      hash_elem_clear_ptr(h, he);
315
0
      he->ptr = p;
316
0
      he->dtor = dtor;
317
0
      return p;
318
0
    }
319
0
  }
320
321
41.6k
  he = hash_elem_create(key, key_len, p, dtor);
322
41.6k
  if(!he)
323
0
    return NULL; /* OOM */
324
325
41.6k
  hash_elem_link(h, slot, he);
326
41.6k
  return p; /* return the new entry */
327
41.6k
}
328
329
/* Insert the data in the hash. If there already was a match in the hash, that
330
 * data is replaced. This function also "lazily" allocates the table if
331
 * needed, as it is not done in the _init function (anymore).
332
 *
333
 * @unittest: 1305
334
 * @unittest: 1602
335
 * @unittest: 1603
336
 */
337
void *Curl_hash_add(struct Curl_hash *h,
338
                    const void *key, size_t key_len, void *p)
339
41.5k
{
340
41.5k
  return Curl_hash_add2(h, key, key_len, p, NULL);
341
41.5k
}
342
343
/* Remove the identified hash entry.
344
 * Returns non-zero on failure.
345
 *
346
 * @unittest: 1603
347
 */
348
int Curl_hash_delete(struct Curl_hash *h, const void *key, size_t key_len)
349
68.1k
{
350
68.1k
  const struct hash_functions *functions;
351
352
68.1k
  DEBUGASSERT(h);
353
68.1k
  DEBUGASSERT(h->slots);
354
68.1k
  DEBUGASSERT(h->init == HASHINIT);
355
68.1k
  if(!hash_key_is_valid(h->type, key_len))
356
0
    return 1;
357
68.1k
  if(h->table) {
358
22.3k
    struct Curl_hash_element *he, **he_anchor;
359
360
22.3k
    functions = hash_get_functions(h->type);
361
22.3k
    he_anchor = &h->table[functions->hash(key, key_len, h->slots)];
362
22.3k
    while(*he_anchor) {
363
22.2k
      he = *he_anchor;
364
22.2k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
365
22.2k
        hash_elem_unlink(h, he_anchor, he);
366
22.2k
        hash_elem_destroy(h, he);
367
22.2k
        return 0;
368
22.2k
      }
369
0
      he_anchor = &he->next;
370
0
    }
371
22.3k
  }
372
45.9k
  return 1;
373
68.1k
}
374
375
/* Retrieves a hash element.
376
 *
377
 * @unittest: 1603
378
 */
379
void *Curl_hash_pick(struct Curl_hash *h, const void *key, size_t key_len)
380
93.2k
{
381
93.2k
  const struct hash_functions *functions;
382
383
93.2k
  DEBUGASSERT(h);
384
93.2k
  DEBUGASSERT(h->init == HASHINIT);
385
93.2k
  if(!hash_key_is_valid(h->type, key_len))
386
0
    return NULL;
387
93.2k
  if(h->table) {
388
30.3k
    struct Curl_hash_element *he;
389
30.3k
    DEBUGASSERT(h->slots);
390
30.3k
    functions = hash_get_functions(h->type);
391
30.3k
    he = h->table[functions->hash(key, key_len, h->slots)];
392
30.3k
    while(he) {
393
26.5k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
394
26.4k
        return he->ptr;
395
26.4k
      }
396
18
      he = he->next;
397
18
    }
398
30.3k
  }
399
66.7k
  return NULL;
400
93.2k
}
401
402
/* Destroys all the entries in the given hash and resets its attributes,
403
 * prepping the given hash for [static|dynamic] deallocation.
404
 *
405
 * @unittest: 1305
406
 * @unittest: 1602
407
 * @unittest: 1603
408
 */
409
void Curl_hash_destroy(struct Curl_hash *h)
410
163k
{
411
163k
  DEBUGASSERT(h->init == HASHINIT);
412
163k
  if(h->table) {
413
38.4k
    Curl_hash_clean(h);
414
38.4k
    curlx_safefree(h->table);
415
38.4k
  }
416
163k
  DEBUGASSERT(h->size == 0);
417
163k
  h->slots = 0;
418
163k
}
419
420
/* Removes all the entries in the given hash.
421
 *
422
 * @unittest: 1602
423
 */
424
void Curl_hash_clean(struct Curl_hash *h)
425
38.4k
{
426
38.4k
  if(h && h->table) {
427
38.4k
    struct Curl_hash_element *he, **he_anchor;
428
38.4k
    size_t i;
429
38.4k
    DEBUGASSERT(h->init == HASHINIT);
430
3.26M
    for(i = 0; i < h->slots; ++i) {
431
3.22M
      he_anchor = &h->table[i];
432
3.24M
      while(*he_anchor) {
433
19.4k
        he = *he_anchor;
434
19.4k
        hash_elem_unlink(h, he_anchor, he);
435
19.4k
        hash_elem_destroy(h, he);
436
19.4k
      }
437
3.22M
    }
438
38.4k
  }
439
38.4k
}
440
441
size_t Curl_hash_count(struct Curl_hash *h)
442
22.6k
{
443
22.6k
  DEBUGASSERT(h->init == HASHINIT);
444
22.6k
  return h->size;
445
22.6k
}
446
447
/* Cleans all entries that pass the comp function criteria. */
448
void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
449
                                    int (*comp)(void *, void *))
450
22.6k
{
451
22.6k
  size_t i;
452
453
22.6k
  if(!h || !h->table)
454
146
    return;
455
456
22.5k
  DEBUGASSERT(h->init == HASHINIT);
457
1.62M
  for(i = 0; i < h->slots; ++i) {
458
1.60M
    struct Curl_hash_element *he, **he_anchor = &h->table[i];
459
1.62M
    while(*he_anchor) {
460
      /* ask the callback function if we shall remove this entry or not */
461
22.7k
      if(!comp || comp(user, (*he_anchor)->ptr)) {
462
19
        he = *he_anchor;
463
19
        hash_elem_unlink(h, he_anchor, he);
464
19
        hash_elem_destroy(h, he);
465
19
      }
466
22.7k
      else
467
22.7k
        he_anchor = &(*he_anchor)->next;
468
22.7k
    }
469
1.60M
  }
470
22.5k
}
471
472
void Curl_hash_start_iterate(struct Curl_hash *hash,
473
                             struct Curl_hash_iterator *iter)
474
0
{
475
0
  DEBUGASSERT(hash->init == HASHINIT);
476
0
  iter->hash = hash;
477
0
  iter->slot_index = 0;
478
0
  iter->current = NULL;
479
0
#ifdef DEBUGBUILD
480
0
  iter->init = ITERINIT;
481
0
#endif
482
0
}
483
484
struct Curl_hash_element *Curl_hash_next_element(
485
  struct Curl_hash_iterator *iter)
486
0
{
487
0
  struct Curl_hash *h;
488
0
  DEBUGASSERT(iter->init == ITERINIT);
489
0
  h = iter->hash;
490
0
  if(!h->table)
491
0
    return NULL; /* empty hash, nothing to return */
492
493
  /* Get the next element in the current list, if any */
494
0
  if(iter->current)
495
0
    iter->current = iter->current->next;
496
497
  /* If we have reached the end of the list, find the next one */
498
0
  if(!iter->current) {
499
0
    size_t i;
500
0
    for(i = iter->slot_index; i < h->slots; i++) {
501
0
      if(h->table[i]) {
502
0
        iter->current = h->table[i];
503
0
        iter->slot_index = i + 1;
504
0
        break;
505
0
      }
506
0
    }
507
0
  }
508
509
0
  return iter->current;
510
0
}