[clang] [analyzer] Fix VAListChecker for by-value va_list (PR #245289)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 08:54:31 PST 2026


================
@@ -213,10 +289,35 @@ void VAListChecker::checkPreStmt(const VAArgExpr *VAA,
 void VAListChecker::checkDeadSymbols(SymbolReaper &SR,
                                      CheckerContext &C) const {
   ProgramStateRef State = C.getState();
+
+  // Cleanup runs before PreCall, and getVAListState may need these entries
+  // from any caller frame while the callee is analyzed.
+  llvm::SmallPtrSet<const MemRegion *, 2> CallArguments;
+  if (std::optional<PreStmtPurgeDeadSymbols> PP =
+          C.getLocation().getAs<PreStmtPurgeDeadSymbols>();
+      PP && CallEvent::isCallStmt(PP->getStmt())) {
+    CallEventRef<> Call =
+        State->getStateManager().getCallEventManager().getCall(
+            PP->getStmt(), State, C.getStackFrame(), C.getCFGElementRef());
+    if (Call) {
+      for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
+        const Expr *ArgExpr = Call->getArgExpr(I);
+        if (!ArgExpr->getType()->isRecordType())
+          continue;
+        if (const MemRegion *Arg =
+                getVAListAsRegion(Call->getArgSVal(I), ArgExpr, C))
+          CallArguments.insert(Arg);
+      }
+    }
+  }
+
   VAListStateMapTy Tracked = State->get<VAListStateMap>();
   RegionVector Leaked;
   for (const auto &[Reg, S] : Tracked) {
-    if (SR.isLiveRegion(Reg))
+    const auto *VR = dyn_cast<VarRegion>(Reg);
+    const bool NeededByCallee =
+        VR && VR->getStackFrame()->isParentOf(C.getStackFrame());
+    if (SR.isLiveRegion(Reg) || CallArguments.contains(Reg) || NeededByCallee)
----------------
NagyDonat wrote:

I understand that you want to keep these entries alive because `getVAListState` may need them -- but keeping them alive has the side effect that you don't emit leak warnings for them. Does the checker still emit all the leak warnings that are needed? 

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


More information about the cfe-commits mailing list