<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/149240>149240</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            possible false-positive clang-analyzer-cplusplus.PlacementNew
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            false-positive
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          puetzk
      </td>
    </tr>
</table>

<pre>
    ```c++
#include <new>

int main() {
    void *buf = ::operator new(sizeof(int)*2);
 int * placement_int = new (buf) int[2];
    placement_int[0] = 42;
 ::operator delete(buf);
}
```

clang-tidy produces

```
<source>:5:27: warning: Storage provided to placement new is only 8 bytes, whereas the allocated array type requires more space for internal needs [clang-analyzer-cplusplus.PlacementNew]
    5 |     int * placement_int = new (buf) int[2];
      | ^~~~~~~~~~~~~~~~
<source>:4:5: note: 'buf' initialized here
    4 | void *buf = ::operator new(sizeof(int)*2);
      | ^~~~~~~~~
<source>:5:27: note: Storage provided to placement new is only 8 bytes, whereas the allocated array type requires more space for internal needs
    5 |     int * placement_int = new (buf) int[2];
      | ^~~~~~~~~~~~~~~~
```

Reproduced in https://godbolt.org/z/MYGc5qfn6 using clang version 22.0.0git (https://github.com/llvm/llvm-project.git 82d7405b3bb911c86d0b07c8a6bec5044acbdf66)

I think this warning is incorrect, as described at https://stackoverflow.com/questions/8720425/array-placement-new-requires-unspecified-overhead-in-the-buffer and in https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2382 or https://cplusplus.github.io/CWG/issues/2382.html. This is a non-allocating placement new, in which case (per that defect resolution) no additional space may be requested.

So I think these "array overhead" warnings are *always* false-positives. However, they do seem to have been added in May 2020:
https://github.com/llvm/llvm-project/blame/1c541aa9f9a2453324724bfb9d661bc672778d10/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp#L120-L134 shows it came in with https://github.com/llvm/llvm-project/commit/7c3768495e8c1599dc30986f7bd47d5e91f303f2 and https://reviews.llvm.org/D76996
That dates this first being added to clang **after** that DR had been reported, voted on, and the resolution accepted into CD5. So I don't think they are simply outdated; maybe the author/reviewers were unaware of the defect report?

Or maybe I'm misunderstanding something and this really is buggy code - "I'm wrong" is certainly possible too :-)


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVk9v-8YR_TSry0ACOSRF8aCDbEVpgKQt8gtQ9BTsn6G4MbXL3-5ShHzoZy-GlOzaDdoGARpDFpfUzuPMe2-GlDHasyPai-pJVMeVHFPnw34YKb2-rJQ3t73YZstHC3ziT3YQWFin-9EQiOLZ0SSKb_hydrAuwUVaJ3AnsAFR83YAgKu3BgQe1NiCKI4gioMoDn6gIJMPwBi4i_aVfCtwZ10S2Ag8IB-KGYShBR5g6KWmC7n083ylOHIwCNypseV7cmz1hKI63gMBPsaI6ikT1XEOLfG-6VM-hnpK9Aa6bBL1kb8ffCwV61668zpZc4MheDNqissPH_YVz9GPQRMTVRwqURywFsUBJhmcdWdefkk-yDMxytUaMpD8e95zjTaCd_0NdqBuiaLAZ5g6CiQjpI5A9r3XMpEBGYK8QboNBIG-jjZQhIsPBHGQmqD1gVmi4GQPjshEENXTUoh0sr-9UljroR8j_2_--kjizzQxqwulFYj6mRe_SxiYUUT1zT8-_v07Z-WdN3A-ER8F1jNwDdbZZGVvX8kA83HHLmfs32-8X0vzP2n6SPCPFPT_JtLHZviR7j1gwDroUhoi840ngaezN8r3aePDWeDpVeDph79_q6uvrdvCGK07w-xAuFKI1jtA3GSb7Gw5790nKJu6UW20vwg89f31cVgPwf9COm04aoemLrNKFUo1ea53W5OprNY7uVWkq6wspVam3W5Z6Dn57yB11r3wd3w0JktknfYhkE6sj4xgKOpgFcuSPtUYk9Qv_kqh7f10z-_rSDFZ76LA067GrMRK4GlWdP2mxtrRtH5Iux5dHEjb1pJZM1pH0qytW6eO1mpsWwog3a9QPE3Txg_k1jGZO8-_JJ1zYhqRN5y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