[Support] Fix undefined behavior in RandomNumberGenerator.
authorZachary Turner <zturner@google.com>
Tue, 11 Oct 2016 18:17:26 +0000 (18:17 +0000)
committerZachary Turner <zturner@google.com>
Tue, 11 Oct 2016 18:17:26 +0000 (18:17 +0000)
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.

llvm-svn: 283912

llvm/lib/Support/RandomNumberGenerator.cpp

index d340764..8ea02d7 100644 (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);