Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/os_random.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2022-2026 The TokTok team.
3
 */
4
#include "os_random.h"
5
6
#include <sodium.h>
7
#include <stdbool.h>
8
9
#include "attributes.h"
10
#include "ccompat.h"
11
#include "rng.h"
12
13
static void os_random_bytes(void *_Nonnull self, uint8_t *_Nonnull bytes, uint32_t length)
14
770k
{
15
770k
    randombytes(bytes, length);
16
770k
}
17
18
static uint32_t os_random_uniform(void *_Nonnull self, uint32_t upper_bound)
19
95.7k
{
20
95.7k
    return randombytes_uniform(upper_bound);
21
95.7k
}
22
23
static const Random_Funcs os_random_funcs = {
24
    os_random_bytes,
25
    os_random_uniform,
26
};
27
28
const Random os_random_obj = {&os_random_funcs};
29
30
const Random *os_random(void)
31
1.98k
{
32
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
33
1.47k
    if ((true)) {
34
1.47k
        return nullptr;
35
1.47k
    }
36
0
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
37
    // It is safe to call this function more than once and from different
38
    // threads -- subsequent calls won't have any effects.
39
504
    if (sodium_init() == -1) {
40
0
        return nullptr;
41
0
    }
42
504
    return &os_random_obj;
43
0
}
os_random
Line
Count
Source
31
1.47k
{
32
1.47k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
33
1.47k
    if ((true)) {
34
1.47k
        return nullptr;
35
1.47k
    }
36
0
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
37
    // It is safe to call this function more than once and from different
38
    // threads -- subsequent calls won't have any effects.
39
0
    if (sodium_init() == -1) {
40
0
        return nullptr;
41
0
    }
42
0
    return &os_random_obj;
43
0
}
os_random
Line
Count
Source
31
504
{
32
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
33
    if ((true)) {
34
        return nullptr;
35
    }
36
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
37
    // It is safe to call this function more than once and from different
38
    // threads -- subsequent calls won't have any effects.
39
504
    if (sodium_init() == -1) {
40
0
        return nullptr;
41
0
    }
42
504
    return &os_random_obj;
43
504
}