[libcxx-commits] [PATCH] D94953: [libc++] Use ioctl when available to get random_device entropy.

Marek Kurdej via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 19 12:49:52 PST 2021


curdeius added inline comments.


================
Comment at: libcxx/src/random.cpp:33
 #include <unistd.h>
+#if __has_include(<unistd.h>) && __has_include(<linux/random.h>)
+#include <sys/ioctl.h>
----------------
ldionne wrote:
> Should this be `__has_include(<sys/ioctl.h>)` instead?
Oops, good catch.


================
Comment at: libcxx/test/std/numerics/rand/rand.device/entropy.pass.cpp:25-26
+  double e = r.entropy();
+  assert(e >= 0);
+  assert(e <= 32);
 
----------------
ldionne wrote:
> I think we want to make those be `LIBCPP_ASSERT(...)` instead, since the entropy is technically not mandated by the standard. It's just that our implementation behaves that way.
The standard says this http://eel.is/c++draft/rand.device#5. So entropy should be between min and log2(max+1), where max is max of result_type.
At the same time, http://eel.is/c++draft/rand.device#2 says result_type=unsigned int.
So the only thing that can change is the size of unsigned int (which can be non-32-bit somewhere).
To be precise I should rather `assert(e <= sizeof(result_type) * CHAR_BIT)`.
And it should be for whatever implementation of stdlib.
WDYT?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94953/new/

https://reviews.llvm.org/D94953



More information about the libcxx-commits mailing list