[clang] [analyzer] Add alpha.cplusplus.PlacementSelfCopy checker (PR #233525)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 00:54:39 PST 2026
================
@@ -3503,6 +3503,83 @@ void bad_insert3(std::vector &v1, std::vector &v2) {
}
```
+(alpha-cplusplus-placementselfcopy)=
+
+#### alpha.cplusplus.PlacementSelfCopy (C++)
+
+Check for placement new expressions that access the storage they are placed
+into through the old object while the new object is being constructed, e.g.
+`::new (p) T(*p)`. A value read during the construction through a glvalue not
+obtained from `this` is unspecified ([class.cdtor]/2); writes through such a
+glvalue are reported as well. Calls of
+`std::construct_at` are checked as well; a report that would end inside the
+standard library is emitted at the call site.
+
+The checker follows the reference or pointer that received the old storage
+through variables, pointer arithmetic, the functions it is passed to and the
+methods called on it, and reports the first of these operations on it:
+
+- a read or a write of the storage;
+- passing it to a function or calling its method that is not inlined, except
+ for calls that only return their argument, such as `std::move`,
+ `std::forward`, `std::as_const` and `std::addressof`;
+- capturing it in a lambda, unless a pointer to it is captured.
+
+Taking, storing, comparing and shifting its address, accessing members without
+reading them and calling inlined methods that read nothing are allowed, and so
+are all accesses through `this` or its copies. An object that only contains the
+storage, such as a pool with an inline buffer, is not the old object; only its
+accesses that overlap the storage are reported.
+
+```cpp
+struct Buffer {
+ int *Data;
+ int Size;
+ Buffer(const Buffer &O)
+ : Data(new int[O.Size]), // warn: placement new accesses the storage it
+ // is placed into during construction of
+ // the new object
+ Size(O.Size) {
+ for (int I = 0; I < Size; ++I)
+ Data[I] = O.Data[I];
+ }
+};
+
+void reconstruct(Buffer *B) {
+ ::new (B) Buffer(*B);
+}
+```
+
+An access of the old object in the new-initializer itself is reported as well,
+whatever the order of the elements.
----------------
NagyDonat wrote:
> I believe the example still has undefined behavior. [[basic.life]/2](https://eel.is/c++draft/basic.life#2) says that "storage is considered reused after it is returned from the allocation function, but before the evaluation of the new-initializer", so P->Y and P->X read the old object after its lifetime has ended.
Good point, this is technically undefined behavior (because the standard says so).
However [[new.delete.placement]](https://eel.is/c++draft/new.delete.placement) defines that the placement new operator "Intentionally performs no other action.", and the evaluation of the new-initializer must be sequenced before the call of the constructor call (because it produces the arguments of the constructor), so the standard also states that in this case the object whose lifetime has technically ended is still "untouched" and accessing its members can't produce anything but the correct values.
There are many precedents where the analyzer intentionally accepts standard-non-compliant code that cannot cause actual buggy behavior on standard widespread compilers. (In my opinion the analyzer is a practical tool for catching bugs, not a theoretical tool for checking standard compliance.)
Based on this, **I do not support including this feature in this checker**, because it adds lots of complexity and doesn't provide actual practical benefits for developers who use the analyzer. As this PR is very large and this feature is unrelated to the rest of this checker (cites a different part of the standard), you should remove this feature from this PR and limit the scope of this review to the other, more practical parts.
Later, if you want, you can try to reintroduce this "check reads in the new-initializer" feature in a separate PR. I'm not willing to spend time on reviewing it, but if it is accepted by another reviewer, I won't block merging it.
> The standard's own example is almost the same code:
>
> ```
> S x{1};
> new(&x) S(x.m); // undefined behavior
> ```
Actually, this code example in the draft standard **does not compile** [(godbolt link)](https://godbolt.org/z/7GE84Er4x) because the type `struct S { int m; }` doesn't have a constructor that takes an `int`!
https://github.com/llvm/llvm-project/pull/233525
More information about the cfe-commits
mailing list