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: 99628 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp703750vqr; Fri, 26 May 2023 12:27:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6k97tneh9kf8NTDJ+ZFu3/qEsnygjLQPq4E5OvNBVH26D7ZbmvaHwF9FKYyV2wU4nmGe3D X-Received: by 2002:a17:906:ef06:b0:973:92d4:9f4e with SMTP id f6-20020a170906ef0600b0097392d49f4emr2780926ejs.53.1685129277856; Fri, 26 May 2023 12:27:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685129277; cv=none; d=google.com; s=arc-20160816; b=gRBYJH/7zcGcpayhFBp7qp1Z/EIXQnrFy3WnGW6qltd06vfM0KqFNPDGK9pvO/fZWc DSd4xKAJODac0Bq10xesgayh3tXQe0mb19Ci4eTusBJxGGw6IyPmbLw0ojBhu+lRUJ+8 naStdlflh5rns17hrCgnIZDAcWspAgd9rWRZdLXCJzn0Ccgb+qTJ55TDc0TKu3YPTNwS IA4ilQL9a7dl1qFaQO5ZJCkn9WdUtH9F2ujPB8OdIl9ofb1XYiGXPMxXWfNIo6IIkX+x 0Vqo2/JgqqE0o4lu7m8DdvZB96hBMtm/jkzyQH/0bikdItolJrXmZXGHuWTw/vm9MLIw 6uTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:user-agent:date:cc:to :subject:message-id:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=D0bxTLeBQk1IHzovI5FHIlI9fNRJtH2dNGmrthqe80M=; b=L1SGT5r6ZcMNyMkbj8gDySnn149vZOB+wmLZqy2B+gw+lOzIPTxEmxDmBiMTIv/Axk MBKjpGboUZSZzJE70YFih51uUGftDnlCm/6u7t8hHAiXFZCqPPFZbz+ipvhwjrdn/q1n wVsw9Mzuk4UxiBXHZcsRKf0bmqcUrxx2OwKJfmtwzXtOK8juPoQCc6af/ZPOJAEtmnS5 8IASOJQlzHhC5MvIhzZV1KWMKtcJqp8shyOgBwA8JTI8QNQKyp2AUoHbxOwyF5gQogbF AnV0QSuAkOtn3NrWSx7Xo83at2sQjuIvOsz+AvEWBkYFBXap6oUk6wiFkyV+HfGnIaUy mqvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rxNIf2XG; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id q15-20020a170906144f00b0094f7bc61a0esi100050ejc.969.2023.05.26.12.27.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 May 2023 12:27:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rxNIf2XG; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C0EBE3857717 for ; Fri, 26 May 2023 19:27:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0EBE3857717 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685129276; bh=D0bxTLeBQk1IHzovI5FHIlI9fNRJtH2dNGmrthqe80M=; h=Subject:To:Cc:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=rxNIf2XGRFsbrJvDAnQo5KE/J9ZvU1qvBQ2TXwblH2dbiwqxoAE2H60wFo3dpQV/n mf/vf9YIXsjHAlPmmiArsBnDMSWhUrRHV1h5vXy6r1FfseevmZGkLLPpbci4wyRfaL VqnRMhecjYq5rsdbkrlaBp0kpkl0pwQiGAPgpm6s= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766986117630215619?= X-GMAIL-MSGID: =?utf-8?q?1766986117630215619?= 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" } */ +} +