Coverage Report

Created: 2026-02-14 07:20

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
263k
{
36
263k
    OPENSSL_sk_compfunc old = sk->comp;
37
38
263k
    if (sk->comp != c)
39
263k
        sk->sorted = 0;
40
263k
    sk->comp = c;
41
42
263k
    return old;
43
263k
}
44
45
OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk)
46
39.2M
{
47
39.2M
    OPENSSL_STACK *ret;
48
49
39.2M
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
50
0
        goto err;
51
52
39.2M
    if (sk == NULL) {
53
196
        ret->num = 0;
54
196
        ret->sorted = 0;
55
196
        ret->comp = NULL;
56
39.2M
    } else {
57
        /* direct structure assignment */
58
39.2M
        *ret = *sk;
59
39.2M
    }
60
61
39.2M
    if (sk == NULL || sk->num == 0) {
62
        /* postpone |ret->data| allocation */
63
221
        ret->data = NULL;
64
221
        ret->num_alloc = 0;
65
221
        return ret;
66
221
    }
67
68
    /* duplicate |sk->data| content */
69
39.2M
    ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc);
70
39.2M
    if (ret->data == NULL)
71
0
        goto err;
72
39.2M
    memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
73
39.2M
    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
39.2M
}
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.69M
{
85
1.69M
    OPENSSL_STACK *ret;
86
1.69M
    int i;
87
88
1.69M
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
89
0
        goto err;
90
91
1.69M
    if (sk == NULL) {
92
188
        ret->num = 0;
93
188
        ret->sorted = 0;
94
188
        ret->comp = NULL;
95
1.69M
    } else {
96
        /* direct structure assignment */
97
1.69M
        *ret = *sk;
98
1.69M
    }
99
100
1.69M
    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.69M
    ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes;
108
1.69M
    ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc);
109
1.69M
    if (ret->data == NULL)
110
0
        goto err;
111
112
17.4M
    for (i = 0; i < ret->num; ++i) {
113
15.7M
        if (sk->data[i] == NULL)
114
0
            continue;
115
15.7M
        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.7M
    }
122
1.69M
    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.69M
}
129
130
OPENSSL_STACK *OPENSSL_sk_new_null(void)
131
174M
{
132
174M
    return OPENSSL_sk_new_reserve(NULL, 0);
133
174M
}
134
135
OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c)
136
23.4M
{
137
23.4M
    return OPENSSL_sk_new_reserve(c, 0);
138
23.4M
}
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
3.95M
{
160
3.95M
    const int limit = (max_nodes / 3) * 2 + (max_nodes % 3 ? 1 : 0);
161
162
7.91M
    while (current < target) {
163
        /* Check to see if we're at the hard limit */
164
3.95M
        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
3.95M
        current = current < limit ? current + current / 2 : max_nodes;
169
3.95M
    }
170
3.95M
    return current;
171
3.95M
}
172
173
/* internal STACK storage allocation */
174
static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
175
398M
{
176
398M
    const void **tmpdata;
177
398M
    int num_alloc;
178
179
    /* Check to see the reservation isn't exceeding the hard limit */
180
398M
    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
398M
    num_alloc = st->num + n;
187
398M
    if (num_alloc < min_nodes)
188
37.4M
        num_alloc = min_nodes;
189
190
    /* If |st->data| allocation was postponed */
191
398M
    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
26.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
26.0M
        st->num_alloc = num_alloc;
201
26.0M
        return 1;
202
26.0M
    }
203
204
372M
    if (!exact) {
205
372M
        if (num_alloc <= st->num_alloc)
206
363M
            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
198M
{
229
198M
    OPENSSL_STACK *st = OPENSSL_zalloc(sizeof(OPENSSL_STACK));
230
231
198M
    if (st == NULL) {
232
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
233
0
        return NULL;
234
0
    }
235
236
198M
    st->comp = c;
237
238
198M
    if (n <= 0)
239
197M
        return st;
240
241
1.06M
    if (!sk_reserve(st, n, 1)) {
242
0
        OPENSSL_sk_free(st);
243
0
        return NULL;
244
0
    }
245
246
1.06M
    return st;
247
1.06M
}
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
327M
{
263
327M
    if (st == NULL) {
264
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
265
0
        return 0;
266
0
    }
267
327M
    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
327M
    if (!sk_reserve(st, 1, 0))
273
0
        return 0;
274
275
327M
    if ((loc >= st->num) || (loc < 0)) {
276
327M
        st->data[st->num] = data;
277
327M
    } else {
278
5.32k
        memmove(&st->data[loc + 1], &st->data[loc],
279
5.32k
            sizeof(st->data[0]) * (st->num - loc));
280
5.32k
        st->data[loc] = data;
281
5.32k
    }
282
327M
    st->num++;
283
327M
    st->sorted = 0;
284
327M
    return st->num;
285
327M
}
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
232k
        memmove(&st->data[loc], &st->data[loc + 1],
293
232k
            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
144k
{
301
144k
    int i;
302
303
144k
    if (st == NULL)
304
0
        return NULL;
305
306
656k
    for (i = 0; i < st->num; i++)
307
656k
        if (st->data[i] == p)
308
144k
            return internal_delete(st, i);
309
0
    return NULL;
310
144k
}
311
312
void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc)
313
291k
{
314
291k
    if (st == NULL || loc < 0 || loc >= st->num)
315
0
        return NULL;
316
317
291k
    return internal_delete(st, loc);
318
291k
}
319
320
static int internal_find(OPENSSL_STACK *st, const void *data,
321
    int ret_val_options, int *pnum)
322
23.0k
{
323
23.0k
    const void *r;
324
23.0k
    int i;
325
326
23.0k
    if (st == NULL || st->num == 0)
327
9.17k
        return -1;
328
329
13.8k
    if (st->comp == NULL) {
330
242k
        for (i = 0; i < st->num; i++)
331
242k
            if (st->data[i] == data) {
332
2.92k
                if (pnum != NULL)
333
0
                    *pnum = 1;
334
2.92k
                return i;
335
2.92k
            }
336
243
        if (pnum != NULL)
337
0
            *pnum = 0;
338
243
        return -1;
339
3.16k
    }
340
341
10.7k
    if (!st->sorted) {
342
1.70k
        if (st->num > 1)
343
0
            qsort(st->data, st->num, sizeof(void *), st->comp);
344
1.70k
        st->sorted = 1; /* empty or single-element stack is considered sorted */
345
1.70k
    }
346
10.7k
    if (data == NULL)
347
0
        return -1;
348
10.7k
    if (pnum != NULL)
349
2.30k
        ret_val_options |= OSSL_BSEARCH_FIRST_VALUE_ON_MATCH;
350
10.7k
    r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp,
351
10.7k
        ret_val_options);
352
353
10.7k
    if (pnum != NULL) {
354
2.30k
        *pnum = 0;
355
2.30k
        if (r != NULL) {
356
1.76k
            const void **p = (const void **)r;
357
358
3.53k
            while (p < st->data + st->num) {
359
1.76k
                if (st->comp(&data, p) != 0)
360
0
                    break;
361
1.76k
                ++*pnum;
362
1.76k
                ++p;
363
1.76k
            }
364
1.76k
        }
365
2.30k
    }
366
367
10.7k
    return r == NULL ? -1 : (int)((const void **)r - st->data);
368
10.7k
}
369
370
int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)
371
172k
{
372
172k
    return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, NULL);
373
172k
}
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
102k
{
382
102k
    return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, pnum);
383
102k
}
384
385
int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
386
397M
{
387
397M
    if (st == NULL)
388
0
        return -1;
389
397M
    return OPENSSL_sk_insert(st, data, st->num);
390
397M
}
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
64
{
399
64
    if (st == NULL || st->num == 0)
400
64
        return NULL;
401
0
    return internal_delete(st, 0);
402
64
}
403
404
void *OPENSSL_sk_pop(OPENSSL_STACK *st)
405
2.15M
{
406
2.15M
    if (st == NULL || st->num == 0)
407
10.9k
        return NULL;
408
2.14M
    return internal_delete(st, st->num - 1);
409
2.15M
}
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
48.4M
{
421
48.4M
    int i;
422
423
48.4M
    if (st == NULL)
424
11.1M
        return;
425
215M
    for (i = 0; i < st->num; i++)
426
178M
        if (st->data[i] != NULL)
427
178M
            func((char *)st->data[i]);
428
37.2M
    OPENSSL_sk_free(st);
429
37.2M
}
430
431
void OPENSSL_sk_free(OPENSSL_STACK *st)
432
294M
{
433
294M
    if (st == NULL)
434
55.1M
        return;
435
239M
    OPENSSL_free(st->data);
436
239M
    OPENSSL_free(st);
437
239M
}
438
439
int OPENSSL_sk_num(const OPENSSL_STACK *st)
440
1.20G
{
441
1.20G
    return st == NULL ? -1 : st->num;
442
1.20G
}
443
444
void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)
445
1.33G
{
446
1.33G
    if (st == NULL || i < 0 || i >= st->num)
447
4.94k
        return NULL;
448
1.33G
    return (void *)st->data[i];
449
1.33G
}
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.5M
{
469
23.5M
    if (st != NULL && !st->sorted && st->comp != NULL) {
470
18.8M
        if (st->num > 1)
471
296k
            qsort(st->data, st->num, sizeof(void *), st->comp);
472
18.8M
        st->sorted = 1; /* empty or single-element stack is considered sorted */
473
18.8M
    }
474
23.5M
}
475
476
int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st)
477
83.4k
{
478
83.4k
    return st == NULL ? 1 : st->sorted;
479
83.4k
}