brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 72875a5 Raw
54 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: asan10 11// <vector>12 13// reference operator[](size_type n);14 15#include <vector>16#include <cassert>17#include <cstdlib>18 19#include "asan_testing.h"20#include "min_allocator.h"21#include "test_iterators.h"22#include "test_macros.h"23 24extern "C" void __sanitizer_set_death_callback(void (*callback)(void));25 26void do_exit() { exit(0); }27 28int main(int, char**) {29  {30    typedef cpp17_input_iterator<int*> MyInputIter;31    // Sould not trigger ASan.32    std::vector<int> v;33    v.reserve(1);34    int i[] = {42};35    v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1));36    assert(v[0] == 42);37    assert(is_contiguous_container_asan_correct(v));38  }39 40  __sanitizer_set_death_callback(do_exit);41  {42    typedef int T;43    typedef std::vector<T> C;44    const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};45    C c(std::begin(t), std::end(t));46    c.reserve(2 * c.size());47    assert(is_contiguous_container_asan_correct(c));48    assert(!__sanitizer_verify_contiguous_container(c.data(), c.data() + 1, c.data() + c.capacity()));49    volatile T foo = c[c.size()]; // should trigger ASAN. Use volatile to prevent being optimized away.50    assert(false);                // if we got here, ASAN didn't trigger51    ((void)foo);52  }53}54