Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
120k
{
20
120k
    pitem *item = OPENSSL_malloc(sizeof(*item));
21
22
120k
    if (item == NULL)
23
0
        return NULL;
24
25
120k
    memcpy(item->priority, prio64be, sizeof(item->priority));
26
120k
    item->data = data;
27
120k
    item->next = NULL;
28
120k
    return item;
29
120k
}
30
31
void pitem_free(pitem *item)
32
120k
{
33
120k
    OPENSSL_free(item);
34
120k
}
35
36
pqueue *pqueue_new(void)
37
675k
{
38
675k
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
39
40
675k
    return pq;
41
675k
}
42
43
void pqueue_free(pqueue *pq)
44
675k
{
45
675k
    OPENSSL_free(pq);
46
675k
}
47
48
pitem *pqueue_insert(pqueue *pq, pitem *item)
49
120k
{
50
120k
    pitem *curr, *next;
51
52
120k
    if (pq->items == NULL) {
53
56.6k
        pq->items = item;
54
56.6k
        return item;
55
56.6k
    }
56
57
63.9k
    for (curr = NULL, next = pq->items;
58
194k
        next != NULL; curr = next, next = next->next) {
59
        /*
60
         * we can compare 64-bit value in big-endian encoding with memcmp:-)
61
         */
62
156k
        int cmp = memcmp(next->priority, item->priority, 8);
63
156k
        if (cmp > 0) { /* next > item */
64
7.44k
            item->next = next;
65
66
7.44k
            if (curr == NULL)
67
3.55k
                pq->items = item;
68
3.89k
            else
69
3.89k
                curr->next = item;
70
71
7.44k
            return item;
72
7.44k
        }
73
74
148k
        else if (cmp == 0) /* duplicates not allowed */
75
17.6k
            return NULL;
76
156k
    }
77
78
38.8k
    item->next = NULL;
79
38.8k
    curr->next = item;
80
81
38.8k
    return item;
82
63.9k
}
83
84
pitem *pqueue_peek(pqueue *pq)
85
312k
{
86
312k
    return pq->items;
87
312k
}
88
89
pitem *pqueue_pop(pqueue *pq)
90
24.7M
{
91
24.7M
    pitem *item = pq->items;
92
93
24.7M
    if (pq->items != NULL)
94
102k
        pq->items = pq->items->next;
95
96
24.7M
    return item;
97
24.7M
}
98
99
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
100
84.2k
{
101
84.2k
    pitem *next;
102
84.2k
    pitem *found = NULL;
103
104
84.2k
    if (pq->items == NULL)
105
35.1k
        return NULL;
106
107
92.9k
    for (next = pq->items; next->next != NULL; next = next->next) {
108
49.5k
        if (memcmp(next->priority, prio64be, 8) == 0) {
109
5.82k
            found = next;
110
5.82k
            break;
111
5.82k
        }
112
49.5k
    }
113
114
    /* check the one last node */
115
49.1k
    if (memcmp(next->priority, prio64be, 8) == 0)
116
32.0k
        found = next;
117
118
49.1k
    if (!found)
119
17.0k
        return NULL;
120
121
32.0k
    return found;
122
49.1k
}
123
124
pitem *pqueue_iterator(pqueue *pq)
125
178k
{
126
178k
    return pqueue_peek(pq);
127
178k
}
128
129
pitem *pqueue_next(piterator *item)
130
180k
{
131
180k
    pitem *ret;
132
133
180k
    if (item == NULL || *item == NULL)
134
113k
        return NULL;
135
136
    /* *item != NULL */
137
67.6k
    ret = *item;
138
67.6k
    *item = (*item)->next;
139
140
67.6k
    return ret;
141
180k
}
142
143
size_t pqueue_size(pqueue *pq)
144
24.7k
{
145
24.7k
    pitem *item = pq->items;
146
24.7k
    size_t count = 0;
147
148
231k
    while (item != NULL) {
149
207k
        count++;
150
207k
        item = item->next;
151
207k
    }
152
24.7k
    return count;
153
24.7k
}