Fix UB in string.bench.cpp.
The usage of aligned_storage failed to pass the alignment it wanted, which caused it to have a larger size and alignment that the std::string's it was intended to store. This patch manually specifies the alignment, as well as cleaning up type alias bugs. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@346779 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -195,21 +195,21 @@ template <class Length>
|
|||||||
struct StringMove {
|
struct StringMove {
|
||||||
static void run(benchmark::State& state) {
|
static void run(benchmark::State& state) {
|
||||||
// Keep two object locations and move construct back and forth.
|
// Keep two object locations and move construct back and forth.
|
||||||
std::aligned_storage<sizeof(std::string)>::type Storage[2];
|
std::aligned_storage<sizeof(std::string), alignof(std::string)>::type Storage[2];
|
||||||
using S = std::string;
|
using S = std::string;
|
||||||
S* Data = reinterpret_cast<S*>(Storage);
|
|
||||||
size_t I = 0;
|
size_t I = 0;
|
||||||
new (static_cast<void*>(Data)) std::string(makeString(Length()));
|
S *newS = new (static_cast<void*>(Storage)) std::string(makeString(Length()));
|
||||||
for (auto _ : state) {
|
for (auto _ : state) {
|
||||||
// Switch locations.
|
// Switch locations.
|
||||||
I ^= 1;
|
I ^= 1;
|
||||||
benchmark::DoNotOptimize(Storage);
|
benchmark::DoNotOptimize(Storage);
|
||||||
// Move construct into the new location,
|
// Move construct into the new location,
|
||||||
new (static_cast<void*>(Storage + I)) S(std::move(Data[I ^ 1]));
|
S *tmpS = new (static_cast<void*>(Storage + I)) S(std::move(*newS));
|
||||||
// then destroy the old one.
|
// then destroy the old one.
|
||||||
Data[I ^ 1].~S();
|
newS->~S();
|
||||||
|
newS = tmpS;
|
||||||
}
|
}
|
||||||
Data[I].~S();
|
newS->~S();
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string name() { return "BM_StringMove" + Length::name(); }
|
static std::string name() { return "BM_StringMove" + Length::name(); }
|
||||||
|
|||||||
Reference in New Issue
Block a user