Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bsearch.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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 <stddef.h>
11
#include "internal/cryptlib.h"
12
13
typedef int (*cmpthunk_fn)(const void *, const void *);
14
const void *ossl_bsearch(const void *key, const void *base, int num,
15
    int size, int (*cmp)(const void *, const void *),
16
    int (*cmp_thunk)(cmpthunk_fn real_cmp_fn, const void *, const void *),
17
    int flags)
18
14.3M
{
19
14.3M
    const char *base_ = base;
20
14.3M
    int l, h, i = 0, c = 0;
21
14.3M
    const char *p = NULL;
22
23
14.3M
    if (num == 0)
24
0
        return NULL;
25
14.3M
    l = 0;
26
14.3M
    h = num;
27
138M
    while (l < h) {
28
130M
        i = l + (h - l) / 2;
29
130M
        p = &(base_[i * size]);
30
130M
        if (cmp_thunk != NULL)
31
39.5k
            c = cmp_thunk((cmpthunk_fn)cmp, key, (const void *)p);
32
130M
        else
33
130M
            c = cmp(key, p);
34
130M
        if (c < 0)
35
89.5M
            h = i;
36
40.6M
        else if (c > 0)
37
34.6M
            l = i + 1;
38
5.99M
        else
39
5.99M
            break;
40
130M
    }
41
14.3M
    if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH))
42
8.31M
        p = NULL;
43
5.99M
    else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) {
44
48.8k
        while (i > 0) {
45
11.7k
            if (cmp_thunk != NULL) {
46
703
                if (cmp_thunk((cmpthunk_fn)cmp, key, (const void *)&(base_[(i - 1) * size])))
47
186
                    break;
48
11.0k
            } else {
49
11.0k
                if (cmp(key, &(base_[(i - 1) * size])))
50
11.0k
                    break;
51
11.0k
            }
52
517
            i--;
53
517
        }
54
48.3k
        p = &(base_[i * size]);
55
48.3k
    }
56
14.3M
    return p;
57
14.3M
}