[clang] [clang] Implement -Walloc-size diagnostic option (PR #150028)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 30 09:58:22 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'
----------------
AaronBallman wrote:
Maybe it would be helpful to also add:
```
int *ptr = my_malloc(sizeof(int)); // no warning
```
to show a positive use of the attribute?
https://github.com/llvm/llvm-project/pull/150028
More information about the cfe-commits
mailing list