Coverage Report

Created: 2025-11-16 06:40

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
100k
{
20
100k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
100k
    if (item == NULL) {
23
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
24
0
        return NULL;
25
0
    }
26
27
100k
    memcpy(item->priority, prio64be, sizeof(item->priority));
28
100k
    item->data = data;
29
100k
    item->next = NULL;
30
100k
    return item;
31
100k
}
32
33
void pitem_free(pitem *item)
34
100k
{
35
100k
    OPENSSL_free(item);
36
100k
}
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
538k
{
50
538k
    OPENSSL_free(pq);
51
538k
}
52
53
pitem *pqueue_insert(pqueue *pq, pitem *item)
54
100k
{
55
100k
    pitem *curr, *next;
56
57
100k
    if (pq->items == NULL) {
58
46.3k
        pq->items = item;
59
46.3k
        return item;
60
46.3k
    }
61
62
53.9k
    for (curr = NULL, next = pq->items;
63
173k
         next != NULL; curr = next, next = next->next) {
64
        /*
65
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
66
         */
67
141k
        int cmp = memcmp(next->priority, item->priority, 8);
68
141k
        if (cmp > 0) {          /* next > item */
69
6.43k
            item->next = next;
70
71
6.43k
            if (curr == NULL)
72
3.07k
                pq->items = item;
73
3.36k
            else
74
3.36k
                curr->next = item;
75
76
6.43k
            return item;
77
6.43k
        }
78
79
135k
        else if (cmp == 0)      /* duplicates not allowed */
80
15.9k
            return NULL;
81
141k
    }
82
83
31.6k
    item->next = NULL;
84
31.6k
    curr->next = item;
85
86
31.6k
    return item;
87
53.9k
}
88
89
pitem *pqueue_peek(pqueue *pq)
90
255k
{
91
255k
    return pq->items;
92
255k
}
93
94
pitem *pqueue_pop(pqueue *pq)
95
18.7M
{
96
18.7M
    pitem *item = pq->items;
97
98
18.7M
    if (pq->items != NULL)
99
84.3k
        pq->items = pq->items->next;
100
101
18.7M
    return item;
102
18.7M
}
103
104
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
105
73.2k
{
106
73.2k
    pitem *next;
107
73.2k
    pitem *found = NULL;
108
109
73.2k
    if (pq->items == NULL)
110
28.6k
        return NULL;
111
112
89.6k
    for (next = pq->items; next->next != NULL; next = next->next) {
113
49.7k
        if (memcmp(next->priority, prio64be, 8) == 0) {
114
4.65k
            found = next;
115
4.65k
            break;
116
4.65k
        }
117
49.7k
    }
118
119
    /* check the one last node */
120
44.5k
    if (memcmp(next->priority, prio64be, 8) == 0)
121
29.6k
        found = next;
122
123
44.5k
    if (!found)
124
14.8k
        return NULL;
125
126
29.6k
    return found;
127
44.5k
}
128
129
pitem *pqueue_iterator(pqueue *pq)
130
148k
{
131
148k
    return pqueue_peek(pq);
132
148k
}
133
134
pitem *pqueue_next(piterator *item)
135
150k
{
136
150k
    pitem *ret;
137
138
150k
    if (item == NULL || *item == NULL)
139
92.1k
        return NULL;
140
141
    /* *item != NULL */
142
58.7k
    ret = *item;
143
58.7k
    *item = (*item)->next;
144
145
58.7k
    return ret;
146
150k
}
147
148
size_t pqueue_size(pqueue *pq)
149
21.7k
{
150
21.7k
    pitem *item = pq->items;
151
21.7k
    size_t count = 0;
152
153
226k
    while (item != NULL) {
154
204k
        count++;
155
204k
        item = item->next;
156
204k
    }
157
21.7k
    return count;
158
21.7k
}