58 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// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// If we have a copy-propagating cache, when we copy ZeroOnDestroy, we will get a12// dangling reference to the copied-from object. This test ensures that we do not13// propagate the cache on copy.14 15#include <ranges>16 17#include <cstddef>18#include <cstring>19 20#include "test_macros.h"21#include "types.h"22 23struct ZeroOnDestroy : std::ranges::view_base {24 unsigned count = 0;25 int buff[8] = {1, 2, 3, 4, 5, 6, 7, 8};26 27 constexpr ForwardIter begin() { return ForwardIter(buff); }28 constexpr ForwardIter begin() const { return ForwardIter(); }29 constexpr ForwardIter end() { return ForwardIter(buff + 8); }30 constexpr ForwardIter end() const { return ForwardIter(); }31 32 ZeroOnDestroy() = default;33 ZeroOnDestroy(const ZeroOnDestroy&) = default;34 ZeroOnDestroy& operator=(const ZeroOnDestroy&) = default;35 ~ZeroOnDestroy() {36 std::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 will43 // 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 50int main(int, char**) {51 auto noDanglingCache = 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(*noDanglingCache.begin() == 5);55 56 return 0;57}58