1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: clang-3.3, clang-3.4, clang-3.5
11
12 // <vector>
13
14 // reference operator[](size_type n);
15
16 #include <vector>
17 #include <cassert>
18 #include <cstdlib>
19
20 #include "asan_testing.h"
21 #include "min_allocator.h"
22 #include "test_iterators.h"
23 #include "test_macros.h"
24
25 #ifndef _LIBCPP_HAS_NO_ASAN
26 extern "C" void __sanitizer_set_death_callback(void (*callback)(void));
27
do_exit()28 void do_exit() {
29 exit(0);
30 }
31
main()32 int main()
33 {
34 #if TEST_STD_VER >= 11
35 {
36 typedef int T;
37 typedef std::vector<T, min_allocator<T>> C;
38 const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
39 C c(std::begin(t), std::end(t));
40 c.reserve(2*c.size());
41 T foo = c[c.size()]; // bad, but not caught by ASAN
42 }
43 #endif
44
45 {
46 typedef input_iterator<int*> MyInputIter;
47 // Sould not trigger ASan.
48 std::vector<int> v;
49 v.reserve(1);
50 int i[] = {42};
51 v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1));
52 assert(v[0] == 42);
53 assert(is_contiguous_container_asan_correct(v));
54 }
55
56 __sanitizer_set_death_callback(do_exit);
57 {
58 typedef int T;
59 typedef std::vector<T> C;
60 const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
61 C c(std::begin(t), std::end(t));
62 c.reserve(2*c.size());
63 assert(is_contiguous_container_asan_correct(c));
64 assert(!__sanitizer_verify_contiguous_container ( c.data(), c.data() + 1, c.data() + c.capacity()));
65 T foo = c[c.size()]; // should trigger ASAN
66 assert(false); // if we got here, ASAN didn't trigger
67 }
68 }
69 #else
main()70 int main () { return 0; }
71 #endif
72