Coverage Report

Created: 2025-06-13 06:58

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