Skip to content

Dangling false positive if the the owner is also moved in the initializer. #114201

Open
@hokein

Description

@hokein

This issue is identified in #112751.

// case1
namespace std {
template<typename T>
struct unique_ptr {
  T &operator*();
  T *get() const [[clang::lifetimebound]];
};
} // namespace std

struct X {
  X(std::unique_ptr<int> up) :
    pointer(up.get()), owner(std::move(up)) {}

  int *pointer;
  std::unique_ptr<int> owner;
};

When we add the clang::lifetimebound annotation to unique_ptr::get(), clang emits a dangling-field warning for the pointer(up.get()) member initializer. This warning is a false positive in this context, as the owner member is moved as part of the initialization, retaining ownership.

Another example occurs in designated-initializer cases:

// case2
struct X {
   int *pointer;
   std::unique_ptr<int> owner;
};

X func(std::unique_ptr<int> up) {
   return {
      .pointer = up.get(),
      .owner = std::move(up)
   };
}

Fixing these false positives is hard because it would require tracking dependencies between expressions, which is beyond the capabilities of the current statement-local analysis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang: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