51 lines · cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus -verify %s2// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus -verify %s -DEMPTY_CLASS3// UNSUPPORTED: system-windows4// expected-no-diagnostics5 6// This test reproduces the issue that previously the static analyzer7// initialized an [[no_unique_address]] empty field to zero,8// over-writing a non-empty field with the same offset.9 10namespace std {11#ifdef EMPTY_CLASS12 13 struct default_delete {};14 template <class _Tp, class _Dp = default_delete >15#else16 // Class with methods and static members is still empty:17 template <typename T>18 class default_delete {19 T dump();20 static T x;21 };22 template <class _Tp, class _Dp = default_delete<_Tp> >23#endif24 class unique_ptr {25 [[no_unique_address]] _Tp * __ptr_;26 [[no_unique_address]] _Dp __deleter_;27 28 public:29 explicit unique_ptr(_Tp* __p) noexcept30 : __ptr_(__p),31 __deleter_() {}32 33 ~unique_ptr() {34 delete __ptr_;35 }36 };37}38 39struct X {};40 41int main()42{43 // Previously a leak falsely reported here. It was because the44 // Static Analyzer engine simulated the initialization of45 // `__deleter__` incorrectly. The engine assigned zero to46 // `__deleter__`--an empty record sharing offset with `__ptr__`.47 // The assignment over wrote `__ptr__`.48 std::unique_ptr<X> a(new X()); 49 return 0;50}51