Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/stack/stack.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2022 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "internal/numbers.h"
13
#include <openssl/stack.h>
14
#include <errno.h>
15
#include <openssl/e_os2.h> /* For ossl_inline */
16
17
/*
18
 * The initial number of nodes in the array.
19
 */
20
static const int min_nodes = 4;
21
static const int max_nodes = SIZE_MAX / sizeof(void *) < INT_MAX
22
    ? (int)(SIZE_MAX / sizeof(void *))
23
    : INT_MAX;
24
25
struct stack_st {
26
    int num;
27
    const void **data;
28
    int sorted;
29
    int num_alloc;
30
    OPENSSL_sk_compfunc comp;
31
};
32
33
OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk,
34
    OPENSSL_sk_compfunc c)
35
255k
{
36
255k
    OPENSSL_sk_compfunc old = sk->comp;
37
38
255k
    if (sk->comp != c)
39
255k
        sk->sorted = 0;
40
255k
    sk->comp = c;
41
42
255k
    return old;
43
255k
}
44
45
OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk)
46
40.6M
{
47
40.6M
    OPENSSL_STACK *ret;
48
49
40.6M
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
50
0
        goto err;
51
52
40.6M
    if (sk == NULL) {
53
196
        ret->num = 0;
54
196
        ret->sorted = 0;
55
196
        ret->comp = NULL;
56
40.6M
    } else {
57
        /* direct structure assignment */
58
40.6M
        *ret = *sk;
59
40.6M
    }
60
61
40.6M
    if (sk == NULL || sk->num == 0) {
62
        /* postpone |ret->data| allocation */
63
211
        ret->data = NULL;
64
211
        ret->num_alloc = 0;
65
211
        return ret;
66
211
    }
67
68
    /* duplicate |sk->data| content */
69
40.6M
    ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc);
70
40.6M
    if (ret->data == NULL)
71
0
        goto err;
72
40.6M
    memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
73
40.6M
    return ret;
74
75
0
err:
76
0
    ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
77
0
    OPENSSL_sk_free(ret);
78
0
    return NULL;
79
40.6M
}
80
81
OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk,
82
    OPENSSL_sk_copyfunc copy_func,
83
    OPENSSL_sk_freefunc free_func)
84
1.53M
{
85
1.53M
    OPENSSL_STACK *ret;
86
1.53M
    int i;
87
88
1.53M
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
89
0
        goto err;
90
91
1.53M
    if (sk == NULL) {
92
188
        ret->num = 0;
93
188
        ret->sorted = 0;
94
188
        ret->comp = NULL;
95
1.53M
    } else {
96
        /* direct structure assignment */
97
1.53M
        *ret = *sk;
98
1.53M
    }
99
100
1.53M
    if (sk == NULL || sk->num == 0) {
101
        /* postpone |ret| data allocation */
102
188
        ret->data = NULL;
103
188
        ret->num_alloc = 0;
104
188
        return ret;
105
188
    }
106
107
1.53M
    ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes;
108
1.53M
    ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc);
109
1.53M
    if (ret->data == NULL)
110
0
        goto err;
111
112
16.6M
    for (i = 0; i < ret->num; ++i) {
113
15.0M
        if (sk->data[i] == NULL)
114
0
            continue;
115
15.0M
        if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
116
0
            while (--i >= 0)
117
0
                if (ret->data[i] != NULL)
118
0
                    free_func((void *)ret->data[i]);
119
0
            goto err;
120
0
        }
121
15.0M
    }
122
1.53M
    return ret;
123
124
0
err:
125
0
    ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
126
0
    OPENSSL_sk_free(ret);
127
0
    return NULL;
128
1.53M
}
129
130
OPENSSL_STACK *OPENSSL_sk_new_null(void)
131
164M
{
132
164M
    return OPENSSL_sk_new_reserve(NULL, 0);
133
164M
}
134
135
OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c)
136
23.1M
{
137
23.1M
    return OPENSSL_sk_new_reserve(c, 0);
138
23.1M
}
139
140
/*
141
 * Calculate the array growth based on the target size.
142
 *
143
 * The growth fraction is a rational number and is defined by a numerator
144
 * and a denominator.  According to Andrew Koenig in his paper "Why Are
145
 * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
146
 * than the golden ratio (1.618...).
147
 *
148
 * We use 3/2 = 1.5 for simplicity of calculation and overflow checking.
149
 * Another option 8/5 = 1.6 allows for slightly faster growth, although safe
150
 * computation is more difficult.
151
 *
152
 * The limit to avoid overflow is spot on.  The modulo three correction term
153
 * ensures that the limit is the largest number than can be expanded by the
154
 * growth factor without exceeding the hard limit.
155
 *
156
 * Do not call it with |current| lower than 2, or it will infinitely loop.
157
 */
158
static ossl_inline int compute_growth(int target, int current)
159
4.09M
{
160
4.09M
    const int limit = (max_nodes / 3) * 2 + (max_nodes % 3 ? 1 : 0);
161
162
8.19M
    while (current < target) {
163
        /* Check to see if we're at the hard limit */
164
4.09M
        if (current >= max_nodes)
165
0
            return 0;
166
167
        /* Expand the size by a factor of 3/2 if it is within range */
168
4.09M
        current = current < limit ? current + current / 2 : max_nodes;
169
4.09M
    }
170
4.09M
    return current;
171
4.09M
}
172
173
/* internal STACK storage allocation */
174
static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
175
407M
{
176
407M
    const void **tmpdata;
177
407M
    int num_alloc;
178
179
    /* Check to see the reservation isn't exceeding the hard limit */
180
407M
    if (n > max_nodes - st->num) {
181
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS);
182
0
        return 0;
183
0
    }
184
185
    /* Figure out the new size */
186
407M
    num_alloc = st->num + n;
187
407M
    if (num_alloc < min_nodes)
188
38.5M
        num_alloc = min_nodes;
189
190
    /* If |st->data| allocation was postponed */
191
407M
    if (st->data == NULL) {
192
        /*
193
         * At this point, |st->num_alloc| and |st->num| are 0;
194
         * so |num_alloc| value is |n| or |min_nodes| if greater than |n|.
195
         */
196
27.0M
        if ((st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc)) == NULL) {
197
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
198
0
            return 0;
199
0
        }
200
27.0M
        st->num_alloc = num_alloc;
201
27.0M
        return 1;
202
27.0M
    }
203
204
380M
    if (!exact) {
205
380M
        if (num_alloc <= st->num_alloc)
206
370M
            return 1;
207
9.14M
        num_alloc = compute_growth(num_alloc, st->num_alloc);
208
9.14M
        if (num_alloc == 0) {
209
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS);
210
0
            return 0;
211
0
        }
212
9.14M
    } else if (num_alloc == st->num_alloc) {
213
0
        return 1;
214
0
    }
215
216
9.14M
    tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
217
9.14M
    if (tmpdata == NULL) {
218
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
219
0
        return 0;
220
0
    }
221
222
9.14M
    st->data = tmpdata;
223
9.14M
    st->num_alloc = num_alloc;
224
9.14M
    return 1;
225
9.14M
}
226
227
OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n)
228
188M
{
229
188M
    OPENSSL_STACK *st = OPENSSL_zalloc(sizeof(OPENSSL_STACK));
230
231
188M
    if (st == NULL) {
232
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
233
0
        return NULL;
234
0
    }
235
236
188M
    st->comp = c;
237
238
188M
    if (n <= 0)
239
187M
        return st;
240
241
1.09M
    if (!sk_reserve(st, n, 1)) {
242
0
        OPENSSL_sk_free(st);
243
0
        return NULL;
244
0
    }
245
246
1.09M
    return st;
247
1.09M
}
248
249
int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n)
250
0
{
251
0
    if (st == NULL) {
252
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
253
0
        return 0;
254
0
    }
255
256
0
    if (n < 0)
257
0
        return 1;
258
0
    return sk_reserve(st, n, 1);
259
0
}
260
261
int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
262
320M
{
263
320M
    if (st == NULL) {
264
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
265
0
        return 0;
266
0
    }
267
320M
    if (st->num == max_nodes) {
268
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS);
269
0
        return 0;
270
0
    }
271
272
320M
    if (!sk_reserve(st, 1, 0))
273
0
        return 0;
274
275
320M
    if ((loc >= st->num) || (loc < 0)) {
276
320M
        st->data[st->num] = data;
277
320M
    } else {
278
5.62k
        memmove(&st->data[loc + 1], &st->data[loc],
279
5.62k
            sizeof(st->data[0]) * (st->num - loc));
280
5.62k
        st->data[loc] = data;
281
5.62k
    }
282
320M
    st->num++;
283
320M
    st->sorted = 0;
284
320M
    return st->num;
285
320M
}
286
287
static ossl_inline void *internal_delete(OPENSSL_STACK *st, int loc)
288
2.29M
{
289
2.29M
    const void *ret = st->data[loc];
290
291
2.29M
    if (loc != st->num - 1)
292
236k
        memmove(&st->data[loc], &st->data[loc + 1],
293
236k
            sizeof(st->data[0]) * (st->num - loc - 1));
294
2.29M
    st->num--;
295
296
2.29M
    return (void *)ret;
297
2.29M
}
298
299
void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p)
300
148k
{
301
148k
    int i;
302
303
148k
    if (st == NULL)
304
0
        return NULL;
305
306
452k
    for (i = 0; i < st->num; i++)
307
452k
        if (st->data[i] == p)
308
148k
            return internal_delete(st, i);
309
0
    return NULL;
310
148k
}
311
312
void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc)
313
341k
{
314
341k
    if (st == NULL || loc < 0 || loc >= st->num)
315
0
        return NULL;
316
317
341k
    return internal_delete(st, loc);
318
341k
}
319
320
static int internal_find(OPENSSL_STACK *st, const void *data,
321
    int ret_val_options, int *pnum)
322
24.1k
{
323
24.1k
    const void *r;
324
24.1k
    int i;
325
326
24.1k
    if (st == NULL || st->num == 0)
327
9.66k
        return -1;
328
329
14.4k
    if (st->comp == NULL) {
330
265k
        for (i = 0; i < st->num; i++)
331
265k
            if (st->data[i] == data) {
332
3.12k
                if (pnum != NULL)
333
0
                    *pnum = 1;
334
3.12k
                return i;
335
3.12k
            }
336
277
        if (pnum != NULL)
337
0
            *pnum = 0;
338
277
        return -1;
339
3.39k
    }
340
341
11.1k
    if (!st->sorted) {
342
1.82k
        if (st->num > 1)
343
0
            qsort(st->data, st->num, sizeof(void *), st->comp);
344
1.82k
        st->sorted = 1; /* empty or single-element stack is considered sorted */
345
1.82k
    }
346
11.1k
    if (data == NULL)
347
0
        return -1;
348
11.1k
    if (pnum != NULL)
349
2.38k
        ret_val_options |= OSSL_BSEARCH_FIRST_VALUE_ON_MATCH;
350
11.1k
    r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp,
351
11.1k
        ret_val_options);
352
353
11.1k
    if (pnum != NULL) {
354
2.38k
        *pnum = 0;
355
2.38k
        if (r != NULL) {
356
1.83k
            const void **p = (const void **)r;
357
358
3.67k
            while (p < st->data + st->num) {
359
1.83k
                if (st->comp(&data, p) != 0)
360
0
                    break;
361
1.83k
                ++*pnum;
362
1.83k
                ++p;
363
1.83k
            }
364
1.83k
        }
365
2.38k
    }
366
367
11.1k
    return r == NULL ? -1 : (int)((const void **)r - st->data);
368
11.1k
}
369
370
int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)
371
171k
{
372
171k
    return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, NULL);
373
171k
}
374
375
int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data)
376
0
{
377
0
    return internal_find(st, data, OSSL_BSEARCH_VALUE_ON_NOMATCH, NULL);
378
0
}
379
380
int OPENSSL_sk_find_all(OPENSSL_STACK *st, const void *data, int *pnum)
381
101k
{
382
101k
    return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, pnum);
383
101k
}
384
385
int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
386
405M
{
387
405M
    if (st == NULL)
388
0
        return -1;
389
405M
    return OPENSSL_sk_insert(st, data, st->num);
390
405M
}
391
392
int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
393
0
{
394
0
    return OPENSSL_sk_insert(st, data, 0);
395
0
}
396
397
void *OPENSSL_sk_shift(OPENSSL_STACK *st)
398
0
{
399
0
    if (st == NULL || st->num == 0)
400
0
        return NULL;
401
0
    return internal_delete(st, 0);
402
0
}
403
404
void *OPENSSL_sk_pop(OPENSSL_STACK *st)
405
2.38M
{
406
2.38M
    if (st == NULL || st->num == 0)
407
10.0k
        return NULL;
408
2.37M
    return internal_delete(st, st->num - 1);
409
2.38M
}
410
411
void OPENSSL_sk_zero(OPENSSL_STACK *st)
412
0
{
413
0
    if (st == NULL || st->num == 0)
414
0
        return;
415
0
    memset(st->data, 0, sizeof(*st->data) * st->num);
416
0
    st->num = 0;
417
0
}
418
419
void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func)
420
49.0M
{
421
49.0M
    int i;
422
423
49.0M
    if (st == NULL)
424
11.0M
        return;
425
212M
    for (i = 0; i < st->num; i++)
426
174M
        if (st->data[i] != NULL)
427
174M
            func((char *)st->data[i]);
428
38.0M
    OPENSSL_sk_free(st);
429
38.0M
}
430
431
void OPENSSL_sk_free(OPENSSL_STACK *st)
432
283M
{
433
283M
    if (st == NULL)
434
53.0M
        return;
435
230M
    OPENSSL_free(st->data);
436
230M
    OPENSSL_free(st);
437
230M
}
438
439
int OPENSSL_sk_num(const OPENSSL_STACK *st)
440
1.25G
{
441
1.25G
    return st == NULL ? -1 : st->num;
442
1.25G
}
443
444
void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)
445
1.40G
{
446
1.40G
    if (st == NULL || i < 0 || i >= st->num)
447
4.88k
        return NULL;
448
1.40G
    return (void *)st->data[i];
449
1.40G
}
450
451
void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data)
452
12.9M
{
453
12.9M
    if (st == NULL) {
454
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
455
0
        return NULL;
456
0
    }
457
12.9M
    if (i < 0 || i >= st->num) {
458
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
459
0
            "i=%d", i);
460
0
        return NULL;
461
0
    }
462
12.9M
    st->data[i] = data;
463
12.9M
    st->sorted = 0;
464
12.9M
    return (void *)st->data[i];
465
12.9M
}
466
467
void OPENSSL_sk_sort(OPENSSL_STACK *st)
468
23.3M
{
469
23.3M
    if (st != NULL && !st->sorted && st->comp != NULL) {
470
18.9M
        if (st->num > 1)
471
298k
            qsort(st->data, st->num, sizeof(void *), st->comp);
472
18.9M
        st->sorted = 1; /* empty or single-element stack is considered sorted */
473
18.9M
    }
474
23.3M
}
475
476
int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st)
477
81.1k
{
478
81.1k
    return st == NULL ? 1 : st->sorted;
479
81.1k
}