Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
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.9M
{
17
59.9M
    const char *base_ = base;
18
59.9M
    int l, h, i = 0, c = 0;
19
59.9M
    const char *p = NULL;
20
21
59.9M
    if (num == 0)
22
0
        return NULL;
23
59.9M
    l = 0;
24
59.9M
    h = num;
25
503M
    while (l < h) {
26
470M
        i = l + (h - l) / 2;
27
470M
        p = &(base_[i * size]);
28
470M
        c = (*cmp)(key, p);
29
470M
        if (c < 0)
30
309M
            h = i;
31
161M
        else if (c > 0)
32
133M
            l = i + 1;
33
27.9M
        else
34
27.9M
            break;
35
470M
    }
36
59.9M
    if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH))
37
32.0M
        p = NULL;
38
27.9M
    else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) {
39
105k
        while (i > 0 && (*cmp)(key, &(base_[(i - 1) * size])) == 0)
40
3.02k
            i--;
41
102k
        p = &(base_[i * size]);
42
102k
    }
43
59.9M
    return p;
44
59.9M
}