Coverage Report

Created: 2026-07-12 07:21

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