Coverage Report

Created: 2025-08-28 07:07

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