xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/reserve_size.asan.pass.cpp (revision 9ed20568e7de53dce85f1631d7d8c1415e7930ae)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <string>
10 
11 // This test verifies that the ASan annotations for basic_string objects remain accurate
12 // after invoking basic_string::reserve(size_type __requested_capacity).
13 // Different types are used to confirm that ASan works correctly with types of different sizes.
14 #include <string>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "asan_testing.h"
19 
20 template <class S>
test()21 void test() {
22   S short_s1(3, 'a'), long_s1(100, 'c');
23   short_s1.reserve(0x1337);
24   long_s1.reserve(0x1337);
25 
26   LIBCPP_ASSERT(is_string_asan_correct(short_s1));
27   LIBCPP_ASSERT(is_string_asan_correct(long_s1));
28 
29   short_s1.clear();
30   long_s1.clear();
31 
32   LIBCPP_ASSERT(is_string_asan_correct(short_s1));
33   LIBCPP_ASSERT(is_string_asan_correct(long_s1));
34 
35   short_s1.reserve(0x1);
36   long_s1.reserve(0x1);
37 
38   LIBCPP_ASSERT(is_string_asan_correct(short_s1));
39   LIBCPP_ASSERT(is_string_asan_correct(long_s1));
40 
41   S short_s2(3, 'a'), long_s2(100, 'c');
42   short_s2.reserve(0x1);
43   long_s2.reserve(0x1);
44 
45   LIBCPP_ASSERT(is_string_asan_correct(short_s2));
46   LIBCPP_ASSERT(is_string_asan_correct(long_s2));
47 }
48 
main(int,char **)49 int main(int, char**) {
50   test<std::string>();
51 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
52   test<std::wstring>();
53 #endif
54 #if TEST_STD_VER >= 11
55   test<std::u16string>();
56   test<std::u32string>();
57 #endif
58 #if TEST_STD_VER >= 20
59   test<std::u8string>();
60 #endif
61 
62   return 0;
63 }
64