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