Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sal/osl/unx/random.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include <oslrandom.h>
11
12
#include <assert.h>
13
#include <errno.h>
14
#include <fcntl.h>
15
#include <unistd.h>
16
#include <dlfcn.h>
17
18
bool osl_get_system_random_data(char* buffer, size_t desired_len)
19
100k
{
20
100k
    int fd;
21
22
100k
    assert(buffer);
23
24
100k
    static int (*lok_open_urandom)()
25
100k
        = reinterpret_cast<int (*)()>(dlsym(RTLD_DEFAULT, "lok_open_urandom"));
26
100k
    if (!lok_open_urandom || (fd = lok_open_urandom()) < 0)
27
100k
        fd = open("/dev/urandom", O_RDONLY);
28
29
100k
    if (fd != -1)
30
100k
    {
31
201k
        while (desired_len)
32
100k
        {
33
100k
            ssize_t nb_read;
34
100k
            if ((nb_read = read(fd, buffer, desired_len)) < 0)
35
0
            {
36
0
                if (errno != EINTR)
37
0
                {
38
0
                    close(fd);
39
0
                    return false;
40
0
                }
41
0
            }
42
100k
            else
43
100k
            {
44
100k
                buffer += nb_read;
45
100k
                desired_len -= nb_read;
46
100k
            }
47
100k
        }
48
100k
        close(fd);
49
100k
        return true;
50
100k
    }
51
0
    return false;
52
100k
}
53
54
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */