[clang] [WIP] [analyzer] Refactor MallocChecker to use `BindExpr` in `evalCall` (PR #106081)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 27 09:02:34 PDT 2024


================
@@ -1736,6 +1816,25 @@ MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallEvent &Call,
   return MallocMemAux(C, Call, UnknownVal(), UndefinedVal(), State, Family);
 }
 
+ProgramStateRef MallocChecker::MallocBindRetval(CheckerContext &C,
+                                                const CallEvent &Call,
+                                                ProgramStateRef State,
+                                                bool isAlloca) const {
+  const Expr *CE = Call.getOriginExpr();
+
+  // We expect the allocation functions to return a pointer.
+  if (!Loc::isLocType(CE->getType()))
+    return nullptr;
+
+  unsigned Count = C.blockCount();
+  SValBuilder &SVB = C.getSValBuilder();
+  const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
+  DefinedSVal RetVal = (isAlloca ? SVB.getAllocaRegionVal(CE, LCtx, Count)
+                                 : SVB.getConjuredHeapSymbolVal(CE, LCtx, Count)
+                                       .castAs<DefinedSVal>());
----------------
NagyDonat wrote:

Why do we need to do a `castAs()` here?

I know that this is was already present in the old code, but I'm still surprised. If `getConjuredHeapSymbolVal` always returns a defined value, then its return type should be adjusted -- otherwise we should check for an undefined result.

(No action required if you don't see an easy solution -- this is not your responsibility.)

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


More information about the cfe-commits mailing list