[PING] [C PATCH, v2] Add Walloc-size to warn about insufficient size in allocations [PR71219]
Am Montag, dem 18.09.2023 um 23:26 +0200 schrieb Martin Uecker: > > Compared to the previous version I changed the name of the > warning to "Walloc-size" which matches "Wanalyzer-allocation-size" > but is still in line with the other -Walloc-something warnings > we have. I also added it to Wextra. > > I found PR71219 that requests the warning and points out that > it is recommended by the C secure coding guidelines and added > the PR to the commit log (although the version with cast is not > diagnosed so far.) > > I did not have time to implement the extensions suggested > on the list, i.e. warn when the size is not a multiple > of the size of the type and warn for if the size is not > suitable for a flexible array member. (this is also a bit > more complicated than it seems) > > Bootstrapped and regression tested on x86_64. > > > Martin > > > Add option Walloc-size that warns about allocations that have > insufficient storage for the target type of the pointer the > storage is assigned to. > > 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/c-family/c.opt | 4 > gcc/c/c-typeck.cc| 27 + > gcc/doc/invoke.texi | 10 > gcc/testsuite/gcc.dg/Walloc-size-1.c | 36 > 4 files changed, 77 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-1.c > > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > index 7348ad42ee0..9ba08a1fb6d 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-size > +C ObjC Var(warn_alloc_size) 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= Warn for calls to allocation functions > that > diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc > index e2bfd2caf85..c759c6245ed 100644 > --- a/gcc/c/c-typeck.cc > +++ b/gcc/c/c-typeck.cc > @@ -7384,6 +7384,33 @@ 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 > + && 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); > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi > index 33befee7d6b..a4fbcf5e1b5 100644 > --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -8086,6 +8086,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 10
Re: [PING] [C PATCH, v2] Add Walloc-size to warn about insufficient size in allocations [PR71219]
On Tue, 31 Oct 2023, Martin Uecker wrote: > > + if (TREE_CODE (arg) == INTEGER_CST > > + && tree_int_cst_lt (arg, TYPE_SIZE_UNIT (ttl))) What if TYPE_SIZE_UNIT (ttl) is not an INTEGER_CST? I don't see any tests of the case of assigning to a pointer to a variably sized type. -- Joseph S. Myers jos...@codesourcery.com
Re: [PING] [C PATCH, v2] Add Walloc-size to warn about insufficient size in allocations [PR71219]
Am Dienstag, dem 31.10.2023 um 22:19 + schrieb Joseph Myers: > On Tue, 31 Oct 2023, Martin Uecker wrote: > > > > + if (TREE_CODE (arg) == INTEGER_CST > > > + && tree_int_cst_lt (arg, TYPE_SIZE_UNIT (ttl))) > > What if TYPE_SIZE_UNIT (ttl) is not an INTEGER_CST? I don't see any tests > of the case of assigning to a pointer to a variably sized type. > Right. Thanks! Revised version attached. Martin c: Add Walloc-size to warn about insufficient size in allocations [PR71219] 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. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 44b9c862c14..29d3d789a49 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -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= Warn for calls to allocation functions that diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 0de4662bfc6..16fadfb5468 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -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); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 5a9284d635c..815a33d4b87 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -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 index 000..61806f58192 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-1.c @@ -0,0 +1,36 @@ +/* Tests the warnings for insufficient allocation size. + { dg-do compile } + { dg-options "-Walloc-size" } + * */ +#include +#include + +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
Re: [PING] [C PATCH, v2] Add Walloc-size to warn about insufficient size in allocations [PR71219]
On Wed, 1 Nov 2023, Martin Uecker wrote: > Am Dienstag, dem 31.10.2023 um 22:19 + schrieb Joseph Myers: > > On Tue, 31 Oct 2023, Martin Uecker wrote: > > > > > > + if (TREE_CODE (arg) == INTEGER_CST > > > > + && tree_int_cst_lt (arg, TYPE_SIZE_UNIT (ttl))) > > > > What if TYPE_SIZE_UNIT (ttl) is not an INTEGER_CST? I don't see any tests > > of the case of assigning to a pointer to a variably sized type. > > > > Right. Thanks! Revised version attached. This version is OK. -- Joseph S. Myers jos...@codesourcery.com