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 // UNSUPPORTED: c++03, c++11, c++14, c++17 10 // UNSUPPORTED: libcpp-no-concepts 11 // UNSUPPORTED: gcc-10 12 // UNSUPPORTED: libcpp-has-no-incomplete-ranges 13 14 // If we have a copy-propagating cache, when we copy ZeroOnDestroy, we will get a 15 // dangling reference to the copied-from object. This test ensures that we do not 16 // propagate the cache on copy. 17 18 #include <ranges> 19 20 #include <cstddef> 21 #include <cstring> 22 23 #include "test_macros.h" 24 #include "types.h" 25 26 struct ZeroOnDestroy : std::ranges::view_base { 27 unsigned count = 0; 28 int buff[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 29 30 constexpr ForwardIter begin() { return ForwardIter(buff); } 31 constexpr ForwardIter begin() const { return ForwardIter(); } 32 constexpr ForwardIter end() { return ForwardIter(buff + 8); } 33 constexpr ForwardIter end() const { return ForwardIter(); } 34 35 ~ZeroOnDestroy() { 36 memset(buff, 0, sizeof(buff)); 37 } 38 39 static auto dropFirstFour() { 40 ZeroOnDestroy zod; 41 std::ranges::drop_view dv(zod, 4); 42 // Make sure we call begin here so the next call to begin will 43 // use the cached iterator. 44 assert(*dv.begin() == 5); 45 // Intentionally invoke the copy ctor here. 46 return std::ranges::drop_view(dv); 47 } 48 }; 49 50 int main(int, char**) { 51 auto noDanlingCache = ZeroOnDestroy::dropFirstFour(); 52 // If we use the cached version, it will reference the copied-from view. 53 // Worst case this is a segfault, best case it's an assertion fired. 54 assert(*noDanlingCache.begin() == 5); 55 56 return 0; 57 } 58