<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/137252>137252</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[StaticAnalyzer] A Static Analyzer bug related to initializing [[__no_unique_address__]] fields
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:static analyzer
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ziqingluo-90
</td>
</tr>
</table>
<pre>
Hi Static Analyzer Developers,
I was investigating a downstream bug arose from C++ standard library changes.
There is a minimal reproducer:
```
namespace std {
struct default_delete {
};
template <class _Tp, class _Dp = default_delete >
class unique_ptr {
[[__no_unique_address__]] _Tp * __ptr_;
[[__no_unique_address__]] _Dp __deleter_;
public:
explicit unique_ptr(_Tp* __p) noexcept
: __ptr_(__p),
__deleter_() {}
~unique_ptr() {
delete __ptr_;
}
};
}
struct X {};
int main()
{
std::unique_ptr<X> a(new X()); // leak reported
return 0;
}
```
>From what I observed, the two fields `__ptr_` and `__deleter_` have overlapping `MemRegion`s (Same base and both have 0 offset) because of `[[no_unique_address]]`. Then, at the `unique_ptr` constructor call, CSA simulates the following two steps:
1. assigning `__p` to `__ptr_`; and then
2. assigning `0` to `__deleter_`.
Since the two fields have overlapping `MemRegion`s, the second assignment removes the binding of `__ptr_`. So later the destructor is not able to `delete` the allocated object properly. Resulting a false leak report.
If one removes `[[no_unique_address]]` from `__deleter_`'s declaration, the false report goes away.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVUGT2jgT_TXi0hWXkMGGAwcDob4cvksmh9yottXY2pUlR5Ihk8P-9i1ZhmVmDomLKgqs7n7v9VM3eq9aQ7Rj6z1bHxc4hs663S_1Q5lWj_bTli9qK193_1PwEjCoBiqD-vUXOTjSlbQdyHkmDoxXjFdf4IYelLmSD6rFoEwLCNLejA-OsId6bAGd9QQXZ3s4MLFnYg8-oJHoJGhVO3Sv0HRoWvI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