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

Vladimir Vuksanovic via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 6 02:39:45 PDT 2025


================
@@ -932,6 +932,20 @@ An example of how to use ``alloc_size``
     assert(__builtin_object_size(a, 0) == 100);
   }
 
+When ``-Walloc-size`` is enabled, this attribute allows the compiler to
+diagnose cases when the allocated memory is insufficient for the size of the
+type the returned pointer is cast to.
+
+.. code-block:: c
+
+  void *my_malloc(int a) __attribute__((alloc_size(1)));
+  void consumer_func(int *);
+
+  int main() {
+    int *w = my_malloc(1); // warning: allocation of insufficient size '1' for type 'int' with size '4'
----------------
vvuksanovic wrote:

Added

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


More information about the cfe-commits mailing list