summaryrefslogtreecommitdiffstats
path: root/libcxx/benchmarks/string.bench.cpp
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2018-11-13 19:16:19 +0000
committerEric Fiselier <eric@efcs.ca>2018-11-13 19:16:19 +0000
commit3bea50aeb35704798aefa9acbb8973c7864f3732 (patch)
treee0e5afc553d0fdac6d12000717da77fbeb594b79 /libcxx/benchmarks/string.bench.cpp
parentf98ba05f3d01517c9fee4e64da13101fccb821b7 (diff)
downloadbcm5719-llvm-3bea50aeb35704798aefa9acbb8973c7864f3732.tar.gz
bcm5719-llvm-3bea50aeb35704798aefa9acbb8973c7864f3732.zip
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. llvm-svn: 346779
Diffstat (limited to 'libcxx/benchmarks/string.bench.cpp')
-rw-r--r--libcxx/benchmarks/string.bench.cpp12
1 files changed, 6 insertions, 6 deletions
diff --git a/libcxx/benchmarks/string.bench.cpp b/libcxx/benchmarks/string.bench.cpp
index d7f919b1499..b8f97e6e2c1 100644
--- a/libcxx/benchmarks/string.bench.cpp
+++ b/libcxx/benchmarks/string.bench.cpp
@@ -195,21 +195,21 @@ template <class Length>
struct StringMove {
static void run(benchmark::State& state) {
// 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;
- S* Data = reinterpret_cast<S*>(Storage);
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) {
// Switch locations.
I ^= 1;
benchmark::DoNotOptimize(Storage);
// 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.
- Data[I ^ 1].~S();
+ newS->~S();
+ newS = tmpS;
}
- Data[I].~S();
+ newS->~S();
}
static std::string name() { return "BM_StringMove" + Length::name(); }
OpenPOWER on IntegriCloud