[libc-commits] [PATCH] D147981: [LIBC] Fix `getrandom` success return value

Noah Goldstein via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Apr 10 17:15:09 PDT 2023


goldstein.w.n created this revision.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
goldstein.w.n requested review of this revision.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147981

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


Index: libc/test/src/sys/random/linux/getrandom_test.cpp
===================================================================
--- libc/test/src/sys/random/linux/getrandom_test.cpp
+++ libc/test/src/sys/random/linux/getrandom_test.cpp
@@ -29,6 +29,15 @@
   libc_errno = 0;
 }
 
+TEST(LlvmLibcGetRandomTest, ReturnsSize) {
+  static constexpr size_t SIZE = 8192;
+  uint8_t buf[SIZE];
+  for (size_t i = 0; i < SIZE; i += 1 + (i % 64)) {
+    // 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;
Index: libc/src/sys/random/linux/getrandom.cpp
===================================================================
--- libc/src/sys/random/linux/getrandom.cpp
+++ libc/src/sys/random/linux/getrandom.cpp
@@ -23,7 +23,7 @@
     libc_errno = -ret;
     return -1;
   }
-  return 0;
+  return ret;
 }
 
 } // namespace __llvm_libc


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147981.512290.patch
Type: text/x-patch
Size: 1044 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230411/c2e20c88/attachment.bin>


More information about the libc-commits mailing list