Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/ssl/pqueue.c
Line
Count
Source (jump to first uncovered line)
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
24.9k
{
20
24.9k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
24.9k
    if (item == NULL) {
23
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
24
0
        return NULL;
25
0
    }
26
27
24.9k
    memcpy(item->priority, prio64be, sizeof(item->priority));
28
24.9k
    item->data = data;
29
24.9k
    item->next = NULL;
30
24.9k
    return item;
31
24.9k
}
32
33
void pitem_free(pitem *item)
34
24.9k
{
35
24.9k
    OPENSSL_free(item);
36
24.9k
}
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
114k
{
50
114k
    OPENSSL_free(pq);
51
114k
}
52
53
pitem *pqueue_insert(pqueue *pq, pitem *item)
54
24.9k
{
55
24.9k
    pitem *curr, *next;
56
57
24.9k
    if (pq->items == NULL) {
58
8.35k
        pq->items = item;
59
8.35k
        return item;
60
8.35k
    }
61
62
16.6k
    for (curr = NULL, next = pq->items;
63
67.7k
         next != NULL; curr = next, next = next->next) {
64
        /*
65
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
66
         */
67
60.3k
        int cmp = memcmp(next->priority, item->priority, 8);
68
60.3k
        if (cmp > 0) {          /* next > item */
69
2.23k
            item->next = next;
70
71
2.23k
            if (curr == NULL)
72
793
                pq->items = item;
73
1.44k
            else
74
1.44k
                curr->next = item;
75
76
2.23k
            return item;
77
2.23k
        }
78
79
58.0k
        else if (cmp == 0)      /* duplicates not allowed */
80
6.93k
            return NULL;
81
60.3k
    }
82
83
7.44k
    item->next = NULL;
84
7.44k
    curr->next = item;
85
86
7.44k
    return item;
87
16.6k
}
88
89
pitem *pqueue_peek(pqueue *pq)
90
53.3k
{
91
53.3k
    return pq->items;
92
53.3k
}
93
94
pitem *pqueue_pop(pqueue *pq)
95
3.54M
{
96
3.54M
    pitem *item = pq->items;
97
98
3.54M
    if (pq->items != NULL)
99
18.0k
        pq->items = pq->items->next;
100
101
3.54M
    return item;
102
3.54M
}
103
104
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
105
19.0k
{
106
19.0k
    pitem *next;
107
19.0k
    pitem *found = NULL;
108
109
19.0k
    if (pq->items == NULL)
110
4.44k
        return NULL;
111
112
27.9k
    for (next = pq->items; next->next != NULL; next = next->next) {
113
15.1k
        if (memcmp(next->priority, prio64be, 8) == 0) {
114
1.79k
            found = next;
115
1.79k
            break;
116
1.79k
        }
117
15.1k
    }
118
119
    /* check the one last node */
120
14.5k
    if (memcmp(next->priority, prio64be, 8) == 0)
121
11.1k
        found = next;
122
123
14.5k
    if (!found)
124
3.37k
        return NULL;
125
126
11.1k
    return found;
127
14.5k
}
128
129
pitem *pqueue_iterator(pqueue *pq)
130
31.0k
{
131
31.0k
    return pqueue_peek(pq);
132
31.0k
}
133
134
pitem *pqueue_next(piterator *item)
135
31.0k
{
136
31.0k
    pitem *ret;
137
138
31.0k
    if (item == NULL || *item == NULL)
139
17.6k
        return NULL;
140
141
    /* *item != NULL */
142
13.4k
    ret = *item;
143
13.4k
    *item = (*item)->next;
144
145
13.4k
    return ret;
146
31.0k
}
147
148
size_t pqueue_size(pqueue *pq)
149
9.01k
{
150
9.01k
    pitem *item = pq->items;
151
9.01k
    size_t count = 0;
152
153
133k
    while (item != NULL) {
154
124k
        count++;
155
124k
        item = item->next;
156
124k
    }
157
9.01k
    return count;
158
9.01k
}