Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit f3bddd1

Browse files
authored
fix: use a safer token to workaround libstdc++ bug (#272)
The workaround put in place in #208 does not work with g++-9.3.1 on Fedora:31. Despite this being a "newer" version of libstdc++, it does not support `rdrand` as a token for `std::random_device` maybe because the CPU specific tokens are not enabled. For now, use `/dev/urandom` which seems to be supported on all Linux distros. On other platforms we continue to use the default constructor for `std::random_device`. It might be possible to refine this and detect, at run-time, if `rdrand` is supported, but that seems like an improvement that can wait a little bit.
1 parent e1a58fb commit f3bddd1

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

google/cloud/internal/random.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,20 @@ std::vector<unsigned int> FetchEntropy(std::size_t desired_bits) {
5555
// [4]: https://linux.die.net/man/3/arc4random
5656
// [5]: https://en.wikipedia.org/wiki/NaCl_(software)
5757
//
58-
#if defined(__GLIBCXX__) && __GLIBCXX__ >= 20200128
58+
#if defined(__linux) && defined(__GLIBCXX__) && __GLIBCXX__ >= 20200128
5959
// Workaround for a libstd++ bug:
6060
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94087
61-
// we cannot simply use `rdrand` everywhere because this is library and
62-
// version specific, i.e., other standard C++ libraries do not support
63-
// `rdrand`, and even older versions of libstdc++ do not support `rdrand`.
64-
std::random_device rd("rdrand");
61+
// I (@coryan) would like to use `rdrand` as the token, but:
62+
// - `rdrand` is not supported by all versions of libstdc++
63+
// - even for the versions that do support it, that is CPU specific
64+
// - I know of no reliable way to detect if the library version supports
65+
// `rdrand` (other than trying and getting an exception), for
66+
// example:
67+
// * __GLIBCXX__ is 2020318 on Fedora:31 with g++-9.3.1, but it does
68+
// *not* support `rdrand`
69+
// * __GLIBCXX__ is 20200306 on openSUSE/Tumbleweed, with g++-9.2.1,
70+
// but it *does* support `rdrand`
71+
std::random_device rd("/dev/urandom");
6572
#else
6673
std::random_device rd;
6774
#endif // defined(__GLIBCXX__) && __GLIBCXX__ >= 20200128

0 commit comments

Comments
 (0)