Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bsearch.c
Line
Count
Source
1
/*
2
 * Copyright 2019 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
12
{
19
12
    const char *base_ = base;
20
12
    int l, h, i = 0, c = 0;
21
12
    const char *p = NULL;
22
23
12
    if (num == 0)
24
0
        return NULL;
25
12
    l = 0;
26
12
    h = num;
27
24
    while (l < h) {
28
12
        i = (l + h) / 2;
29
12
        p = &(base_[i * size]);
30
12
        if (cmp_thunk != NULL)
31
12
            c = cmp_thunk((cmpthunk_fn)cmp, key, (const void *)p);
32
0
        else
33
0
            c = cmp(key, p);
34
12
        if (c < 0)
35
0
            h = i;
36
12
        else if (c > 0)
37
12
            l = i + 1;
38
0
        else
39
0
            break;
40
12
    }
41
12
    if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH))
42
12
        p = NULL;
43
0
    else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) {
44
0
        while (i > 0) {
45
0
            if (cmp_thunk != NULL) {
46
0
                if (cmp_thunk((cmpthunk_fn)cmp, key, (const void *)&(base_[(i - 1) * size])))
47
0
                    break;
48
0
            } else {
49
0
                if (cmp(key, &(base_[(i - 1) * size])))
50
0
                    break;
51
0
            }
52
0
            i--;
53
0
        }
54
0
        p = &(base_[i * size]);
55
0
    }
56
12
    return p;
57
12
}