| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus -verify %s |
| 2 |
// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus -verify %s -DEMPTY_CLASS |
| 3 |
// UNSUPPORTED: system-windows |
| 4 |
// expected-no-diagnostics |
| 5 |
|
| 6 |
// This test reproduces the issue that previously the static analyzer |
| 7 |
// initialized an [[no_unique_address]] empty field to zero, |
| 8 |
// over-writing a non-empty field with the same offset. |
| 9 |
|
| 10 |
namespace std { |
| 11 |
#ifdef EMPTY_CLASS |
| 12 |
|
| 13 |
struct default_delete {}; |
| 14 |
template <class _Tp, class _Dp = default_delete > |
| 15 |
#else |
| 16 |
// 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 |
#endif |
| 24 |
class unique_ptr { |
| 25 |
[[no_unique_address]] _Tp * __ptr_; |
| 26 |
[[no_unique_address]] _Dp __deleter_; |
| 27 |
|
| 28 |
public: |
| 29 |
explicit unique_ptr(_Tp* __p) noexcept |
| 30 |
: __ptr_(__p), |
| 31 |
__deleter_() {} |
| 32 |
|
| 33 |
~unique_ptr() { |
| 34 |
delete __ptr_; |
| 35 |
} |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
struct X {}; |
| 40 |
|
| 41 |
int main() |
| 42 |
{ |
| 43 |
// Previously a leak falsely reported here. It was because the |
| 44 |
// Static Analyzer engine simulated the initialization of |
| 45 |
// `__deleter__` incorrectly. The engine assigned zero to |
| 46 |
// `__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 |
} |