From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1924) id D25173858C39; Sat, 29 Jul 2023 19:46:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D25173858C39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690659971; bh=/jbY6aKiulfwAkIipyd+6H38f0Wu2/OIy7ee2lqTp5I=; h=From:To:Subject:Date:From; b=ORt1vBxA/w3d47/wfdgX8gRAshmtvILIsR90/GXfBoXMyIB7C/JAFCgEk9rgzUZVa 2s6JDJsRqe2bplOXrrBOWh106FXQFYWzz/Ae93xMy/A4Bwor+lUjzzZemOnrxXahH/ fuVzAPZa4tyFXBvYnPcq6l6mWFB9OgjSnl8OMqTI= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Martin Uecker To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/uecker/heads/vla)] c: Add Walloc-type to warn about insufficient size in allocations X-Act-Checkin: gcc X-Git-Author: Martin Uecker X-Git-Refname: refs/users/uecker/heads/vla X-Git-Oldrev: cc0b9bf372cd08bb23f662c03ea18f21ecfcfb6f X-Git-Newrev: 8e7176755cbd54d034050ff961ce72f885a83043 Message-Id: <20230729194611.D25173858C39@sourceware.org> Date: Sat, 29 Jul 2023 19:46:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:8e7176755cbd54d034050ff961ce72f885a83043 commit 8e7176755cbd54d034050ff961ce72f885a83043 Author: Martin Uecker 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= 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 +#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 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" } */ +} + +