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
This commit is contained in:
Tor Lillqvist
2015-05-06 12:34:17 +03:00
parent 7bd6f298b4
commit 776d74bb55

View File

@@ -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
@@ -36,6 +38,8 @@ struct RandomNumberGenerator
{
STD_RNG_ALGO global_rng;
RandomNumberGenerator()
{
try
{
std::random_device rd;
// initialises the state of the global random number generator
@@ -45,6 +49,12 @@ struct RandomNumberGenerator
// 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));
}
}
};
class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator> {};