Skip to content

Commit 5420382

Browse files
author
Sriya Pratipati
committed
fixed internal error handling and moved implementation to header file
1 parent 0ec5b33 commit 5420382

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

libc/src/__support/HashTable/randomness.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "src/__support/macros/attributes.h"
1515
#include "src/__support/macros/config.h"
1616
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
17-
#include "src/__support/OSUtil/getrandom.h"
17+
#include "src/__support/OSUtil/linux/getrandom.h"
1818
#include "src/__support/libc_errno.h"
1919
#endif
2020

@@ -39,14 +39,14 @@ LIBC_INLINE uint64_t next_random_seed() {
3939
size_t count = sizeof(entropy);
4040
uint8_t *buffer = reinterpret_cast<uint8_t *>(entropy);
4141
while (count > 0) {
42-
ssize_t len = internal::getrandom(buffer, count, 0);
43-
if (len == -1) {
42+
auto len = internal::getrandom(buffer, count, 0);
43+
if (!len.has_value()) {
4444
if (libc_errno == ENOSYS)
4545
break;
4646
continue;
4747
}
48-
count -= len;
49-
buffer += len;
48+
count -= len.value();
49+
buffer += len.value();
5050
}
5151
libc_errno = errno_backup;
5252
#endif

libc/src/__support/OSUtil/getrandom.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

libc/src/__support/OSUtil/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ add_object_library(
99
SRCS
1010
exit.cpp
1111
fcntl.cpp
12-
getrandom.cpp
1312
HDRS
1413
io.h
1514
syscall.h
15+
getrandom.h
1616
DEPENDS
1717
.${LIBC_TARGET_ARCHITECTURE}.linux_${LIBC_TARGET_ARCHITECTURE}_util
1818
libc.src.__support.common
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
//===------------ Linux implementation of getrandom -------------*- C++ -*-===//
1+
//===------------ Implementation of getrandom function ----------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/OSUtil/getrandom.h"
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
11+
12+
#include "hdr/types/ssize_t.h"
1013
#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
1114
#include "src/__support/common.h"
12-
#include "src/__support/libc_errno.h"
15+
#include "src/__support/error_or.h"
1316
#include "src/__support/macros/config.h"
14-
#include <sys/syscall.h> // For syscall numbers.
17+
#include <sys/syscall.h> // For syscall numbers
1518

1619
namespace LIBC_NAMESPACE_DECL {
1720
namespace internal {
1821

19-
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
22+
static inline ErrorOr<ssize_t> getrandom(void *buf, size_t buflen,
23+
unsigned int flags) {
2024
ssize_t ret =
2125
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags);
2226
if (ret < 0) {
23-
libc_errno = static_cast<int>(-ret);
24-
return -1;
27+
return Error(-static_cast<int>(ret));
2528
}
2629
return ret;
2730
}
2831

2932
} // namespace internal
3033
} // namespace LIBC_NAMESPACE_DECL
34+
35+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H

libc/src/sys/random/linux/getrandom.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
#include "src/sys/random/getrandom.h"
1010

11-
#include "src/__support/OSUtil/getrandom.h"
11+
#include "src/__support/OSUtil/linux/getrandom.h"
1212
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1313
#include "src/__support/common.h"
14+
#include "src/__support/error_or.h"
1415

1516
#include "src/__support/libc_errno.h"
1617
#include "src/__support/macros/config.h"
@@ -20,7 +21,11 @@ namespace LIBC_NAMESPACE_DECL {
2021

2122
LLVM_LIBC_FUNCTION(ssize_t, getrandom,
2223
(void *buf, size_t buflen, unsigned int flags)) {
23-
return internal::getrandom(buf, buflen, flags);
24+
auto rand = internal::getrandom(buf, buflen, flags);
25+
if (rand.has_value())
26+
return rand.value();
27+
libc_errno = static_cast<int>(-(rand.error()));
28+
return -1;
2429
}
2530

2631
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)