Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
59.5M
{
17
59.5M
    const char *base_ = base;
18
59.5M
    int l, h, i = 0, c = 0;
19
59.5M
    const char *p = NULL;
20
21
59.5M
    if (num == 0)
22
0
        return NULL;
23
59.5M
    l = 0;
24
59.5M
    h = num;
25
499M
    while (l < h) {
26
467M
        i = l + (h - l) / 2;
27
467M
        p = &(base_[i * size]);
28
467M
        c = (*cmp)(key, p);
29
467M
        if (c < 0)
30
307M
            h = i;
31
160M
        else if (c > 0)
32
132M
            l = i + 1;
33
27.5M
        else
34
27.5M
            break;
35
467M
    }
36
59.5M
    if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH))
37
32.0M
        p = NULL;
38
27.5M
    else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) {
39
101k
        while (i > 0 && (*cmp)(key, &(base_[(i - 1) * size])) == 0)
40
3.53k
            i--;
41
98.3k
        p = &(base_[i * size]);
42
98.3k
    }
43
59.5M
    return p;
44
59.5M
}