]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Add Walloc-size to warn about insufficient size in allocations [PR71219]
authorMartin Uecker <uecker@tugraz.at>
Thu, 27 Jul 2023 11:36:05 +0000 (13:36 +0200)
committerMartin Uecker <uecker@tugraz.at>
Wed, 1 Nov 2023 21:35:14 +0000 (22:35 +0100)
Add option Walloc-size that warns about allocations that have
insufficient storage for the target type of the pointer the
storage is assigned to. Added to Wextra.

PR c/71219
gcc:
* doc/invoke.texi: Document -Walloc-size option.

gcc/c-family:

* c.opt (Walloc-size): New option.

gcc/c:
* c-typeck.cc (convert_for_assignment): Add warning.

gcc/testsuite:

* gcc.dg/Walloc-size-1.c: New test.
* gcc.dg/Walloc-size-2.c: New test.

gcc/c-family/c.opt
gcc/c/c-typeck.cc
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/Walloc-size-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Walloc-size-2.c [new file with mode: 0644]

index 44b9c862c142cddfb3ba43f3643b0e8035d67416..29d3d789a49f5c6958db622d1e88e1294352b52f 100644 (file)
@@ -331,6 +331,10 @@ Walloca
 C ObjC C++ ObjC++ Var(warn_alloca) Warning
 Warn on any use of alloca.
 
+Walloc-size
+C ObjC Var(warn_alloc_size) Warning LangEnabledBy(C ObjC, Wextra)
+Warn when allocating insufficient storage for the target type of the assigned pointer.
+
 Walloc-size-larger-than=
 C ObjC C++ LTO ObjC++ Var(warn_alloc_size_limit) Joined Host_Wide_Int ByteSize Warning Init(HOST_WIDE_INT_MAX)
 -Walloc-size-larger-than=<bytes>       Warn for calls to allocation functions that
index 0de4662bfc6ce4614a2b892133f9a37b2d14d4d1..16fadfb546876d8ddc7075e5c7a894d310a49e31 100644 (file)
@@ -7347,6 +7347,34 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type,
                    "request for implicit conversion "
                    "from %qT to %qT not permitted in C++", rhstype, type);
 
+      /* Warn of new allocations that are not big enough for the target
+        type.  */
+      tree fndecl;
+      if (warn_alloc_size
+         && TREE_CODE (rhs) == CALL_EXPR
+         && (fndecl = get_callee_fndecl (rhs)) != NULL_TREE
+         && DECL_IS_MALLOC (fndecl))
+       {
+         tree fntype = TREE_TYPE (fndecl);
+         tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
+         tree alloc_size = lookup_attribute ("alloc_size", fntypeattrs);
+         if (alloc_size)
+           {
+             tree args = TREE_VALUE (alloc_size);
+             int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
+             /* For calloc only use the second argument.  */
+             if (TREE_CHAIN (args))
+               idx = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
+             tree arg = CALL_EXPR_ARG (rhs, idx);
+             if (TREE_CODE (arg) == INTEGER_CST
+                 && INTEGER_CST == TREE_CODE (TYPE_SIZE_UNIT (ttl))
+                 && tree_int_cst_lt (arg, TYPE_SIZE_UNIT (ttl)))
+                warning_at (location, OPT_Walloc_size, "allocation of "
+                            "insufficient size %qE for type %qT with "
+                            "size %qE", arg, ttl, TYPE_SIZE_UNIT (ttl));
+           }
+       }
+
       /* See if the pointers point to incompatible address spaces.  */
       asl = TYPE_ADDR_SPACE (ttl);
       asr = TYPE_ADDR_SPACE (ttr);
index 53ae784647bc953f9b8ead2841b6e24e8090a0e5..6e776a0faa190156c9c7103f8e8a23102c7b3e44 100644 (file)
@@ -8138,6 +8138,16 @@ always leads to a call to another @code{cold} function such as wrappers of
 C++ @code{throw} or fatal error reporting functions leading to @code{abort}.
 @end table
 
+@opindex Wno-alloc-size
+@opindex Walloc-size
+@item -Walloc-size
+Warn about calls to allocation functions decorated with attribute
+@code{alloc_size} that specify insufficient size for the target type of
+the pointer the result is assigned to, including those to the built-in
+forms of the functions @code{aligned_alloc}, @code{alloca},
+@code{calloc},
+@code{malloc}, and @code{realloc}.
+
 @opindex Wno-alloc-zero
 @opindex Walloc-zero
 @item -Walloc-zero
diff --git a/gcc/testsuite/gcc.dg/Walloc-size-1.c b/gcc/testsuite/gcc.dg/Walloc-size-1.c
new file mode 100644 (file)
index 0000000..cae5e07
--- /dev/null
@@ -0,0 +1,34 @@
+/* Tests the warnings for insufficient allocation size.
+   { dg-do compile }
+   { dg-options "-Walloc-size" }
+ * */
+
+struct b { int x[10]; };
+
+void fo0(void)
+{
+        struct b *p = __builtin_malloc(sizeof *p);
+}
+
+void fo1(void)
+{
+        struct b *p = __builtin_malloc(sizeof p);      /* { dg-warning "allocation of insufficient size" } */
+}
+
+void fo2(void)
+{
+        struct b *p = __builtin_alloca(sizeof p);      /* { dg-warning "allocation of insufficient size" } */
+}
+
+void fo3(void)
+{
+        struct b *p = __builtin_calloc(1, sizeof p);   /* { dg-warning "allocation of insufficient size" } */
+}
+
+void g(struct b* p);
+
+void fo4(void)
+{
+        g(__builtin_malloc(4));                                /* { dg-warning "allocation of insufficient size" } */
+}
+
diff --git a/gcc/testsuite/gcc.dg/Walloc-size-2.c b/gcc/testsuite/gcc.dg/Walloc-size-2.c
new file mode 100644 (file)
index 0000000..de641a6
--- /dev/null
@@ -0,0 +1,18 @@
+/* Test that VLA types do not crash with -Walloc-size
+   { dg-do compile }
+   { dg-options "-Walloc-size" }
+ * */
+
+struct foo { int x[10]; };
+
+void fo0(int n)
+{
+        struct foo (*p)[n] = __builtin_malloc(sizeof *p);
+}
+
+void fo1(int n)
+{
+       struct bar { int x[n]; };
+       struct bar *p = __builtin_malloc(sizeof *p);
+}
+