Coverage Report

Created: 2026-05-24 07:14

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
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
786k
{
38
786k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
786k
    return pq;
41
786k
}
42
43
void pqueue_free(pqueue *pq)
44
786k
{
45
786k
    OPENSSL_free(pq);
46
786k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
151k
{
50
151k
    pitem *curr, *next;
51
52
151k
    if (pq->items == NULL) {
53
72.0k
        pq->items = item;
54
72.0k
        return item;
55
72.0k
    }
56
57
79.2k
    for (curr = NULL, next = pq->items;
58
252k
        next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
204k
        int cmp = memcmp(next->priority, item->priority, 8);
63
204k
        if (cmp > 0) { /* next > item */
64
8.98k
            item->next = next;
65
66
8.98k
            if (curr == NULL)
67
3.66k
                pq->items = item;
68
5.31k
            else
69
5.31k
                curr->next = item;
70
71
8.98k
            return item;
72
8.98k
        }
73
74
195k
        else if (cmp == 0) /* duplicates not allowed */
75
21.7k
            return NULL;
76
204k
    }
77
78
48.5k
    item->next = NULL;
79
48.5k
    curr->next = item;
80
81
48.5k
    return item;
82
79.2k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
366k
{
86
366k
    return pq->items;
87
366k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
34.1M
{
91
34.1M
    pitem *item = pq->items;
92
93
34.1M
    if (pq->items != NULL)
94
129k
        pq->items = pq->items->next;
95
96
34.1M
    return item;
97
34.1M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
90.3k
{
101
90.3k
    pitem *next;
102
90.3k
    pitem *found = NULL;
103
104
90.3k
    if (pq->items == NULL)
105
38.3k
        return NULL;
106
107
95.5k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
52.2k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
8.68k
            found = next;
110
8.68k
            break;
111
8.68k
        }
112
52.2k
    }
113
114
    /* check the one last node */
115
52.0k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
34.4k
        found = next;
117
118
52.0k
    if (!found)
119
17.5k
        return NULL;
120
121
34.4k
    return found;
122
52.0k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
209k
{
126
209k
    return pqueue_peek(pq);
127
209k
}
128
129
pitem *pqueue_next(piterator *item)
130
211k
{
131
211k
    pitem *ret;
132
133
211k
    if (item == NULL || *item == NULL)
134
132k
        return NULL;
135
136
    /* *item != NULL */
137
78.4k
    ret = *item;
138
78.4k
    *item = (*item)->next;
139
140
78.4k
    return ret;
141
211k
}
142
143
size_t pqueue_size(pqueue *pq)
144
31.2k
{
145
31.2k
    pitem *item = pq->items;
146
31.2k
    size_t count = 0;
147
148
314k
    while (item != NULL) {
149
283k
        count++;
150
283k
        item = item->next;
151
283k
    }
152
31.2k
    return count;
153
31.2k
}