[clang] [clang] Implement -Walloc-size diagnostic option (PR #150028)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 18 06:17:09 PDT 2025


================
@@ -7818,6 +7819,35 @@ ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy,
   return prepareVectorSplat(DestTy, CastExpr);
 }
 
+/// Check that a call to alloc_size function specifies sufficient space for the
+/// destination type.
+static void CheckSufficientAllocSize(Sema &S, QualType DestType,
+                                     const Expr *E) {
+  QualType SourceType = E->getType();
+  if (!DestType->isPointerType() || !SourceType->isPointerType() ||
+      DestType == SourceType)
+    return;
+
+  const auto *CE = dyn_cast<CallExpr>(E->IgnoreParenCasts());
----------------
AaronBallman wrote:

Then there's one more test I'd like to see added:
```
void *ptr = (void (*)(int))malloc(0); // Should diagnose
void *ptr = (void (*)(int))malloc(1); // Should not diagnose
```

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


More information about the cfe-commits mailing list