Skip to content

Commit

Permalink
[LIBC] Fix getrandom success return value
Browse files Browse the repository at this point in the history
`getrandom` should return the number of bytes successfully set on
success, not `0`.

Reviewed By: sivachandra, michaelrj

Differential Revision: https://reviews.llvm.org/D147981
  • Loading branch information
goldsteinn committed Apr 13, 2023
1 parent 203aff2 commit ee361a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libc/src/sys/random/linux/getrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(ssize_t, getrandom,
libc_errno = -ret;
return -1;
}
return 0;
return ret;
}

} // namespace __llvm_libc
9 changes: 9 additions & 0 deletions libc/test/src/sys/random/linux/getrandom_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ TEST(LlvmLibcGetRandomTest, InvalidBuffer) {
libc_errno = 0;
}

TEST(LlvmLibcGetRandomTest, ReturnsSize) {
static constexpr size_t SIZE = 8192;
uint8_t buf[SIZE];
for (size_t i = 0; i < SIZE; ++i) {
// Without GRND_RANDOM set this should never fail.
ASSERT_EQ(__llvm_libc::getrandom(buf, i, 0), static_cast<ssize_t>(i));
}
}

TEST(LlvmLibcGetRandomTest, PiEstimation) {
static constexpr size_t LIMIT = 10000000;
static constexpr double PI = 3.14159265358979;
Expand Down

0 comments on commit ee361a3

Please sign in to comment.