Coverage Report

Created: 2025-06-13 06:55

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