Coverage Report

Created: 2026-02-14 07:20

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
128k
{
20
128k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
128k
    if (item == NULL)
23
0
        return NULL;
24
25
128k
    memcpy(item->priority, prio64be, sizeof(item->priority));
26
128k
    item->data = data;
27
128k
    item->next = NULL;
28
128k
    return item;
29
128k
}
30
31
void pitem_free(pitem *item)
32
128k
{
33
128k
    OPENSSL_free(item);
34
128k
}
35
36
pqueue *pqueue_new(void)
37
696k
{
38
696k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
696k
    return pq;
41
696k
}
42
43
void pqueue_free(pqueue *pq)
44
696k
{
45
696k
    OPENSSL_free(pq);
46
696k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
128k
{
50
128k
    pitem *curr, *next;
51
52
128k
    if (pq->items == NULL) {
53
59.5k
        pq->items = item;
54
59.5k
        return item;
55
59.5k
    }
56
57
69.3k
    for (curr = NULL, next = pq->items;
58
229k
        next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
189k
        int cmp = memcmp(next->priority, item->priority, 8);
63
189k
        if (cmp > 0) { /* next > item */
64
8.24k
            item->next = next;
65
66
8.24k
            if (curr == NULL)
67
3.66k
                pq->items = item;
68
4.57k
            else
69
4.57k
                curr->next = item;
70
71
8.24k
            return item;
72
8.24k
        }
73
74
181k
        else if (cmp == 0) /* duplicates not allowed */
75
20.9k
            return NULL;
76
189k
    }
77
78
40.1k
    item->next = NULL;
79
40.1k
    curr->next = item;
80
81
40.1k
    return item;
82
69.3k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
318k
{
86
318k
    return pq->items;
87
318k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
25.4M
{
91
25.4M
    pitem *item = pq->items;
92
93
25.4M
    if (pq->items != NULL)
94
107k
        pq->items = pq->items->next;
95
96
25.4M
    return item;
97
25.4M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
80.5k
{
101
80.5k
    pitem *next;
102
80.5k
    pitem *found = NULL;
103
104
80.5k
    if (pq->items == NULL)
105
35.0k
        return NULL;
106
107
84.7k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
45.0k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
5.88k
            found = next;
110
5.88k
            break;
111
5.88k
        }
112
45.0k
    }
113
114
    /* check the one last node */
115
45.5k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
28.6k
        found = next;
117
118
45.5k
    if (!found)
119
16.8k
        return NULL;
120
121
28.6k
    return found;
122
45.5k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
180k
{
126
180k
    return pqueue_peek(pq);
127
180k
}
128
129
pitem *pqueue_next(piterator *item)
130
182k
{
131
182k
    pitem *ret;
132
133
182k
    if (item == NULL || *item == NULL)
134
115k
        return NULL;
135
136
    /* *item != NULL */
137
66.9k
    ret = *item;
138
66.9k
    *item = (*item)->next;
139
140
66.9k
    return ret;
141
182k
}
142
143
size_t pqueue_size(pqueue *pq)
144
28.8k
{
145
28.8k
    pitem *item = pq->items;
146
28.8k
    size_t count = 0;
147
148
324k
    while (item != NULL) {
149
295k
        count++;
150
295k
        item = item->next;
151
295k
    }
152
28.8k
    return count;
153
28.8k
}