From patchwork Wed Dec 20 19:20:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 181750 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2871724dyi; Wed, 20 Dec 2023 11:20:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEV4ltNjtWc0HitzWZy9eA1K+7mqlqy4ZeNGyzq6naZEip0a+otM9IFQRy/I3Yr0dOSRo7+ X-Received: by 2002:ac8:598a:0:b0:425:9382:6532 with SMTP id e10-20020ac8598a000000b0042593826532mr4439202qte.10.1703100047235; Wed, 20 Dec 2023 11:20:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1703100047; cv=pass; d=google.com; s=arc-20160816; b=MoCOLIpXK93Di4OndXRJ+22eAj7+oSK7lVrXf3ik0yfIkKS2k3odRdRvK7GxXiLg5I 0b/FNKmCnSFuDImDfoSAgf6S5D8ZHZoJXs6wpYmCadlkeIspTGq0UV9Iy4A/sBSohRmi fnOnUkKtXBq8DL4jQGzT5MEGDTixajcKn199UvnuIR4Lp4l4uwn+E5Lve1+t/t1iQKt9 eZJCgCEiSzUyK13cJz/V2FzzSKNCLUDOuToLgJzViXlnTry5uiGerJOEJ1uLMVFWC9W4 fVFfLxdJh2EwEjjetvLQkB0YzKz4QeytdmlBPCI4V+l5XXh6GnwqnXJWicBAK3DN00Gc OhSA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=n+bXmPxaSKIaN2S+FrqgvuNQp0a//Qef5XBmZww/kgo=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=r6FEFEO7pf+mzLOahqpIEzFrJUS1CkcdujYUL8Y4BIZyPc99Amhk9A5qIJqg101dlO wRBUugimOx/noWX49W/KSJSLHhjYQ6VnOxf6Y+s6FixpVkJs/VVk1FIRVhaQ3WhpBpq9 byilf8gzBHh8k/rtcKpdbF/yAM7Kihj3hCHXRL5mgWjMW01UuyqG1pJHVjLFz8xDYkGZ oADWK4PeFTOSh1I53vGjq4czYdrNMugLTslo26rbFVSmUHSqdeXpG/ufIftBCpEzaMFi goWYUtXbCQCu9sv1Tr0IxBBTMAxeW3K+wDVWSPXVS7Gi5sEsSVpZ4AGzJ3f26awTJYx4 vinA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gcsjj8bT; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u18-20020a05622a14d200b00425a114df71si283883qtx.681.2023.12.20.11.20.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 11:20:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gcsjj8bT; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E3FDF386C598 for ; Wed, 20 Dec 2023 19:20:46 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 058123847725 for ; Wed, 20 Dec 2023 19:20:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 058123847725 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 058123847725 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703100024; cv=none; b=Z4H5N8axvPtyOs9PJeB/h1DHFMWppd8oWfGmi4uzO6Q1wS9n8hY6T/ou1mhuwbnh68YOAtjofVmcrOhBzFwPzgZjr8UoSPY20VkJer0+EMu8TqzMNZoEh7OidV42M2rLNV5MOjJ8bSZzIKXwS+T7IFKkJRNFx2TaKYdUHkgt4OQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703100024; c=relaxed/simple; bh=gcrH2hJSudsND865wuKPivrPAa5bPxDCj1DSQgTp8nI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=qCCIXzxICLhfkOC92iB082vqd5kew1X7JU/RbjeA0bI4H2yY7IqY+9TAgVVEkDyS36urZvi0WEnSEhsr0RDSwmn0gzKwWWGJmIIXIUHdRA23+O6rJOBTcaqNcQ9NRm08q+B+mBDYT+LWI3Z8ktDCn+xQZw7LIEf7JtV+pPMvECg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703100022; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=n+bXmPxaSKIaN2S+FrqgvuNQp0a//Qef5XBmZww/kgo=; b=gcsjj8bTuWC4lqXfbHszP+LWbdFwGgNjuR05eANF4VEmUBqzABPMAXrE+GS4vHDTa3HTe2 6xrs4yEoEOosoAq0EHTV3fcXX+ygAy6XIhPBuU1OKqS6lrEMcpX2QDAC/rIl4oD7vsFgJS Ja2E+Q28hUlmF44lHEhJCN+ILWMPOyc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-403-PJtOkZFKNKWk2t3UCHfzUg-1; Wed, 20 Dec 2023 14:20:20 -0500 X-MC-Unique: PJtOkZFKNKWk2t3UCHfzUg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6D7A98820C4 for ; Wed, 20 Dec 2023 19:20:20 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 31A90492BC7; Wed, 20 Dec 2023 19:20:20 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3BKJKHgw2762287 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 20 Dec 2023 20:20:18 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BKJKHrJ2762286; Wed, 20 Dec 2023 20:20:17 +0100 Date: Wed, 20 Dec 2023 20:20:17 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Enable -Walloc-size and -Wcalloc-transposed-args warnings for C++ Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785829834944474955 X-GMAIL-MSGID: 1785829834944474955 Hi! The following patch enables the -Walloc-size and -Wcalloc-transposed-args warnings for C++ as well. Ok for trunk if it passes bootstrap/regtest? 2023-12-20 Jakub Jelinek gcc/c-family/ * c.opt (Walloc-size): Enable also for C++ and ObjC++. gcc/cp/ * cp-gimplify.cc (cp_genericize_r): If warn_alloc_size, call warn_for_alloc_size for -Walloc-size diagnostics. * semantics.cc (finish_call_expr): If warn_calloc_transposed_args, call warn_for_calloc for -Wcalloc-transposed-args diagnostics. gcc/testsuite/ * g++.dg/warn/Walloc-size-1.C: New test. * g++.dg/warn/Wcalloc-transposed-args-1.C: New test. Jakub --- gcc/c-family/c.opt.jj 2023-12-20 11:31:07.897806698 +0100 +++ gcc/c-family/c.opt 2023-12-20 20:02:36.889910599 +0100 @@ -332,7 +332,7 @@ C ObjC C++ ObjC++ Var(warn_alloca) Warni Warn on any use of alloca. Walloc-size -C ObjC Var(warn_alloc_size) Warning LangEnabledBy(C ObjC, Wextra) +C ObjC C++ ObjC++ Var(warn_alloc_size) Warning LangEnabledBy(C ObjC C++ ObjC++, Wextra) Warn when allocating insufficient storage for the target type of the assigned pointer. Walloc-size-larger-than= --- gcc/cp/cp-gimplify.cc.jj 2023-12-15 10:08:53.877236695 +0100 +++ gcc/cp/cp-gimplify.cc 2023-12-20 20:10:59.689914648 +0100 @@ -2048,6 +2048,25 @@ cp_genericize_r (tree *stmt_p, int *walk case NOP_EXPR: *stmt_p = predeclare_vla (*stmt_p); + + /* Warn of new allocations that are not big enough for the target + type. */ + if (warn_alloc_size + && TREE_CODE (TREE_OPERAND (stmt, 0)) == CALL_EXPR + && POINTER_TYPE_P (TREE_TYPE (stmt))) + { + if (tree fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 0))) + if (DECL_IS_MALLOC (fndecl)) + { + tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl)); + tree alloc_size = lookup_attribute ("alloc_size", attrs); + if (alloc_size) + warn_for_alloc_size (EXPR_LOCATION (stmt), + TREE_TYPE (TREE_TYPE (stmt)), + TREE_OPERAND (stmt, 0), alloc_size); + } + } + if (!wtd->no_sanitize_p && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT) && TYPE_REF_P (TREE_TYPE (stmt))) --- gcc/cp/semantics.cc.jj 2023-12-14 07:49:53.150580801 +0100 +++ gcc/cp/semantics.cc 2023-12-20 20:13:33.773772759 +0100 @@ -2939,15 +2939,24 @@ finish_call_expr (tree fn, vec (mycalloc (42, 42, sizeof *p, 1)); + myfree (p, 42, 42); + p = static_cast (mymalloc (42, 42, sizeof *p)); + myfree (p, 42, 42); + p = static_cast (mymalloc (42, 42, sizeof p)); // { dg-warning "allocation of insufficient size" } + myfree (p, 42, 42); + p = static_cast (mycalloc (42, 42, 1, sizeof p)); // { dg-warning "allocation of insufficient size" } + myfree (p, 42, 42); + bar (static_cast (mymalloc (42, 42, 4))); // { dg-warning "allocation of insufficient size" } + myfree (p, 42, 42); +}