Coverage Report

Created: 2026-10-28 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmexpat/lib/random_dev_urandom.c
Line
Count
Source
1
/*
2
                            __  __            _
3
                         ___\ \/ /_ __   __ _| |_
4
                        / _ \\  /| '_ \ / _` | __|
5
                       |  __//  \| |_) | (_| | |_
6
                        \___/_/\_\ .__/ \__,_|\__|
7
                                 |_| XML parser
8
9
   Copyright (c) 2017-2026 Sebastian Pipping <sebastian@pipping.org>
10
   Copyright (c) 2026      Matthew Fernandez <matthew.fernandez@gmail.com>
11
   Licensed under the MIT license:
12
13
   Permission is  hereby granted,  free of charge,  to any  person obtaining
14
   a  copy  of  this  software   and  associated  documentation  files  (the
15
   "Software"),  to  deal in  the  Software  without restriction,  including
16
   without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17
   distribute, sublicense, and/or sell copies of the Software, and to permit
18
   persons  to whom  the Software  is  furnished to  do so,  subject to  the
19
   following conditions:
20
21
   The above copyright  notice and this permission notice  shall be included
22
   in all copies or substantial portions of the Software.
23
24
   THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25
   EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27
   NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28
   DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29
   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30
   USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32
   SPDX-License-Identifier: MIT
33
*/
34
35
#include "random_dev_urandom.h"
36
37
#if ! defined(_POSIX_C_SOURCE)                                                 \
38
    || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE < 200809L))
39
#  define _POSIX_C_SOURCE 200809L // for O_CLOEXEC
40
#endif
41
42
#include <errno.h>
43
#include <fcntl.h>  // open
44
#include <unistd.h> // close
45
46
/* Extract entropy from /dev/urandom */
47
bool
48
0
writeRandomBytes_dev_urandom(void *target, size_t count) {
49
0
  int success = false; /* full count bytes written? */
50
0
  size_t bytesWrittenTotal = 0;
51
52
0
  const int fd = open("/dev/urandom", O_RDONLY
53
0
#ifdef O_CLOEXEC
54
0
                      | O_CLOEXEC
55
0
#endif
56
0
                      );
57
0
  if (fd < 0) {
58
0
    return false;
59
0
  }
60
61
0
  do {
62
0
    void *const currentTarget = (char *)target + bytesWrittenTotal;
63
0
    const size_t bytesToWrite = count - bytesWrittenTotal;
64
65
0
    errno = 0;
66
67
0
    const ssize_t bytesWrittenMore = read(fd, currentTarget, bytesToWrite);
68
69
0
    if (bytesWrittenMore > 0) {
70
0
      bytesWrittenTotal += bytesWrittenMore;
71
0
      if (bytesWrittenTotal >= count)
72
0
        success = true;
73
0
    }
74
0
  } while (! success && (errno == EINTR));
75
76
0
  close(fd);
77
0
  return success;
78
0
}