Coverage Report

Created: 2024-07-27 06:39

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