Coverage Report

Created: 2025-08-28 07:07

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