Coverage Report

Created: 2026-10-30 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/gl/gl_array_list.c
Line
Count
Source
1
/* Sequential list data type implemented by an array.
2
   Copyright (C) 2006-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2006.
4
5
   This file is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as
7
   published by the Free Software Foundation; either version 2.1 of the
8
   License, or (at your option) any later version.
9
10
   This file 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
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17
18
#include <config.h>
19
20
/* Specification.  */
21
#include "gl_array_list.h"
22
23
#include <stdint.h>
24
#include <stdlib.h>
25
/* Get memcpy.  */
26
#include <string.h>
27
28
/* Checked size_t computations.  */
29
#include "xsize.h"
30
31
/* -------------------------- gl_list_t Data Type -------------------------- */
32
33
/* Concrete gl_list_impl type, valid for this file only.  */
34
struct gl_list_impl
35
{
36
  struct gl_list_impl_base base;
37
  size_t count;
38
  /* An array of ALLOCATED elements, of which the first COUNT are used.
39
     0 <= COUNT <= ALLOCATED.  */
40
  const void **elements
41
    _GL_ATTRIBUTE_COUNTED_BY (count);
42
  size_t allocated;
43
};
44
45
/* struct gl_list_node_impl doesn't exist here.  The pointers are actually
46
   indices + 1.  */
47
0
#define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t) {(index) + 1}
48
0
#define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
49
50
static gl_list_t
51
gl_array_nx_create_empty (gl_list_implementation_t implementation,
52
                          gl_listelement_equals_fn equals_fn,
53
                          gl_listelement_hashcode_fn hashcode_fn,
54
                          gl_listelement_dispose_fn dispose_fn,
55
                          bool allow_duplicates)
56
0
{
57
0
  struct gl_list_impl *list =
58
0
    (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
59
60
0
  if (list == NULL)
61
0
    return NULL;
62
63
0
  list->base.vtable = implementation;
64
0
  list->base.equals_fn = equals_fn;
65
0
  list->base.hashcode_fn = hashcode_fn;
66
0
  list->base.dispose_fn = dispose_fn;
67
0
  list->base.allow_duplicates = allow_duplicates;
68
0
  list->elements = NULL;
69
0
  list->count = 0;
70
0
  list->allocated = 0;
71
72
0
  return list;
73
0
}
74
75
static gl_list_t
76
gl_array_nx_create (gl_list_implementation_t implementation,
77
                    gl_listelement_equals_fn equals_fn,
78
                    gl_listelement_hashcode_fn hashcode_fn,
79
                    gl_listelement_dispose_fn dispose_fn,
80
                    bool allow_duplicates,
81
                    size_t count, const void **contents)
82
0
{
83
0
  struct gl_list_impl *list =
84
0
    (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
85
86
0
  if (list == NULL)
87
0
    return NULL;
88
89
0
  list->base.vtable = implementation;
90
0
  list->base.equals_fn = equals_fn;
91
0
  list->base.hashcode_fn = hashcode_fn;
92
0
  list->base.dispose_fn = dispose_fn;
93
0
  list->base.allow_duplicates = allow_duplicates;
94
0
  if (count > 0)
95
0
    {
96
0
      if (size_overflow_p (xtimes (count, sizeof (const void *))))
97
0
        goto fail;
98
0
      list->elements = (const void **) malloc (count * sizeof (const void *));
99
0
      if (list->elements == NULL)
100
0
        goto fail;
101
0
      memcpy (list->elements, contents, count * sizeof (const void *));
102
0
    }
103
0
  else
104
0
    list->elements = NULL;
105
0
  list->count = count;
106
0
  list->allocated = count;
107
108
0
  return list;
109
110
0
 fail:
111
0
  free (list);
112
0
  return NULL;
113
0
}
114
115
static size_t _GL_ATTRIBUTE_PURE
116
gl_array_size (gl_list_t list)
117
0
{
118
0
  return list->count;
119
0
}
120
121
static const void * _GL_ATTRIBUTE_PURE
122
gl_array_node_value (gl_list_t list, gl_list_node_t node)
123
0
{
124
0
  uintptr_t index = NODE_TO_INDEX (node);
125
0
  if (!(index < list->count))
126
    /* Invalid argument.  */
127
0
    abort ();
128
0
  return list->elements[index];
129
0
}
130
131
static int
132
gl_array_node_nx_set_value (gl_list_t list, gl_list_node_t node,
133
                            const void *elt)
134
0
{
135
0
  uintptr_t index = NODE_TO_INDEX (node);
136
0
  if (!(index < list->count))
137
    /* Invalid argument.  */
138
0
    abort ();
139
0
  list->elements[index] = elt;
140
0
  return 0;
141
0
}
142
143
static gl_list_node_t _GL_ATTRIBUTE_PURE
144
gl_array_next_node (gl_list_t list, gl_list_node_t node)
145
0
{
146
0
  uintptr_t index = NODE_TO_INDEX (node);
147
0
  if (!(index < list->count))
148
    /* Invalid argument.  */
149
0
    abort ();
150
0
  index++;
151
0
  if (index < list->count)
152
0
    return INDEX_TO_NODE (index);
153
0
  else
154
0
    return NULL;
155
0
}
156
157
static gl_list_node_t _GL_ATTRIBUTE_PURE
158
gl_array_previous_node (gl_list_t list, gl_list_node_t node)
159
0
{
160
0
  uintptr_t index = NODE_TO_INDEX (node);
161
0
  if (!(index < list->count))
162
    /* Invalid argument.  */
163
0
    abort ();
164
0
  if (index > 0)
165
0
    return INDEX_TO_NODE (index - 1);
166
0
  else
167
0
    return NULL;
168
0
}
169
170
static gl_list_node_t _GL_ATTRIBUTE_PURE
171
gl_array_first_node (gl_list_t list)
172
0
{
173
0
  if (list->count > 0)
174
0
    return INDEX_TO_NODE (0);
175
0
  else
176
0
    return NULL;
177
0
}
178
179
static gl_list_node_t _GL_ATTRIBUTE_PURE
180
gl_array_last_node (gl_list_t list)
181
0
{
182
0
  if (list->count > 0)
183
0
    return INDEX_TO_NODE (list->count - 1);
184
0
  else
185
0
    return NULL;
186
0
}
187
188
static const void * _GL_ATTRIBUTE_PURE
189
gl_array_get_at (gl_list_t list, size_t position)
190
0
{
191
0
  size_t count = list->count;
192
193
0
  if (!(position < count))
194
    /* Invalid argument.  */
195
0
    abort ();
196
0
  return list->elements[position];
197
0
}
198
199
static gl_list_node_t
200
gl_array_nx_set_at (gl_list_t list, size_t position, const void *elt)
201
0
{
202
0
  size_t count = list->count;
203
204
0
  if (!(position < count))
205
    /* Invalid argument.  */
206
0
    abort ();
207
0
  list->elements[position] = elt;
208
0
  return INDEX_TO_NODE (position);
209
0
}
210
211
static size_t _GL_ATTRIBUTE_PURE
212
gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
213
                          const void *elt)
214
  _GL_LIST_INVOKES_FN_PTR
215
0
{
216
0
  size_t count = list->count;
217
218
0
  if (!(start_index <= end_index && end_index <= count))
219
    /* Invalid arguments.  */
220
0
    abort ();
221
222
0
  if (start_index < end_index)
223
0
    {
224
0
      gl_listelement_equals_fn equals = list->base.equals_fn;
225
0
      if (equals != NULL)
226
0
        {
227
0
          for (size_t i = start_index;;)
228
0
            {
229
0
              if (equals (elt, list->elements[i]))
230
0
                return i;
231
0
              i++;
232
0
              if (i == end_index)
233
0
                break;
234
0
            }
235
0
        }
236
0
      else
237
0
        {
238
0
          for (size_t i = start_index;;)
239
0
            {
240
0
              if (elt == list->elements[i])
241
0
                return i;
242
0
              i++;
243
0
              if (i == end_index)
244
0
                break;
245
0
            }
246
0
        }
247
0
    }
248
0
  return (size_t)(-1);
249
0
}
250
251
static gl_list_node_t _GL_ATTRIBUTE_PURE
252
gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
253
                         const void *elt)
254
0
{
255
0
  size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
256
0
  return INDEX_TO_NODE (index);
257
0
}
258
259
/* Ensure that list->allocated > list->count.
260
   Return 0 upon success, -1 upon out-of-memory.  */
261
static int
262
grow (gl_list_t list)
263
0
{
264
0
  size_t new_allocated = xtimes (list->allocated, 2);
265
0
  new_allocated = xsum (new_allocated, 1);
266
0
  size_t memory_size = xtimes (new_allocated, sizeof (const void *));
267
0
  if (size_overflow_p (memory_size))
268
    /* Overflow, would lead to out of memory.  */
269
0
    return -1;
270
0
  const void **memory = (const void **) realloc (list->elements, memory_size);
271
0
  if (memory == NULL)
272
    /* Out of memory.  */
273
0
    return -1;
274
0
  list->elements = memory;
275
0
  list->allocated = new_allocated;
276
0
  return 0;
277
0
}
278
279
static gl_list_node_t
280
gl_array_nx_add_first (gl_list_t list, const void *elt)
281
0
{
282
0
  size_t count = list->count;
283
284
0
  if (count == list->allocated)
285
0
    if (grow (list) < 0)
286
0
      return NULL;
287
0
  const void **elements = list->elements;
288
0
  list->count = count + 1;
289
0
  for (size_t i = count; i > 0; i--)
290
0
    elements[i] = elements[i - 1];
291
0
  elements[0] = elt;
292
0
  return INDEX_TO_NODE (0);
293
0
}
294
295
static gl_list_node_t
296
gl_array_nx_add_last (gl_list_t list, const void *elt)
297
0
{
298
0
  size_t count = list->count;
299
300
0
  if (count == list->allocated)
301
0
    if (grow (list) < 0)
302
0
      return NULL;
303
0
  list->count = count + 1;
304
0
  list->elements[count] = elt;
305
0
  return INDEX_TO_NODE (count);
306
0
}
307
308
static gl_list_node_t
309
gl_array_nx_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
310
0
{
311
0
  size_t count = list->count;
312
0
  uintptr_t index = NODE_TO_INDEX (node);
313
314
0
  if (!(index < count))
315
    /* Invalid argument.  */
316
0
    abort ();
317
0
  size_t position = index;
318
0
  if (count == list->allocated)
319
0
    if (grow (list) < 0)
320
0
      return NULL;
321
0
  const void **elements = list->elements;
322
0
  list->count = count + 1;
323
0
  for (size_t i = count; i > position; i--)
324
0
    elements[i] = elements[i - 1];
325
0
  elements[position] = elt;
326
0
  return INDEX_TO_NODE (position);
327
0
}
328
329
static gl_list_node_t
330
gl_array_nx_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
331
0
{
332
0
  size_t count = list->count;
333
0
  uintptr_t index = NODE_TO_INDEX (node);
334
335
0
  if (!(index < count))
336
    /* Invalid argument.  */
337
0
    abort ();
338
0
  size_t position = index + 1;
339
0
  if (count == list->allocated)
340
0
    if (grow (list) < 0)
341
0
      return NULL;
342
0
  const void **elements = list->elements;
343
0
  list->count = count + 1;
344
0
  for (size_t i = count; i > position; i--)
345
0
    elements[i] = elements[i - 1];
346
0
  elements[position] = elt;
347
0
  return INDEX_TO_NODE (position);
348
0
}
349
350
static gl_list_node_t
351
gl_array_nx_add_at (gl_list_t list, size_t position, const void *elt)
352
0
{
353
0
  size_t count = list->count;
354
355
0
  if (!(position <= count))
356
    /* Invalid argument.  */
357
0
    abort ();
358
0
  if (count == list->allocated)
359
0
    if (grow (list) < 0)
360
0
      return NULL;
361
0
  const void **elements = list->elements;
362
0
  list->count = count + 1;
363
0
  for (size_t i = count; i > position; i--)
364
0
    elements[i] = elements[i - 1];
365
0
  elements[position] = elt;
366
0
  return INDEX_TO_NODE (position);
367
0
}
368
369
static bool
370
gl_array_remove_node (gl_list_t list, gl_list_node_t node)
371
  _GL_LIST_INVOKES_FN_PTR
372
0
{
373
0
  size_t count = list->count;
374
0
  uintptr_t index = NODE_TO_INDEX (node);
375
376
0
  if (!(index < count))
377
    /* Invalid argument.  */
378
0
    abort ();
379
0
  size_t position = index;
380
0
  const void **elements = list->elements;
381
0
  if (list->base.dispose_fn != NULL)
382
0
    list->base.dispose_fn (elements[position]);
383
0
  for (size_t i = position + 1; i < count; i++)
384
0
    elements[i - 1] = elements[i];
385
0
  list->count = count - 1;
386
0
  return true;
387
0
}
388
389
static bool
390
gl_array_remove_at (gl_list_t list, size_t position)
391
  _GL_LIST_INVOKES_FN_PTR
392
0
{
393
0
  size_t count = list->count;
394
395
0
  if (!(position < count))
396
    /* Invalid argument.  */
397
0
    abort ();
398
0
  const void **elements = list->elements;
399
0
  if (list->base.dispose_fn != NULL)
400
0
    list->base.dispose_fn (elements[position]);
401
0
  for (size_t i = position + 1; i < count; i++)
402
0
    elements[i - 1] = elements[i];
403
0
  list->count = count - 1;
404
0
  return true;
405
0
}
406
407
static bool
408
gl_array_remove (gl_list_t list, const void *elt)
409
0
{
410
0
  size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
411
0
  if (position == (size_t)(-1))
412
0
    return false;
413
0
  else
414
0
    return gl_array_remove_at (list, position);
415
0
}
416
417
static void
418
gl_array_list_free (gl_list_t list)
419
  _GL_LIST_INVOKES_FN_PTR
420
0
{
421
0
  if (list->elements != NULL)
422
0
    {
423
0
      if (list->base.dispose_fn != NULL)
424
0
        {
425
0
          size_t count = list->count;
426
427
0
          if (count > 0)
428
0
            {
429
0
              gl_listelement_dispose_fn dispose = list->base.dispose_fn;
430
0
              const void **elements = list->elements;
431
432
0
              do
433
0
                dispose (*elements++);
434
0
              while (--count > 0);
435
0
            }
436
0
        }
437
0
      free (list->elements);
438
0
    }
439
0
  free (list);
440
0
}
441
442
/* --------------------- gl_list_iterator_t Data Type --------------------- */
443
444
static gl_list_iterator_t _GL_ATTRIBUTE_PURE
445
gl_array_iterator (gl_list_t list)
446
0
{
447
0
  gl_list_iterator_t result;
448
449
0
  result.vtable = list->base.vtable;
450
0
  result.list = list;
451
0
  result.count = list->count;
452
0
  result.p = list->elements + 0;
453
0
  result.q = list->elements + list->count;
454
#if defined GCC_LINT || defined lint
455
  result.i = 0;
456
  result.j = 0;
457
#endif
458
459
0
  return result;
460
0
}
461
462
static gl_list_iterator_t _GL_ATTRIBUTE_PURE
463
gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
464
0
{
465
0
  gl_list_iterator_t result;
466
467
0
  if (!(start_index <= end_index && end_index <= list->count))
468
    /* Invalid arguments.  */
469
0
    abort ();
470
0
  result.vtable = list->base.vtable;
471
0
  result.list = list;
472
0
  result.count = list->count;
473
0
  result.p = list->elements + start_index;
474
0
  result.q = list->elements + end_index;
475
#if defined GCC_LINT || defined lint
476
  result.i = 0;
477
  result.j = 0;
478
#endif
479
480
0
  return result;
481
0
}
482
483
static bool
484
gl_array_iterator_next (gl_list_iterator_t *iterator,
485
                        const void **eltp, gl_list_node_t *nodep)
486
0
{
487
0
  gl_list_t list = iterator->list;
488
0
  if (iterator->count != list->count)
489
0
    {
490
0
      if (iterator->count != list->count + 1)
491
        /* Concurrent modifications were done on the list.  */
492
0
        abort ();
493
      /* The last returned element was removed.  */
494
0
      iterator->count--;
495
0
      iterator->p = (const void **) iterator->p - 1;
496
0
      iterator->q = (const void **) iterator->q - 1;
497
0
    }
498
0
  if (iterator->p < iterator->q)
499
0
    {
500
0
      const void **p = (const void **) iterator->p;
501
0
      *eltp = *p;
502
0
      if (nodep != NULL)
503
0
        *nodep = INDEX_TO_NODE (p - list->elements);
504
0
      iterator->p = p + 1;
505
0
      return true;
506
0
    }
507
0
  else
508
0
    return false;
509
0
}
510
511
static void
512
gl_array_iterator_free (gl_list_iterator_t *_GL_UNNAMED (iterator))
513
0
{
514
0
}
515
516
/* ---------------------- Sorted gl_list_t Data Type ---------------------- */
517
518
static size_t _GL_ATTRIBUTE_PURE
519
gl_array_sortedlist_indexof_from_to (gl_list_t list,
520
                                     gl_listelement_compar_fn compar,
521
                                     size_t low, size_t high,
522
                                     const void *elt)
523
  _GL_LIST_INVOKES_FN_PTR
524
0
{
525
0
  if (!(low <= high && high <= list->count))
526
    /* Invalid arguments.  */
527
0
    abort ();
528
0
  if (low < high)
529
0
    {
530
      /* At each loop iteration, low < high; for indices < low the values
531
         are smaller than ELT; for indices >= high the values are greater
532
         than ELT.  So, if the element occurs in the list, it is at
533
         low <= position < high.  */
534
0
      do
535
0
        {
536
0
          size_t mid = low + (high - low) / 2; /* low <= mid < high */
537
0
          int cmp = compar (list->elements[mid], elt);
538
539
0
          if (cmp < 0)
540
0
            low = mid + 1;
541
0
          else if (cmp > 0)
542
0
            high = mid;
543
0
          else /* cmp == 0 */
544
0
            {
545
              /* We have an element equal to ELT at index MID.  But we need
546
                 the minimal such index.  */
547
0
              high = mid;
548
              /* At each loop iteration, low <= high and
549
                   compar (list->elements[high], elt) == 0,
550
                 and we know that the first occurrence of the element is at
551
                 low <= position <= high.  */
552
0
              while (low < high)
553
0
                {
554
0
                  size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
555
0
                  int cmp2 = compar (list->elements[mid2], elt);
556
557
0
                  if (cmp2 < 0)
558
0
                    low = mid2 + 1;
559
0
                  else if (cmp2 > 0)
560
                    /* The list was not sorted.  */
561
0
                    abort ();
562
0
                  else /* cmp2 == 0 */
563
0
                    {
564
0
                      if (mid2 == low)
565
0
                        break;
566
0
                      high = mid2 - 1;
567
0
                    }
568
0
                }
569
0
              return low;
570
0
            }
571
0
        }
572
0
      while (low < high);
573
      /* Here low == high.  */
574
0
    }
575
0
  return (size_t)(-1);
576
0
}
577
578
static size_t _GL_ATTRIBUTE_PURE
579
gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
580
                             const void *elt)
581
0
{
582
0
  return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
583
0
                                              elt);
584
0
}
585
586
static gl_list_node_t _GL_ATTRIBUTE_PURE
587
gl_array_sortedlist_search_from_to (gl_list_t list,
588
                                    gl_listelement_compar_fn compar,
589
                                    size_t low, size_t high,
590
                                    const void *elt)
591
0
{
592
0
  size_t index =
593
0
    gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
594
0
  return INDEX_TO_NODE (index);
595
0
}
596
597
static gl_list_node_t _GL_ATTRIBUTE_PURE
598
gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
599
                            const void *elt)
600
0
{
601
0
  size_t index =
602
0
    gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
603
0
  return INDEX_TO_NODE (index);
604
0
}
605
606
static gl_list_node_t
607
gl_array_sortedlist_nx_add (gl_list_t list, gl_listelement_compar_fn compar,
608
                            const void *elt)
609
  _GL_LIST_INVOKES_FN_PTR
610
0
{
611
0
  size_t count = list->count;
612
0
  size_t low = 0;
613
0
  size_t high = count;
614
615
  /* At each loop iteration, low <= high; for indices < low the values are
616
     smaller than ELT; for indices >= high the values are greater than ELT.  */
617
0
  while (low < high)
618
0
    {
619
0
      size_t mid = low + (high - low) / 2; /* low <= mid < high */
620
0
      int cmp = compar (list->elements[mid], elt);
621
622
0
      if (cmp < 0)
623
0
        low = mid + 1;
624
0
      else if (cmp > 0)
625
0
        high = mid;
626
0
      else /* cmp == 0 */
627
0
        {
628
0
          low = mid;
629
0
          break;
630
0
        }
631
0
    }
632
0
  return gl_array_nx_add_at (list, low, elt);
633
0
}
634
635
static bool
636
gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
637
                            const void *elt)
638
0
{
639
0
  size_t index = gl_array_sortedlist_indexof (list, compar, elt);
640
0
  if (index == (size_t)(-1))
641
0
    return false;
642
0
  else
643
0
    return gl_array_remove_at (list, index);
644
0
}
645
646
647
const struct gl_list_implementation gl_array_list_implementation =
648
  {
649
    gl_array_nx_create_empty,
650
    gl_array_nx_create,
651
    gl_array_size,
652
    gl_array_node_value,
653
    gl_array_node_nx_set_value,
654
    gl_array_next_node,
655
    gl_array_previous_node,
656
    gl_array_first_node,
657
    gl_array_last_node,
658
    gl_array_get_at,
659
    gl_array_nx_set_at,
660
    gl_array_search_from_to,
661
    gl_array_indexof_from_to,
662
    gl_array_nx_add_first,
663
    gl_array_nx_add_last,
664
    gl_array_nx_add_before,
665
    gl_array_nx_add_after,
666
    gl_array_nx_add_at,
667
    gl_array_remove_node,
668
    gl_array_remove_at,
669
    gl_array_remove,
670
    gl_array_list_free,
671
    gl_array_iterator,
672
    gl_array_iterator_from_to,
673
    gl_array_iterator_next,
674
    gl_array_iterator_free,
675
    gl_array_sortedlist_search,
676
    gl_array_sortedlist_search_from_to,
677
    gl_array_sortedlist_indexof,
678
    gl_array_sortedlist_indexof_from_to,
679
    gl_array_sortedlist_nx_add,
680
    gl_array_sortedlist_remove
681
  };