Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/ssl/pqueue.c
Line
Count
Source
1
/*
2
 * Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "ssl_local.h"
11
#include <openssl/bn.h>
12
13
struct pqueue_st {
14
    pitem *items;
15
    int count;
16
};
17
18
pitem *pitem_new(unsigned char *prio64be, void *data)
19
129k
{
20
129k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
129k
    if (item == NULL)
23
0
        return NULL;
24
25
129k
    memcpy(item->priority, prio64be, sizeof(item->priority));
26
129k
    item->data = data;
27
129k
    item->next = NULL;
28
129k
    return item;
29
129k
}
30
31
void pitem_free(pitem *item)
32
129k
{
33
129k
    OPENSSL_free(item);
34
129k
}
35
36
pqueue *pqueue_new(void)
37
702k
{
38
702k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
702k
    return pq;
41
702k
}
42
43
void pqueue_free(pqueue *pq)
44
702k
{
45
702k
    OPENSSL_free(pq);
46
702k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
129k
{
50
129k
    pitem *curr, *next;
51
52
129k
    if (pq->items == NULL) {
53
59.7k
        pq->items = item;
54
59.7k
        return item;
55
59.7k
    }
56
57
70.0k
    for (curr = NULL, next = pq->items;
58
232k
        next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
191k
        int cmp = memcmp(next->priority, item->priority, 8);
63
191k
        if (cmp > 0) { /* next > item */
64
8.40k
            item->next = next;
65
66
8.40k
            if (curr == NULL)
67
3.66k
                pq->items = item;
68
4.74k
            else
69
4.74k
                curr->next = item;
70
71
8.40k
            return item;
72
8.40k
        }
73
74
183k
        else if (cmp == 0) /* duplicates not allowed */
75
20.9k
            return NULL;
76
191k
    }
77
78
40.6k
    item->next = NULL;
79
40.6k
    curr->next = item;
80
81
40.6k
    return item;
82
70.0k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
323k
{
86
323k
    return pq->items;
87
323k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
24.1M
{
91
24.1M
    pitem *item = pq->items;
92
93
24.1M
    if (pq->items != NULL)
94
108k
        pq->items = pq->items->next;
95
96
24.1M
    return item;
97
24.1M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
83.8k
{
101
83.8k
    pitem *next;
102
83.8k
    pitem *found = NULL;
103
104
83.8k
    if (pq->items == NULL)
105
35.1k
        return NULL;
106
107
88.9k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
46.7k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
6.53k
            found = next;
110
6.53k
            break;
111
6.53k
        }
112
46.7k
    }
113
114
    /* check the one last node */
115
48.7k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
31.2k
        found = next;
117
118
48.7k
    if (!found)
119
17.4k
        return NULL;
120
121
31.2k
    return found;
122
48.7k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
184k
{
126
184k
    return pqueue_peek(pq);
127
184k
}
128
129
pitem *pqueue_next(piterator *item)
130
186k
{
131
186k
    pitem *ret;
132
133
186k
    if (item == NULL || *item == NULL)
134
117k
        return NULL;
135
136
    /* *item != NULL */
137
69.1k
    ret = *item;
138
69.1k
    *item = (*item)->next;
139
140
69.1k
    return ret;
141
186k
}
142
143
size_t pqueue_size(pqueue *pq)
144
28.9k
{
145
28.9k
    pitem *item = pq->items;
146
28.9k
    size_t count = 0;
147
148
325k
    while (item != NULL) {
149
296k
        count++;
150
296k
        item = item->next;
151
296k
    }
152
28.9k
    return count;
153
28.9k
}