diff options
author | Tor Lillqvist <tml@collabora.com> | 2015-05-06 12:34:17 +0300 |
---|---|---|
committer | Tor Lillqvist <tml@collabora.com> | 2015-05-06 12:39:36 +0300 |
commit | 776d74bb557e243c3aefe868dd5367a2a065ee06 (patch) | |
tree | 2d87994fa00a360b67e444b512015a5c9403bca5 /comphelper | |
parent | 7bd6f298b43732fd5d4a270f2493ae11eb20ad22 (diff) |
If using std::random_device fails, fall back to just time(nullptr)
For instance, if using LibreOfficeKit in a chroot jail, there might
not be a /dev/urandom, which causes the std::random_device ctor to
throw a std::runtime_error exception, at least in the libstdc++ I
have.
Change-Id: Icc91a4ddf92ce66c66b6ffb8b4d1a02ab5f29ee9
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/source/misc/random.cxx | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx index 84e31760a379..50f3ce46a840 100644 --- a/comphelper/source/misc/random.cxx +++ b/comphelper/source/misc/random.cxx @@ -12,9 +12,11 @@ #include <comphelper/random.hxx> #include <rtl/instance.hxx> +#include <rtl/ustring.hxx> #include <assert.h> #include <time.h> #include <random> +#include <stdexcept> // this is nothing but a simple wrapper around // the std::random generators @@ -37,13 +39,21 @@ struct RandomNumberGenerator STD_RNG_ALGO global_rng; RandomNumberGenerator() { - std::random_device rd; - // initialises the state of the global random number generator - // should only be called once. - // (note, a few std::variate_generator<> (like normal) have their - // own state which would need a reset as well to guarantee identical - // sequence of numbers, e.g. via myrand.distribution().reset()) - global_rng.seed(rd() ^ time(nullptr)); + try + { + std::random_device rd; + // initialises the state of the global random number generator + // should only be called once. + // (note, a few std::variate_generator<> (like normal) have their + // own state which would need a reset as well to guarantee identical + // sequence of numbers, e.g. via myrand.distribution().reset()) + global_rng.seed(rd() ^ time(nullptr)); + } + catch (std::runtime_error& e) + { + SAL_WARN("comphelper.random", OUString("Using std::random_device failed: ") << e.what()); + global_rng.seed(time(nullptr)); + } } }; |