Coverage Report

Created: 2026-09-12 06:55

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