diff options
-rw-r--r-- | libcxx/src/random.cpp | 17 | ||||
-rw-r--r-- | libcxx/test/numerics/rand/rand.device/eval.pass.cpp | 16 |
2 files changed, 30 insertions, 3 deletions
diff --git a/libcxx/src/random.cpp b/libcxx/src/random.cpp index 21e21689a87..86017ef0d46 100644 --- a/libcxx/src/random.cpp +++ b/libcxx/src/random.cpp @@ -62,7 +62,22 @@ unsigned random_device::operator()() { unsigned r; - read(__f_, &r, sizeof(r)); + size_t n = sizeof(r); + char* p = reinterpret_cast<char*>(&r); + while (n > 0) + { + ssize_t s = read(__f_, p, n); + if (s == 0) + __throw_system_error(ENODATA, "random_device got EOF"); + if (s == -1) + { + if (errno != EINTR) + __throw_system_error(errno, "random_device got an unexpected error"); + continue; + } + n -= static_cast<size_t>(s); + p += static_cast<size_t>(s); + } return r; } #endif // defined(_WIN32) diff --git a/libcxx/test/numerics/rand/rand.device/eval.pass.cpp b/libcxx/test/numerics/rand/rand.device/eval.pass.cpp index 2422635c92f..72aff076a5d 100644 --- a/libcxx/test/numerics/rand/rand.device/eval.pass.cpp +++ b/libcxx/test/numerics/rand/rand.device/eval.pass.cpp @@ -18,6 +18,18 @@ int main() { - std::random_device r; - std::random_device::result_type e = r(); + { + std::random_device r; + std::random_device::result_type e = r(); + } + + try + { + std::random_device r("/dev/null"); + r(); + assert(false); + } + catch (const std::system_error& e) + { + } } |