Coverage Report

Created: 2025-12-04 06:33

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