Coverage Report

Created: 2026-11-16 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/pqueue.c
Line
Count
Source
1
/*
2
 * Copyright 2005-2026 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
pitem *pitem_new(unsigned char *prio64be, void *data)
14
0
{
15
0
    pitem *item = OPENSSL_malloc(sizeof(*item));
16
17
0
    if (item == NULL)
18
0
        return NULL;
19
20
0
    memcpy(item->priority, prio64be, sizeof(item->priority));
21
0
    item->data = data;
22
0
    item->next = NULL;
23
24
0
    return item;
25
0
}
26
27
pitem *pitem_new_u64(uint64_t prio, void *data)
28
0
{
29
0
    pitem *item = OPENSSL_malloc(sizeof(*item));
30
0
    unsigned char *p_item_prio;
31
32
0
    if (item == NULL)
33
0
        return NULL;
34
35
0
    p_item_prio = item->priority;
36
0
    l2n8(prio, p_item_prio);
37
0
    item->data = data;
38
0
    item->next = NULL;
39
40
0
    return item;
41
0
}
42
43
void pitem_free(pitem *item)
44
0
{
45
0
    OPENSSL_free(item);
46
0
}
47
48
pqueue *pqueue_new(void)
49
0
{
50
0
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
51
52
0
    return pq;
53
0
}
54
55
void pqueue_free(pqueue *pq)
56
0
{
57
0
    OPENSSL_free(pq);
58
0
}
59
60
pitem *pqueue_insert(pqueue *pq, pitem *item)
61
0
{
62
0
    pitem *curr, *next;
63
64
0
    if (pq->items == NULL) {
65
0
        item->next = NULL;
66
0
        pq->items = item;
67
0
        pq->count++;
68
69
0
        return item;
70
0
    }
71
72
0
    for (curr = NULL, next = pq->items;
73
0
        next != NULL; curr = next, next = next->next) {
74
        /*
75
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
76
         */
77
0
        int cmp = memcmp(next->priority, item->priority, 8);
78
0
        if (cmp > 0) { /* next > item */
79
0
            item->next = next;
80
81
0
            if (curr == NULL)
82
0
                pq->items = item;
83
0
            else
84
0
                curr->next = item;
85
0
            pq->count++;
86
87
0
            return item;
88
0
        }
89
90
0
        else if (cmp == 0) /* duplicates not allowed */
91
0
            return NULL;
92
0
    }
93
94
0
    item->next = NULL;
95
0
    curr->next = item;
96
0
    pq->count++;
97
98
0
    return item;
99
0
}
100
101
pitem *pqueue_peek(pqueue *pq)
102
0
{
103
0
    return pq->items;
104
0
}
105
106
pitem *pqueue_pop(pqueue *pq)
107
0
{
108
0
    pitem *item = pq->items;
109
110
0
    if (pq->items != NULL) {
111
0
        pq->items = pq->items->next;
112
0
        pq->count--;
113
0
    }
114
115
0
    return item;
116
0
}
117
118
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
119
0
{
120
0
    pitem *next;
121
0
    pitem *found = NULL;
122
123
0
    if (pq->items == NULL)
124
0
        return NULL;
125
126
0
    for (next = pq->items; next->next != NULL; next = next->next) {
127
0
        if (memcmp(next->priority, prio64be, 8) == 0) {
128
0
            found = next;
129
0
            break;
130
0
        }
131
0
    }
132
133
    /* check the one last node */
134
0
    if (memcmp(next->priority, prio64be, 8) == 0)
135
0
        found = next;
136
137
0
    if (!found)
138
0
        return NULL;
139
140
0
    return found;
141
0
}
142
143
pitem *pqueue_find_u64(pqueue *pq, uint64_t prio)
144
0
{
145
0
    unsigned char prio64be[8], *p_prio64be = prio64be;
146
147
0
    l2n8(prio, p_prio64be);
148
149
0
    return pqueue_find(pq, prio64be);
150
0
}
151
152
pitem *pqueue_iterator(pqueue *pq)
153
0
{
154
0
    return pqueue_peek(pq);
155
0
}
156
157
pitem *pqueue_next(piterator *item)
158
0
{
159
0
    pitem *ret;
160
161
0
    if (item == NULL || *item == NULL)
162
0
        return NULL;
163
164
    /* *item != NULL */
165
0
    ret = *item;
166
0
    *item = (*item)->next;
167
168
0
    return ret;
169
0
}
170
171
size_t pqueue_size(pqueue *pq)
172
0
{
173
0
    return pq->count;
174
0
}