Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
152k
{
20
152k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
152k
    if (item == NULL)
23
0
        return NULL;
24
25
152k
    memcpy(item->priority, prio64be, sizeof(item->priority));
26
152k
    item->data = data;
27
152k
    item->next = NULL;
28
152k
    return item;
29
152k
}
30
31
void pitem_free(pitem *item)
32
152k
{
33
152k
    OPENSSL_free(item);
34
152k
}
35
36
pqueue *pqueue_new(void)
37
762k
{
38
762k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
762k
    return pq;
41
762k
}
42
43
void pqueue_free(pqueue *pq)
44
762k
{
45
762k
    OPENSSL_free(pq);
46
762k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
152k
{
50
152k
    pitem *curr, *next;
51
52
152k
    if (pq->items == NULL) {
53
70.7k
        pq->items = item;
54
70.7k
        return item;
55
70.7k
    }
56
57
82.0k
    for (curr = NULL, next = pq->items;
58
267k
        next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
219k
        int cmp = memcmp(next->priority, item->priority, 8);
63
219k
        if (cmp > 0) { /* next > item */
64
9.55k
            item->next = next;
65
66
9.55k
            if (curr == NULL)
67
3.47k
                pq->items = item;
68
6.08k
            else
69
6.08k
                curr->next = item;
70
71
9.55k
            return item;
72
9.55k
        }
73
74
210k
        else if (cmp == 0) /* duplicates not allowed */
75
24.5k
            return NULL;
76
219k
    }
77
78
47.9k
    item->next = NULL;
79
47.9k
    curr->next = item;
80
81
47.9k
    return item;
82
82.0k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
357k
{
86
357k
    return pq->items;
87
357k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
24.4M
{
91
24.4M
    pitem *item = pq->items;
92
93
24.4M
    if (pq->items != NULL)
94
128k
        pq->items = pq->items->next;
95
96
24.4M
    return item;
97
24.4M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
89.7k
{
101
89.7k
    pitem *next;
102
89.7k
    pitem *found = NULL;
103
104
89.7k
    if (pq->items == NULL)
105
38.1k
        return NULL;
106
107
93.6k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
51.3k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
9.28k
            found = next;
110
9.28k
            break;
111
9.28k
        }
112
51.3k
    }
113
114
    /* check the one last node */
115
51.6k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
34.9k
        found = next;
117
118
51.6k
    if (!found)
119
16.6k
        return NULL;
120
121
34.9k
    return found;
122
51.6k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
205k
{
126
205k
    return pqueue_peek(pq);
127
205k
}
128
129
pitem *pqueue_next(piterator *item)
130
207k
{
131
207k
    pitem *ret;
132
133
207k
    if (item == NULL || *item == NULL)
134
129k
        return NULL;
135
136
    /* *item != NULL */
137
77.5k
    ret = *item;
138
77.5k
    *item = (*item)->next;
139
140
77.5k
    return ret;
141
207k
}
142
143
size_t pqueue_size(pqueue *pq)
144
34.4k
{
145
34.4k
    pitem *item = pq->items;
146
34.4k
    size_t count = 0;
147
148
354k
    while (item != NULL) {
149
319k
        count++;
150
319k
        item = item->next;
151
319k
    }
152
34.4k
    return count;
153
34.4k
}