1560170faSzoecarver //===----------------------------------------------------------------------===//
2560170faSzoecarver //
3560170faSzoecarver // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4560170faSzoecarver // See https://llvm.org/LICENSE.txt for license information.
5560170faSzoecarver // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6560170faSzoecarver //
7560170faSzoecarver //===----------------------------------------------------------------------===//
8560170faSzoecarver
9560170faSzoecarver // UNSUPPORTED: c++03, c++11, c++14, c++17
1071909de3SMark de Wever // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11560170faSzoecarver
12560170faSzoecarver // If we have a copy-propagating cache, when we copy ZeroOnDestroy, we will get a
13560170faSzoecarver // dangling reference to the copied-from object. This test ensures that we do not
14560170faSzoecarver // propagate the cache on copy.
15560170faSzoecarver
16560170faSzoecarver #include <ranges>
17560170faSzoecarver
186cb05ca3SChristopher Di Bella #include <cstddef>
196cb05ca3SChristopher Di Bella #include <cstring>
206cb05ca3SChristopher Di Bella
21560170faSzoecarver #include "test_macros.h"
22560170faSzoecarver #include "types.h"
23560170faSzoecarver
24560170faSzoecarver struct ZeroOnDestroy : std::ranges::view_base {
25560170faSzoecarver unsigned count = 0;
26560170faSzoecarver int buff[8] = {1, 2, 3, 4, 5, 6, 7, 8};
27560170faSzoecarver
beginZeroOnDestroy28560170faSzoecarver constexpr ForwardIter begin() { return ForwardIter(buff); }
beginZeroOnDestroy29560170faSzoecarver constexpr ForwardIter begin() const { return ForwardIter(); }
endZeroOnDestroy30560170faSzoecarver constexpr ForwardIter end() { return ForwardIter(buff + 8); }
endZeroOnDestroy31560170faSzoecarver constexpr ForwardIter end() const { return ForwardIter(); }
32560170faSzoecarver
~ZeroOnDestroyZeroOnDestroy33560170faSzoecarver ~ZeroOnDestroy() {
34*770602cfSLouis Dionne std::memset(buff, 0, sizeof(buff));
35560170faSzoecarver }
36560170faSzoecarver
dropFirstFourZeroOnDestroy37560170faSzoecarver static auto dropFirstFour() {
38560170faSzoecarver ZeroOnDestroy zod;
39560170faSzoecarver std::ranges::drop_view dv(zod, 4);
40560170faSzoecarver // Make sure we call begin here so the next call to begin will
41560170faSzoecarver // use the cached iterator.
42560170faSzoecarver assert(*dv.begin() == 5);
43560170faSzoecarver // Intentionally invoke the copy ctor here.
44560170faSzoecarver return std::ranges::drop_view(dv);
45560170faSzoecarver }
46560170faSzoecarver };
47560170faSzoecarver
main(int,char **)48560170faSzoecarver int main(int, char**) {
49560170faSzoecarver auto noDanlingCache = ZeroOnDestroy::dropFirstFour();
50560170faSzoecarver // If we use the cached version, it will reference the copied-from view.
51560170faSzoecarver // Worst case this is a segfault, best case it's an assertion fired.
52560170faSzoecarver assert(*noDanlingCache.begin() == 5);
53560170faSzoecarver
54560170faSzoecarver return 0;
55560170faSzoecarver }
56