@@ -58,15 +58,18 @@ void test() {
5858 {
5959 {
6060 std::unique_ptr<WithCookie[]> ptr (new WithCookie[5 ]);
61+ assert (&ptr[1 ] == ptr.get () + 1 ); // ensure no assertion
6162 TEST_LIBCPP_ASSERT_FAILURE (ptr[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
6263 }
6364 {
6465 std::unique_ptr<WithCookie[]> ptr = std::make_unique<WithCookie[]>(5 );
66+ assert (&ptr[1 ] == ptr.get () + 1 ); // ensure no assertion
6567 TEST_LIBCPP_ASSERT_FAILURE (ptr[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
6668 }
6769#if TEST_STD_VER >= 20
6870 {
6971 std::unique_ptr<WithCookie[]> ptr = std::make_unique_for_overwrite<WithCookie[]>(5 );
72+ assert (&ptr[1 ] == ptr.get () + 1 ); // ensure no assertion
7073 TEST_LIBCPP_ASSERT_FAILURE (ptr[6 ] = WithCookie (), " unique_ptr<T[]>::operator[](index): index out of range" );
7174 }
7275#endif
@@ -82,11 +85,13 @@ void test() {
8285 {
8386 {
8487 std::unique_ptr<NoCookie[]> ptr = std::make_unique<NoCookie[]>(5 );
88+ assert (&ptr[1 ] == ptr.get () + 1 ); // ensure no assertion
8589 TEST_LIBCPP_ASSERT_FAILURE (ptr[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
8690 }
8791# if TEST_STD_VER >= 20
8892 {
8993 std::unique_ptr<NoCookie[]> ptr = std::make_unique_for_overwrite<NoCookie[]>(5 );
94+ assert (&ptr[1 ] == ptr.get () + 1 ); // ensure no assertion
9095 TEST_LIBCPP_ASSERT_FAILURE (ptr[6 ] = NoCookie (), " unique_ptr<T[]>::operator[](index): index out of range" );
9196 }
9297# endif
@@ -101,6 +106,7 @@ void test() {
101106 {
102107 std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5 );
103108 std::unique_ptr<T[]> other (std::move (ptr));
109+ assert (&other[1 ] == other.get () + 1 ); // ensure no assertion
104110 TEST_LIBCPP_ASSERT_FAILURE (other[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
105111 }
106112
@@ -109,13 +115,15 @@ void test() {
109115 std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5 );
110116 std::unique_ptr<T[]> other;
111117 other = std::move (ptr);
118+ assert (&other[1 ] == other.get () + 1 ); // ensure no assertion
112119 TEST_LIBCPP_ASSERT_FAILURE (other[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
113120 }
114121
115122 // Bounds carried through converting move-constructor
116123 {
117124 std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5 );
118125 std::unique_ptr<T[], MyDeleter> other (std::move (ptr));
126+ assert (&other[1 ] == other.get () + 1 ); // ensure no assertion
119127 TEST_LIBCPP_ASSERT_FAILURE (other[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
120128 }
121129
@@ -124,6 +132,7 @@ void test() {
124132 std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5 );
125133 std::unique_ptr<T[], MyDeleter> other;
126134 other = std::move (ptr);
135+ assert (&other[1 ] == other.get () + 1 ); // ensure no assertion
127136 TEST_LIBCPP_ASSERT_FAILURE (other[6 ], " unique_ptr<T[]>::operator[](index): index out of range" );
128137 }
129138 });
@@ -144,6 +153,34 @@ struct WithCookie {
144153 char padding[Size];
145154};
146155
156+ template <std::size_t Size>
157+ struct alignas (128 ) OveralignedNoCookie {
158+ char padding[Size];
159+ };
160+
161+ template <std::size_t Size>
162+ struct alignas (128 ) OveralignedWithCookie {
163+ OveralignedWithCookie () = default ;
164+ OveralignedWithCookie (OveralignedWithCookie const &) {}
165+ OveralignedWithCookie& operator =(OveralignedWithCookie const &) { return *this ; }
166+ ~OveralignedWithCookie () {}
167+ char padding[Size];
168+ };
169+
170+ // These types have a different ABI alignment (alignof) and preferred alignment (__alignof) on some platforms.
171+ // Make sure things work with these types because array cookies can be sensitive to preferred alignment on some
172+ // platforms.
173+ struct WithCookiePreferredAlignment {
174+ WithCookiePreferredAlignment () = default ;
175+ WithCookiePreferredAlignment (WithCookiePreferredAlignment const &) {}
176+ WithCookiePreferredAlignment& operator =(WithCookiePreferredAlignment const &) { return *this ; }
177+ ~WithCookiePreferredAlignment () {}
178+ long double data;
179+ };
180+ struct NoCookiePreferredAlignment {
181+ long double data;
182+ };
183+
147184int main (int , char **) {
148185 test<WithCookie<1 >, NoCookie<1 >>();
149186 test<WithCookie<2 >, NoCookie<2 >>();
@@ -153,7 +190,18 @@ int main(int, char**) {
153190 test<WithCookie<16 >, NoCookie<16 >>();
154191 test<WithCookie<32 >, NoCookie<32 >>();
155192 test<WithCookie<256 >, NoCookie<256 >>();
193+
194+ test<OveralignedWithCookie<1 >, OveralignedNoCookie<1 >>();
195+ test<OveralignedWithCookie<2 >, OveralignedNoCookie<2 >>();
196+ test<OveralignedWithCookie<3 >, OveralignedNoCookie<3 >>();
197+ test<OveralignedWithCookie<4 >, OveralignedNoCookie<4 >>();
198+ test<OveralignedWithCookie<8 >, OveralignedNoCookie<8 >>();
199+ test<OveralignedWithCookie<16 >, OveralignedNoCookie<16 >>();
200+ test<OveralignedWithCookie<32 >, OveralignedNoCookie<32 >>();
201+ test<OveralignedWithCookie<256 >, OveralignedNoCookie<256 >>();
202+
156203 test<std::string, int >();
204+ test<WithCookiePreferredAlignment, NoCookiePreferredAlignment>();
157205
158206 return 0 ;
159207}
0 commit comments