Coverage Report

Created: 2026-10-15 06:17

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
87.2k
#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
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
102k
{
93
102k
  const char *key_str = (const char *)key;
94
102k
  const char *end = key_str + key_length;
95
102k
  size_t h = 5381 ^ seed;
96
97
1.99M
  while(key_str < end) {
98
1.89M
    size_t j = (size_t)*key_str++;
99
1.89M
    h += h << 5;
100
1.89M
    h ^= j;
101
1.89M
  }
102
103
102k
  return (h % slots_num);
104
102k
}
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
102k
{
111
102k
  return hash_str_seeded(key, key_length, slots_num, hash_seed);
112
102k
}
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
102k
{
117
102k
  return (char *)he + offsetof(struct Curl_hash_element, key);
118
102k
}
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
63.1k
{
148
63.1k
  return (key1_len == key2_len) && !memcmp(key1, key2, key1_len);
149
63.1k
}
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
102k
{
158
102k
  DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST);
159
102k
  if((unsigned int)type >= CURL_HASH_TYPE_LAST)
160
0
    type = CURL_HASH_TYPE_BYTES;
161
102k
  return &hash_functions[type];
162
102k
}
163
164
static bool hash_key_is_valid(uint8_t type, size_t key_len)
165
166k
{
166
166k
  return (type == CURL_HASH_TYPE_BYTES) ||
167
9.99k
    ((type == CURL_HASH_TYPE_SOCKET) &&
168
9.99k
     (key_len == sizeof(curl_socket_t)));
169
166k
}
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
87.2k
{
218
87.2k
  DEBUGASSERT(h);
219
87.2k
  DEBUGASSERT(slots);
220
87.2k
  DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST);
221
87.2k
  DEBUGASSERT(dtor);
222
223
87.2k
  h->table = NULL;
224
87.2k
  h->dtor = dtor;
225
87.2k
  h->size = 0;
226
87.2k
  h->slots = slots;
227
87.2k
  h->type = (uint8_t)type;
228
87.2k
#ifdef DEBUGBUILD
229
87.2k
  h->init = HASHINIT;
230
87.2k
#endif
231
87.2k
}
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
29.6k
{
238
29.6k
  struct Curl_hash_element *he;
239
240
  /* allocate the struct plus memory after it to store the key */
241
29.6k
  he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len);
242
29.6k
  if(he) {
243
29.6k
    he->next = NULL;
244
    /* copy the key */
245
29.6k
    memcpy(hash_elem_key(he), key, key_len);
246
29.6k
    he->key_len = key_len;
247
29.6k
    he->ptr = p;
248
29.6k
    he->dtor = dtor;
249
29.6k
  }
250
29.6k
  return he;
251
29.6k
}
252
253
static void hash_elem_clear_ptr(struct Curl_hash *h,
254
                                struct Curl_hash_element *he)
255
29.6k
{
256
29.6k
  DEBUGASSERT(h);
257
29.6k
  DEBUGASSERT(he);
258
29.6k
  if(he->ptr) {
259
29.6k
    if(he->dtor)
260
9.44k
      he->dtor(hash_elem_key(he), he->key_len, he->ptr);
261
20.1k
    else
262
20.1k
      h->dtor(he->ptr);
263
29.6k
    he->ptr = NULL;
264
29.6k
  }
265
29.6k
}
266
267
static void hash_elem_destroy(struct Curl_hash *h,
268
                              struct Curl_hash_element *he)
269
29.6k
{
270
29.6k
  hash_elem_clear_ptr(h, he);
271
29.6k
  curlx_free(he);
272
29.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
29.6k
{
278
29.6k
  *he_anchor = he->next;
279
29.6k
  --h->size;
280
29.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
29.6k
{
286
29.6k
  he->next = *he_anchor;
287
29.6k
  *he_anchor = he;
288
29.6k
  ++h->size;
289
29.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
29.6k
{
295
29.6k
  const struct hash_functions *functions;
296
29.6k
  struct Curl_hash_element *he, **slot;
297
298
29.6k
  DEBUGASSERT(h);
299
29.6k
  DEBUGASSERT(h->slots);
300
29.6k
  DEBUGASSERT(h->init == HASHINIT);
301
29.6k
  if(!hash_key_is_valid(h->type, key_len))
302
0
    return NULL;
303
29.6k
  if(!h->table) {
304
29.6k
    h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *));
305
29.6k
    if(!h->table)
306
4
      return NULL; /* OOM */
307
29.6k
  }
308
309
29.6k
  functions = hash_get_functions(h->type);
310
29.6k
  slot = &h->table[functions->hash(key, key_len, h->slots)];
311
29.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
29.6k
  he = hash_elem_create(key, key_len, p, dtor);
322
29.6k
  if(!he)
323
5
    return NULL; /* OOM */
324
325
29.6k
  hash_elem_link(h, slot, he);
326
29.6k
  return p; /* return the new entry */
327
29.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
20.2k
{
340
20.2k
  return Curl_hash_add2(h, key, key_len, p, NULL);
341
20.2k
}
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
33.2k
{
350
33.2k
  const struct hash_functions *functions;
351
352
33.2k
  DEBUGASSERT(h);
353
33.2k
  DEBUGASSERT(h->slots);
354
33.2k
  DEBUGASSERT(h->init == HASHINIT);
355
33.2k
  if(!hash_key_is_valid(h->type, key_len))
356
0
    return 1;
357
33.2k
  if(h->table) {
358
19.6k
    struct Curl_hash_element *he, **he_anchor;
359
360
19.6k
    functions = hash_get_functions(h->type);
361
19.6k
    he_anchor = &h->table[functions->hash(key, key_len, h->slots)];
362
19.6k
    while(*he_anchor) {
363
10.1k
      he = *he_anchor;
364
10.1k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
365
10.1k
        hash_elem_unlink(h, he_anchor, he);
366
10.1k
        hash_elem_destroy(h, he);
367
10.1k
        return 0;
368
10.1k
      }
369
0
      he_anchor = &he->next;
370
0
    }
371
19.6k
  }
372
23.0k
  return 1;
373
33.2k
}
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
103k
{
381
103k
  const struct hash_functions *functions;
382
383
103k
  DEBUGASSERT(h);
384
103k
  DEBUGASSERT(h->init == HASHINIT);
385
103k
  if(!hash_key_is_valid(h->type, key_len))
386
0
    return NULL;
387
103k
  if(h->table) {
388
53.0k
    struct Curl_hash_element *he;
389
53.0k
    DEBUGASSERT(h->slots);
390
53.0k
    functions = hash_get_functions(h->type);
391
53.0k
    he = h->table[functions->hash(key, key_len, h->slots)];
392
53.0k
    while(he) {
393
53.0k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
394
53.0k
        return he->ptr;
395
53.0k
      }
396
0
      he = he->next;
397
0
    }
398
53.0k
  }
399
50.6k
  return NULL;
400
103k
}
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
87.2k
{
411
87.2k
  DEBUGASSERT(h->init == HASHINIT);
412
87.2k
  if(h->table) {
413
29.6k
    Curl_hash_clean(h);
414
29.6k
    curlx_safefree(h->table);
415
29.6k
  }
416
87.2k
  DEBUGASSERT(h->size == 0);
417
87.2k
  h->slots = 0;
418
87.2k
}
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
29.6k
{
426
29.6k
  if(h && h->table) {
427
29.6k
    struct Curl_hash_element *he, **he_anchor;
428
29.6k
    size_t i;
429
29.6k
    DEBUGASSERT(h->init == HASHINIT);
430
1.94M
    for(i = 0; i < h->slots; ++i) {
431
1.91M
      he_anchor = &h->table[i];
432
1.93M
      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
1.91M
    }
438
29.6k
  }
439
29.6k
}
440
441
size_t Curl_hash_count(struct Curl_hash *h)
442
10.1k
{
443
10.1k
  DEBUGASSERT(h->init == HASHINIT);
444
10.1k
  return h->size;
445
10.1k
}
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
10.1k
{
451
10.1k
  size_t i;
452
453
10.1k
  if(!h || !h->table)
454
149
    return;
455
456
10.0k
  DEBUGASSERT(h->init == HASHINIT);
457
720k
  for(i = 0; i < h->slots; ++i) {
458
710k
    struct Curl_hash_element *he, **he_anchor = &h->table[i];
459
720k
    while(*he_anchor) {
460
      /* ask the callback function if we shall remove this entry or not */
461
10.0k
      if(!comp || comp(user, (*he_anchor)->ptr)) {
462
0
        he = *he_anchor;
463
0
        hash_elem_unlink(h, he_anchor, he);
464
0
        hash_elem_destroy(h, he);
465
0
      }
466
10.0k
      else
467
10.0k
        he_anchor = &(*he_anchor)->next;
468
10.0k
    }
469
710k
  }
470
10.0k
}
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
}