Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
130k
{
20
130k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
130k
    if (item == NULL) {
23
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
24
0
        return NULL;
25
0
    }
26
27
130k
    memcpy(item->priority, prio64be, sizeof(item->priority));
28
130k
    item->data = data;
29
130k
    item->next = NULL;
30
130k
    return item;
31
130k
}
32
33
void pitem_free(pitem *item)
34
130k
{
35
130k
    OPENSSL_free(item);
36
130k
}
37
38
pqueue *pqueue_new(void)
39
{
40
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
41
42
    if (pq == NULL)
43
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
44
45
    return pq;
46
}
47
48
void pqueue_free(pqueue *pq)
49
708k
{
50
708k
    OPENSSL_free(pq);
51
708k
}
52
53
pitem *pqueue_insert(pqueue *pq, pitem *item)
54
130k
{
55
130k
    pitem *curr, *next;
56
57
130k
    if (pq->items == NULL) {
58
60.0k
        pq->items = item;
59
60.0k
        return item;
60
60.0k
    }
61
62
70.8k
    for (curr = NULL, next = pq->items;
63
238k
        next != NULL; curr = next, next = next->next) {
64
        /*
65
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
66
         */
67
197k
        int cmp = memcmp(next->priority, item->priority, 8);
68
197k
        if (cmp > 0) { /* next > item */
69
8.83k
            item->next = next;
70
71
8.83k
            if (curr == NULL)
72
3.75k
                pq->items = item;
73
5.07k
            else
74
5.07k
                curr->next = item;
75
76
8.83k
            return item;
77
8.83k
        }
78
79
188k
        else if (cmp == 0) /* duplicates not allowed */
80
21.0k
            return NULL;
81
197k
    }
82
83
40.9k
    item->next = NULL;
84
40.9k
    curr->next = item;
85
86
40.9k
    return item;
87
70.8k
}
88
89
pitem *pqueue_peek(pqueue *pq)
90
325k
{
91
325k
    return pq->items;
92
325k
}
93
94
pitem *pqueue_pop(pqueue *pq)
95
24.9M
{
96
24.9M
    pitem *item = pq->items;
97
98
24.9M
    if (pq->items != NULL)
99
109k
        pq->items = pq->items->next;
100
101
24.9M
    return item;
102
24.9M
}
103
104
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
105
84.8k
{
106
84.8k
    pitem *next;
107
84.8k
    pitem *found = NULL;
108
109
84.8k
    if (pq->items == NULL)
110
34.6k
        return NULL;
111
112
92.5k
    for (next = pq->items; next->next != NULL; next = next->next) {
113
50.4k
        if (memcmp(next->priority, prio64be, 8) == 0) {
114
8.06k
            found = next;
115
8.06k
            break;
116
8.06k
        }
117
50.4k
    }
118
119
    /* check the one last node */
120
50.2k
    if (memcmp(next->priority, prio64be, 8) == 0)
121
32.9k
        found = next;
122
123
50.2k
    if (!found)
124
17.3k
        return NULL;
125
126
32.9k
    return found;
127
50.2k
}
128
129
pitem *pqueue_iterator(pqueue *pq)
130
185k
{
131
185k
    return pqueue_peek(pq);
132
185k
}
133
134
pitem *pqueue_next(piterator *item)
135
187k
{
136
187k
    pitem *ret;
137
138
187k
    if (item == NULL || *item == NULL)
139
117k
        return NULL;
140
141
    /* *item != NULL */
142
69.5k
    ret = *item;
143
69.5k
    *item = (*item)->next;
144
145
69.5k
    return ret;
146
187k
}
147
148
size_t pqueue_size(pqueue *pq)
149
29.4k
{
150
29.4k
    pitem *item = pq->items;
151
29.4k
    size_t count = 0;
152
153
336k
    while (item != NULL) {
154
307k
        count++;
155
307k
        item = item->next;
156
307k
    }
157
29.4k
    return count;
158
29.4k
}