From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from cross.elm.relay.mailchannels.net (cross.elm.relay.mailchannels.net [23.83.212.46]) by sourceware.org (Postfix) with ESMTPS id D6EBE3858CD1 for ; Mon, 31 Jul 2023 19:39:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6EBE3858CD1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 233A880499; Mon, 31 Jul 2023 19:39:48 +0000 (UTC) Received: from pdx1-sub0-mail-a206.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id AC861814ED; Mon, 31 Jul 2023 19:39:47 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1690832387; a=rsa-sha256; cv=none; b=K2aXTIt5xblmlj+tws/H2TUmINkzo+Tr6kZxi6NMICzY26bHn5BS336GE9rjE5r2CcvW8M POUK8fOKo2aU7EqFUsrsQxIUT47X03Pq8PUMGrJl8QKEti5FMU8zD+NLKWWTgqqvgDYEnX pJIV9rG2HjVWQZ2QzyobbgzTYWDAN7wmeeq1hZ98j8nRNkEX+dZVrRNosNKTeQMb1CPw2o sy41dtNvzonu98FLJ+iLpStIxPuebVSG+5bZUbD3Te8CiKa1qZr7PrZDXggMWHDkmh65eD k/pzQI2Q0aT2t4clDHIOPGaAKAI3Nck1LfLkH97TGqVLcu5jjOyi9OZmrGUafg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1690832387; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gDwDSDqKrqLFA2SSb7r3c89cG12zkVyUl6iuCKkPsLM=; b=ObiYYLyVXW3Mb4M6GYcS4qnnYghE5nABd88tBskZvhe05xbe6e7p6EQzdF7nNewCSDB3+q 9GDRAfp/X5Yv0BJHuV5u7GXz0SSIK1mipP7r5qDh5mIWkSuuhxuDEXjVOXNznpkbTKh6G0 pTDdd8B95qcS6NVSIgyZjIocTv8LvqnmuF0/2DjJ21nkLFApsBbjyWIsn4fvvD+X0AdAl8 OPHDj3ptvzcD48VSaV6VxCtsOz6E4XqTg6fubTYGD5drX8EiWCy7JPSI+OipCrD4xSA6V8 714wgmj42Z4MXjLeusafTigqBZ944DOgrigrZdht1adFGuZt5q3TEyr3FBekOQ== ARC-Authentication-Results: i=1; rspamd-849d547c58-dzv8s; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Oafish-Illegal: 03720576102b1925_1690832387945_2907910793 X-MC-Loop-Signature: 1690832387945:4136737762 X-MC-Ingress-Time: 1690832387945 Received: from pdx1-sub0-mail-a206.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.122.193.216 (trex/6.9.1); Mon, 31 Jul 2023 19:39:47 +0000 Received: from [192.168.2.12] (bras-vprn-toroon4834w-lp130-02-142-113-138-184.dsl.bell.ca [142.113.138.184]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a206.dreamhost.com (Postfix) with ESMTPSA id 4RF7qR0ZFTz1Q; Mon, 31 Jul 2023 12:39:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1690832387; bh=gDwDSDqKrqLFA2SSb7r3c89cG12zkVyUl6iuCKkPsLM=; h=Date:Subject:To:Cc:From:Content-Type:Content-Transfer-Encoding; b=Egp4vV2MFTKJMPITm8hbJbmwJ+vSlArk7NREtxW4Lr98sAloHialwAdVFvtf5Ayrv cM3xbk9iVhLtTchETC54n3+1yrZtD1vcYlncshgIKnW1+QVJD/U7ZsapV5lg7AdfGf HIiEG57j1LVPHdvPhN2FIjLkz2mdDWNm2xsAnYCBr1ENvdHZeuWtizDoYOm5ELYJPI bc5UmvIORvrOdzLsmmO+pm4/UE/eAQ9aUdMRISLRe6xjvJmfOED2W0ibSsJF4iKhAi G+/LqN33tGWF//w9jSDJPNd+yO5TDuxazm67OFXsFvzdKPpbmelfw9U4ui/bwZ4xio +efUCn1ueO5nw== Message-ID: <9a779b3b-f576-90db-ab86-efc53a264592@gotplt.org> Date: Mon, 31 Jul 2023 15:39:45 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [C PATCH]: Add Walloc-type to warn about insufficient size in allocations Content-Language: en-US To: Martin Uecker , gcc-patches@gcc.gnu.org Cc: Joseph Myers References: From: Siddhesh Poyarekar In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3036.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2023-07-21 07:21, Martin Uecker via Gcc-patches wrote: > > > This patch adds a warning for allocations with insufficient size > based on the "alloc_size" attribute and the type of the pointer > the result is assigned to. While it is theoretically legal to > assign to the wrong pointer type and cast it to the right type > later, this almost always indicates an error. Since this catches > common mistakes and is simple to diagnose, it is suggested to > add this warning. > > > Bootstrapped and regression tested on x86. > > > Martin > > > > 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 -Wstrict-flex-arrays 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 --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 7cf411155c6..2e392f9c952 100644 > --- a/gcc/c/c-typeck.cc > +++ b/gcc/c/c-typeck.cc > @@ -7343,6 +7343,32 @@ 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 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)); > + } > + } > + Wouldn't this be much more useful in later phases with ranger feedback like with the warn_access warnings? That way the comparison won't be limited to constant sizes. Thanks, Sid