From patchwork Fri May 26 19:27:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1786662 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=VoKxdkk3; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QSZgq6MsXz20PT for ; Sat, 27 May 2023 05:27:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 627D43857739 for ; Fri, 26 May 2023 19:27:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 627D43857739 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685129252; bh=D0bxTLeBQk1IHzovI5FHIlI9fNRJtH2dNGmrthqe80M=; h=Subject:To:Cc:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=VoKxdkk3w7cE6O18oFJZ3qvHdFQMmMG1IY69Q2VVXy58nstNxlA4W6a6+hsn7StJz bpDHSqBKVXIpIQ7//YiqAluZiTORNg90M6DJZT40hqSvhXW8/rweu5cI4l8Pj4QmIB qoPXhfb8EDhcMWm+FRWImpOzvu9NpG64q1FYXhOk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 76AE23858C5F for ; Fri, 26 May 2023 19:27:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 76AE23858C5F Received: from vra-170-30.tugraz.at (vra-170-30.tugraz.at [129.27.170.30]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4QSZg85829z3wGQ; Fri, 26 May 2023 21:27:00 +0200 (CEST) Message-ID: Subject: [C PATCH] -Wstringop-overflow for parameters with forward-declared sizes To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Fri, 26 May 2023 21:27:00 +0200 User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Uecker via Gcc-patches From: Martin Uecker Reply-To: Martin Uecker Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This is a minor change so that parameter that have forward declarations for with -Wstringop-overflow. Bootstrapped and regression tested on x86_64. c: -Wstringop-overflow for parameters with forward-declared sizes Warnings from -Wstringop-overflow do not appear for parameters declared as VLAs when the bound refers to a parameter forward declaration. This is fixed by splitting the loop that passes through parameters into two, first only recording the positions of all possible size expressions and then processing the parameters. PR c/109970 gcc/c-family: * c-attribs.cc (build_attr_access_from_parms): Split loop to first record all parameters. gcc/testsuite: * gcc.dg/pr109970.c: New test. diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 072cfb69147..e2792ca6898 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -5278,6 +5278,15 @@ build_attr_access_from_parms (tree parms, bool skip_voidptr) tree argtype = TREE_TYPE (arg); if (DECL_NAME (arg) && INTEGRAL_TYPE_P (argtype)) arg2pos.put (arg, argpos); + } + + argpos = 0; + for (tree arg = parms; arg; arg = TREE_CHAIN (arg), ++argpos) + { + if (!DECL_P (arg)) + continue; + + tree argtype = TREE_TYPE (arg); tree argspec = DECL_ATTRIBUTES (arg); if (!argspec) diff --git a/gcc/testsuite/gcc.dg/pr109970.c b/gcc/testsuite/gcc.dg/pr109970.c new file mode 100644 index 00000000000..d234e10455f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr109970.c @@ -0,0 +1,15 @@ +/* PR109970 + * { dg-do compile } + * { dg-options "-Wstringop-overflow" } + * */ + +void bar(int x, char buf[x]); +void foo(int x; char buf[x], int x); + +int main() +{ + char buf[10]; + bar(11, buf); /* { dg-warning "accessing 11 bytes in a region of size 10" } */ + foo(buf, 11); /* { dg-warning "accessing 11 bytes in a region of size 10" } */ +} +