[libc-commits] [libc] ee361a3 - [LIBC] Fix `getrandom` success return value

Noah Goldstein via libc-commits libc-commits at lists.llvm.org
Wed Apr 12 17:04:58 PDT 2023


Author: Noah Goldstein
Date: 2023-04-12T19:04:42-05:00
New Revision: ee361a3fabb1832ef080fceea07a2451c399c6e3

URL: https://github.com/llvm/llvm-project/commit/ee361a3fabb1832ef080fceea07a2451c399c6e3
DIFF: https://github.com/llvm/llvm-project/commit/ee361a3fabb1832ef080fceea07a2451c399c6e3.diff

LOG: [LIBC] Fix `getrandom` success return value

`getrandom` should return the number of bytes successfully set on
success, not `0`.

Reviewed By: sivachandra, michaelrj

Differential Revision: https://reviews.llvm.org/D147981

Added: 
    

Modified: 
    libc/src/sys/random/linux/getrandom.cpp
    libc/test/src/sys/random/linux/getrandom_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/sys/random/linux/getrandom.cpp b/libc/src/sys/random/linux/getrandom.cpp
index 58a72fc918522..65b6482ad1dfc 100644
--- a/libc/src/sys/random/linux/getrandom.cpp
+++ b/libc/src/sys/random/linux/getrandom.cpp
@@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(ssize_t, getrandom,
     libc_errno = -ret;
     return -1;
   }
-  return 0;
+  return ret;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/test/src/sys/random/linux/getrandom_test.cpp b/libc/test/src/sys/random/linux/getrandom_test.cpp
index 34b663627fe64..52586313be660 100644
--- a/libc/test/src/sys/random/linux/getrandom_test.cpp
+++ b/libc/test/src/sys/random/linux/getrandom_test.cpp
@@ -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;


        


More information about the libc-commits mailing list