Skip to content

[[gsl::Pointer]] does not diagnose dangling-after-copy #99685

Open
@pkasting

Description

@pkasting

[[gsl::Pointer]] can be used to diagnose a limited set of dangling pointer issues. However, it does not seem to preserve lifetime info through copies; see https://godbolt.org/z/vfjojda7K. Since the docs say a class so annotated "behaves like a pointer into the owner", it seems like copied "pointer"s should also dangle when the original owner is destroyed.

#include <optional>

struct [[gsl::Owner]] T {};

struct [[gsl::Pointer]] S {
  S(const T&);
};

S f1() {
  T t;
  return S(t);  //-Wreturn-stack-address, yay
}

S f2() {
  T t;
  S s(t);
  return s;  // No -Wreturn-stack-address, boo
}

std::optional<S> f3() {
  T t;
  return S(t);  // No -Wreturn-stack-address, boo
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"clang:memory-safetyIssue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions