[clang] [analyzer] Fix a false memory leak reports involving placement new (PR #144341)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 16 06:13:52 PDT 2025


================
@@ -63,6 +64,42 @@ void testGlobalNoThrowPlacementExprNewBeforeOverload() {
   int *p = new(std::nothrow) int;
 } // leak-warning{{Potential leak of memory pointed to by 'p'}}
 
+//----- Standard pointer placement operators
+void testGlobalPointerPlacementNew() {
+  int i;
+  void *p1 = operator new(0, &i); // no warn
+
+  void *p2 = operator new[](0, &i); // no warn
+
+  int *p3 = new(&i) int; // no warn
+
+  int *p4 = new(&i) int[0]; // no warn
----------------
balazs-benics-sonarsource wrote:

```suggestion
  void *p1 = operator new(0, &i); // no leak: placement new never allocates
  void *p2 = operator new[](0, &i); // no leak
  int *p3 = new(&i) int; // no leak
  int *p4 = new(&i) int[0]; // no leak
```

https://github.com/llvm/llvm-project/pull/144341


More information about the cfe-commits mailing list