diff --git a/test/archive/test_ipc.cpp b/test/archive/test_ipc.cpp index 00dcd27..d69c69a 100755 --- a/test/archive/test_ipc.cpp +++ b/test/archive/test_ipc.cpp @@ -84,22 +84,34 @@ void test_basic(char const * name) { EXPECT_EQ(que2.recv(), test2); } -class data_set { - alignas(rand_buf) char datas_[sizeof(rand_buf[LoopCount])]; - rand_buf *d_; - -public: - data_set() { - d_ = ::new(datas_) rand_buf[LoopCount]; - for (int i = 0; i < LoopCount; ++i) { - d_[i].set_id(i); - } - } - - rand_buf const *get() const noexcept { - return d_; - } -} const data_set__; +class data_set { + alignas(rand_buf) char datas_[sizeof(rand_buf[LoopCount])]; + rand_buf *d_; + +public: + data_set() { + // Use individual placement new instead of array placement new + // Array placement new adds a cookie (array size) before the array, + // which would overflow the buffer. MSVC's implementation stores this + // cookie, causing buffer overflow and subsequent memory corruption. + d_ = reinterpret_cast(datas_); + for (int i = 0; i < LoopCount; ++i) { + ::new(d_ + i) rand_buf; + d_[i].set_id(i); + } + } + + ~data_set() { + // Manually destroy each element since we used individual placement new + for (int i = 0; i < LoopCount; ++i) { + d_[i].~rand_buf(); + } + } + + rand_buf const *get() const noexcept { + return d_; + } +} const data_set__; template > void test_sr(char const * name, int s_cnt, int r_cnt) {