[Support] Fix undefined behavior in RandomNumberGenerator.

This has existed pretty much forever AFAICT, but the code was
never being exercised because nobody was using the class.  A
user of this class surfaced, and now we're breaking with UB.
The code was obviously wrong, so it's fixed here.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283912 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Zachary Turner 2016-10-11 18:17:26 +00:00
parent 749baf326d
commit be78316854

View File

@ -47,11 +47,11 @@ RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
// are using a 64-bit RNG. This isn't a problem since the Mersenne
// twister constructor copies these correctly into its initial state.
std::vector<uint32_t> Data;
Data.reserve(2 + Salt.size());
Data.push_back(Seed);
Data.push_back(Seed >> 32);
Data.resize(2 + Salt.size());
Data[0] = Seed;
Data[1] = Seed >> 32;
std::copy(Salt.begin(), Salt.end(), Data.end());
std::copy(Salt.begin(), Salt.end(), Data.begin() + 2);
std::seed_seq SeedSeq(Data.begin(), Data.end());
Generator.seed(SeedSeq);