[clang] Fix unique_ptr aggregate initialization false positives (PR #155131)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 27 05:52:42 PDT 2025


================
@@ -3074,6 +3077,43 @@ void MallocChecker::checkPostCall(const CallEvent &Call,
     (*PostFN)(this, C.getState(), Call, C);
     return;
   }
+
+  ProgramStateRef State = C.getState();
+
+  if (const auto *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
+    // Ensure we are constructing a concrete object/subobject.
+    if (const MemRegion *ObjUnderConstr = Ctor->getCXXThisVal().getAsRegion()) {
+      ProgramStateRef NewState = State;
+
+      for (unsigned I = 0, E = Call.getNumArgs(); I != E; ++I) {
+        SVal ArgV = Call.getArgSVal(I);
+
+        SymbolRef Sym = ArgV.getAsSymbol();
+        if (!Sym)
+          continue;
+
+        // Look up current ref-state for this symbol in the RegionState map.
+        if (const RefState *RS = State->get<RegionState>(Sym)) {
+          // Only re-label symbols that are still owned allocations from C++
+          // new/new[].
+          if (RS->isAllocated() &&
+              (RS->getAllocationFamily().Kind == AF_CXXNew ||
+               RS->getAllocationFamily().Kind == AF_CXXNewArray)) {
----------------
NagyDonat wrote:

I know that it's unusual to use `malloc` in C++, but it works if somebody decides to use it, so personally I wouldn't limit this logic to `new`/`new[]`. (But this is just a vague feeling, not a strong opinion.)

What do you think?

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


More information about the cfe-commits mailing list