comphelper/source/misc/random.cxx | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
New commits: commit 776d74bb557e243c3aefe868dd5367a2a065ee06 Author: Tor Lillqvist <[email protected]> Date: Wed May 6 12:34:17 2015 +0300 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 diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx index 84e3176..50f3ce4 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)); + } } }; _______________________________________________ Libreoffice-commits mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
