[clang] [analyzer] Fix FP for cplusplus.placement new #149240 (PR #150161)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 25 01:49:44 PDT 2025


================
@@ -0,0 +1,24 @@
+// RUN: %clang -fsyntax-only -Xclang -analyze -Xclang -analyzer-checker=cplusplus.PlacementNew -Xclang -verify -std=c++17 %s
+
+#include <new>
+
+void test_exact_size() {
+    void *buf = ::operator new(sizeof(int)*2);
+    int *placement_int = new (buf) int[2]; // no-warning
+    placement_int[0] = 42;
+    ::operator delete(buf);
+}
+
+void test_undersize() {
+    void *buf = ::operator new(sizeof(int)*1);
+    int *placement_int = new (buf) int[2]; // expected-warning {{Storage provided to placement new is only 4 bytes, whereas the allocated type requires 8 bytes [cplusplus.PlacementNew]}}
+    placement_int[0] = 42;
+    ::operator delete(buf);
+}
+
+void test_oversize() {
+    void *buf = ::operator new(sizeof(int)*4);
+    int *placement_int = new (buf) int[2]; // no-warning
+    placement_int[0] = 42;
+    ::operator delete(buf);
+}
----------------
NagyDonat wrote:

What is the reason for adding these tests (in addition to the tests that already exist in `placement-new.cpp`)? As far as I see there is no significant difference between these and the tests that already exist in `placement-new.cpp`.

Also note that the tests of the static analyzer are always placed in the `clang/test/Analysis` directory and not the `clang/test/SemaCXX` directory.

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


More information about the cfe-commits mailing list