Skip to content

Commit 701317f

Browse files
authored
Merge pull request #3153 from mgreter/bugfix/harden-get-seed
Fix seed generator if std::random_device fails (#3151) Closes #3151
2 parents a0b3c59 + 60e7fb0 commit 701317f

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/fn_numbers.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include <cstdint>
66
#include <cstdlib>
77
#include <cmath>
8+
#include <ctime>
89
#include <random>
910
#include <sstream>
1011
#include <iomanip>
1112
#include <algorithm>
13+
#include <thread>
1214

1315
#include "ast.hpp"
1416
#include "units.hpp"
@@ -41,8 +43,25 @@ namespace Sass {
4143
#else
4244
uint64_t GetSeed()
4345
{
44-
std::random_device rd;
45-
return rd();
46+
// Init universe entropy
47+
uint64_t rnd = 42;
48+
// Try to get random number from system
49+
try {
50+
std::random_device rd;
51+
rnd = rd();
52+
}
53+
// On certain system this can throw since either
54+
// underlying hardware or software can be buggy.
55+
// https://github.com/sass/libsass/issues/3151
56+
catch (std::exception&) {
57+
}
58+
// Don't trust anyone to be random, so we
59+
// add a little entropy of our own.
60+
rnd ^= std::time(NULL) ^ std::clock() ^
61+
std::hash<std::thread::id>()
62+
(std::this_thread::get_id());
63+
// Return entropy
64+
return rnd;
4665
}
4766
#endif
4867

0 commit comments

Comments
 (0)