public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/uecker/heads/vla)] c: Add Walloc-type to warn about insufficient size in allocations
@ 2023-07-29 19:46 Martin Uecker
0 siblings, 0 replies; only message in thread
From: Martin Uecker @ 2023-07-29 19:46 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:8e7176755cbd54d034050ff961ce72f885a83043
commit 8e7176755cbd54d034050ff961ce72f885a83043
Author: Martin Uecker <uecker@tugraz.at>
Date: Thu Jul 27 13:36:05 2023 +0200
c: Add Walloc-type to warn about insufficient size in allocations
Add option Walloc-type that warns about allocations that have
insufficient storage for the target type of the pointer the
storage is assigned to.
gcc:
* doc/invoke.texi: Document -Walloc-type option.
gcc/c-family:
* c.opt (Walloc-type): New option.
gcc/c:
* c-typeck.cc (convert_for_assignment): Add Walloc-type warning.
gcc/testsuite:
* gcc.dg/Walloc-type-1.c: New test.
Diff:
---
gcc/c-family/c.opt | 4 ++++
gcc/c/c-typeck.cc | 26 +++++++++++++++++++++++++
gcc/doc/invoke.texi | 10 ++++++++++
gcc/testsuite/gcc.dg/Walloc-type-1.c | 37 ++++++++++++++++++++++++++++++++++++
4 files changed, 77 insertions(+)
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 4abdc8d0e77..8b9d148582b 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -319,6 +319,10 @@ Walloca
C ObjC C++ ObjC++ Var(warn_alloca) Warning
Warn on any use of alloca.
+Walloc-type
+C ObjC Var(warn_alloc_type) Warning
+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
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 40e85450c33..5cd9c871d9f 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -7589,6 +7589,32 @@ convert_for_assignment_instrument (location_t location, location_t expr_loc, tre
"request for implicit conversion "
"from %qT to %qT not permitted in C++", rhstype, type);
+ /* Warn of new allocations are not big enough for the target type. */
+ tree fndecl;
+ if (warn_alloc_type
+ && 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
+ && tree_int_cst_lt (arg, TYPE_SIZE_UNIT (ttl)))
+ warning_at (location, OPT_Walloc_type, "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);
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 578b4c31e4c..33fdd193df6 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -8107,6 +8107,16 @@ Disable @option{-Walloc-size-larger-than=} warnings. The option is
equivalent to @option{-Walloc-size-larger-than=}@samp{SIZE_MAX} or
larger.
+@opindex Wno-alloc-type
+@opindex Walloc-type
+@item -Walloc-type
+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-alloca
@opindex Walloca
@item -Walloca
diff --git a/gcc/testsuite/gcc.dg/Walloc-type-1.c b/gcc/testsuite/gcc.dg/Walloc-type-1.c
new file mode 100644
index 00000000000..bc62e5e9aa3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Walloc-type-1.c
@@ -0,0 +1,37 @@
+/* Tests the warnings for insufficient allocation size.
+ { dg-do compile }
+ * { dg-options "-Walloc-type" }
+ * */
+#include <stdlib.h>
+#include <alloca.h>
+
+struct b { int x[10]; };
+
+void fo0(void)
+{
+ struct b *p = malloc(sizeof *p);
+}
+
+void fo1(void)
+{
+ struct b *p = malloc(sizeof p); /* { dg-warning "allocation of insufficient size" } */
+}
+
+void fo2(void)
+{
+ struct b *p = alloca(sizeof p); /* { dg-warning "allocation of insufficient size" } */
+}
+
+void fo3(void)
+{
+ struct b *p = calloc(1, sizeof p); /* { dg-warning "allocation of insufficient size" } */
+}
+
+void g(struct b* p);
+
+void fo4(void)
+{
+ g(malloc(4)); /* { dg-warning "allocation of insufficient size" } */
+}
+
+
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-07-29 19:46 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-29 19:46 [gcc(refs/users/uecker/heads/vla)] c: Add Walloc-type to warn about insufficient size in allocations Martin Uecker
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).