Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
const void *ossl_bsearch(const void *key, const void *base, int num,
14
    int size, int (*cmp)(const void *, const void *),
15
    int flags)
16
58.4M
{
17
58.4M
    const char *base_ = base;
18
58.4M
    int l, h, i = 0, c = 0;
19
58.4M
    const char *p = NULL;
20
21
58.4M
    if (num == 0)
22
0
        return NULL;
23
58.4M
    l = 0;
24
58.4M
    h = num;
25
485M
    while (l < h) {
26
454M
        i = (l + h) / 2;
27
454M
        p = &(base_[i * size]);
28
454M
        c = (*cmp)(key, p);
29
454M
        if (c < 0)
30
296M
            h = i;
31
157M
        else if (c > 0)
32
130M
            l = i + 1;
33
27.6M
        else
34
27.6M
            break;
35
454M
    }
36
58.4M
    if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH))
37
30.8M
        p = NULL;
38
27.6M
    else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) {
39
100k
        while (i > 0 && (*cmp)(key, &(base_[(i - 1) * size])) == 0)
40
3.32k
            i--;
41
97.5k
        p = &(base_[i * size]);
42
97.5k
    }
43
58.4M
    return p;
44
58.4M
}