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
179k
#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
164k
{
93
164k
  const char *key_str = (const char *)key;
94
164k
  const char *end = key_str + key_length;
95
164k
  size_t h = 5381 ^ seed;
96
97
2.44M
  while(key_str < end) {
98
2.28M
    size_t j = (size_t)*key_str++;
99
2.28M
    h += h << 5;
100
2.28M
    h ^= j;
101
2.28M
  }
102
103
164k
  return (h % slots_num);
104
164k
}
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
164k
{
111
164k
  return hash_str_seeded(key, key_length, slots_num, hash_seed);
112
164k
}
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
203k
{
117
203k
  return (char *)he + offsetof(struct Curl_hash_element, key);
118
203k
}
119
120
static size_t hash_socket(const void *key, size_t key_length,
121
                          size_t slots_num)
122
57.0k
{
123
57.0k
  curl_socket_t socket;
124
125
57.0k
  DEBUGASSERT(key_length == sizeof(socket));
126
57.0k
  if(key_length != sizeof(socket))
127
0
    return 0;
128
57.0k
  memcpy(&socket, key, sizeof(socket));
129
57.0k
  return (size_t)(socket % (curl_socket_t)slots_num);
130
57.0k
}
131
132
static size_t compare_socket(const void *key1, size_t key1_len,
133
                             const void *key2, size_t key2_len)
134
52.9k
{
135
52.9k
  curl_socket_t socket1;
136
52.9k
  curl_socket_t socket2;
137
138
52.9k
  if((key1_len != sizeof(socket1)) || (key2_len != sizeof(socket2)))
139
0
    return 0;
140
52.9k
  memcpy(&socket1, key1, sizeof(socket1));
141
52.9k
  memcpy(&socket2, key2, sizeof(socket2));
142
52.9k
  return socket1 == socket2;
143
52.9k
}
144
145
static size_t compare_bytes(const void *key1, size_t key1_len,
146
                            const void *key2, size_t key2_len)
147
95.6k
{
148
95.6k
  return (key1_len == key2_len) && !memcmp(key1, key2, key1_len);
149
95.6k
}
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
221k
{
158
221k
  DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST);
159
221k
  if((unsigned int)type >= CURL_HASH_TYPE_LAST)
160
0
    type = CURL_HASH_TYPE_BYTES;
161
221k
  return &hash_functions[type];
162
221k
}
163
164
static bool hash_key_is_valid(uint8_t type, size_t key_len)
165
371k
{
166
371k
  return (type == CURL_HASH_TYPE_BYTES) ||
167
85.4k
    ((type == CURL_HASH_TYPE_SOCKET) &&
168
85.4k
     (key_len == sizeof(curl_socket_t)));
169
371k
}
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
179k
{
218
179k
  DEBUGASSERT(h);
219
179k
  DEBUGASSERT(slots);
220
179k
  DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST);
221
179k
  DEBUGASSERT(dtor);
222
223
179k
  h->table = NULL;
224
179k
  h->dtor = dtor;
225
179k
  h->size = 0;
226
179k
  h->slots = slots;
227
179k
  h->type = (uint8_t)type;
228
179k
#ifdef DEBUGBUILD
229
179k
  h->init = HASHINIT;
230
179k
#endif
231
179k
}
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
53.6k
{
238
53.6k
  struct Curl_hash_element *he;
239
240
  /* allocate the struct plus memory after it to store the key */
241
53.6k
  he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len);
242
53.6k
  if(he) {
243
53.6k
    he->next = NULL;
244
    /* copy the key */
245
53.6k
    memcpy(hash_elem_key(he), key, key_len);
246
53.6k
    he->key_len = key_len;
247
53.6k
    he->ptr = p;
248
53.6k
    he->dtor = dtor;
249
53.6k
  }
250
53.6k
  return he;
251
53.6k
}
252
253
static void hash_elem_clear_ptr(struct Curl_hash *h,
254
                                struct Curl_hash_element *he)
255
53.6k
{
256
53.6k
  DEBUGASSERT(h);
257
53.6k
  DEBUGASSERT(he);
258
53.6k
  if(he->ptr) {
259
53.6k
    if(he->dtor)
260
1.34k
      he->dtor(hash_elem_key(he), he->key_len, he->ptr);
261
52.2k
    else
262
52.2k
      h->dtor(he->ptr);
263
53.6k
    he->ptr = NULL;
264
53.6k
  }
265
53.6k
}
266
267
static void hash_elem_destroy(struct Curl_hash *h,
268
                              struct Curl_hash_element *he)
269
53.6k
{
270
53.6k
  hash_elem_clear_ptr(h, he);
271
53.6k
  curlx_free(he);
272
53.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
53.6k
{
278
53.6k
  *he_anchor = he->next;
279
53.6k
  --h->size;
280
53.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
53.6k
{
286
53.6k
  he->next = *he_anchor;
287
53.6k
  *he_anchor = he;
288
53.6k
  ++h->size;
289
53.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
53.6k
{
295
53.6k
  const struct hash_functions *functions;
296
53.6k
  struct Curl_hash_element *he, **slot;
297
298
53.6k
  DEBUGASSERT(h);
299
53.6k
  DEBUGASSERT(h->slots);
300
53.6k
  DEBUGASSERT(h->init == HASHINIT);
301
53.6k
  if(!hash_key_is_valid(h->type, key_len))
302
0
    return NULL;
303
53.6k
  if(!h->table) {
304
43.9k
    h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *));
305
43.9k
    if(!h->table)
306
2
      return NULL; /* OOM */
307
43.9k
  }
308
309
53.6k
  functions = hash_get_functions(h->type);
310
53.6k
  slot = &h->table[functions->hash(key, key_len, h->slots)];
311
53.6k
  for(he = *slot; he; he = he->next) {
312
57
    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
57
  }
320
321
53.6k
  he = hash_elem_create(key, key_len, p, dtor);
322
53.6k
  if(!he)
323
13
    return NULL; /* OOM */
324
325
53.6k
  hash_elem_link(h, slot, he);
326
53.6k
  return p; /* return the new entry */
327
53.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
52.3k
{
340
52.3k
  return Curl_hash_add2(h, key, key_len, p, NULL);
341
52.3k
}
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
85.1k
{
350
85.1k
  const struct hash_functions *functions;
351
352
85.1k
  DEBUGASSERT(h);
353
85.1k
  DEBUGASSERT(h->slots);
354
85.1k
  DEBUGASSERT(h->init == HASHINIT);
355
85.1k
  if(!hash_key_is_valid(h->type, key_len))
356
0
    return 1;
357
85.1k
  if(h->table) {
358
31.9k
    struct Curl_hash_element *he, **he_anchor;
359
360
31.9k
    functions = hash_get_functions(h->type);
361
31.9k
    he_anchor = &h->table[functions->hash(key, key_len, h->slots)];
362
31.9k
    while(*he_anchor) {
363
31.9k
      he = *he_anchor;
364
31.9k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
365
31.9k
        hash_elem_unlink(h, he_anchor, he);
366
31.9k
        hash_elem_destroy(h, he);
367
31.9k
        return 0;
368
31.9k
      }
369
35
      he_anchor = &he->next;
370
35
    }
371
31.9k
  }
372
53.1k
  return 1;
373
85.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
232k
{
381
232k
  const struct hash_functions *functions;
382
383
232k
  DEBUGASSERT(h);
384
232k
  DEBUGASSERT(h->init == HASHINIT);
385
232k
  if(!hash_key_is_valid(h->type, key_len))
386
0
    return NULL;
387
232k
  if(h->table) {
388
136k
    struct Curl_hash_element *he;
389
136k
    DEBUGASSERT(h->slots);
390
136k
    functions = hash_get_functions(h->type);
391
136k
    he = h->table[functions->hash(key, key_len, h->slots)];
392
136k
    while(he) {
393
116k
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
394
115k
        return he->ptr;
395
115k
      }
396
812
      he = he->next;
397
812
    }
398
136k
  }
399
116k
  return NULL;
400
232k
}
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
179k
{
411
179k
  DEBUGASSERT(h->init == HASHINIT);
412
179k
  if(h->table) {
413
43.9k
    Curl_hash_clean(h);
414
43.9k
    curlx_safefree(h->table);
415
43.9k
  }
416
179k
  DEBUGASSERT(h->size == 0);
417
179k
  h->slots = 0;
418
179k
}
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
53.7k
{
426
53.7k
  if(h && h->table) {
427
43.9k
    struct Curl_hash_element *he, **he_anchor;
428
43.9k
    size_t i;
429
43.9k
    DEBUGASSERT(h->init == HASHINIT);
430
3.84M
    for(i = 0; i < h->slots; ++i) {
431
3.79M
      he_anchor = &h->table[i];
432
3.81M
      while(*he_anchor) {
433
21.7k
        he = *he_anchor;
434
21.7k
        hash_elem_unlink(h, he_anchor, he);
435
21.7k
        hash_elem_destroy(h, he);
436
21.7k
      }
437
3.79M
    }
438
43.9k
  }
439
53.7k
}
440
441
size_t Curl_hash_count(struct Curl_hash *h)
442
32.0k
{
443
32.0k
  DEBUGASSERT(h->init == HASHINIT);
444
32.0k
  return h->size;
445
32.0k
}
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
32.0k
{
451
32.0k
  size_t i;
452
453
32.0k
  if(!h || !h->table)
454
116
    return;
455
456
31.9k
  DEBUGASSERT(h->init == HASHINIT);
457
2.14M
  for(i = 0; i < h->slots; ++i) {
458
2.11M
    struct Curl_hash_element *he, **he_anchor = &h->table[i];
459
2.14M
    while(*he_anchor) {
460
      /* ask the callback function if we shall remove this entry or not */
461
32.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
32.0k
      else
467
32.0k
        he_anchor = &(*he_anchor)->next;
468
32.0k
    }
469
2.11M
  }
470
31.9k
}
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
}