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: 82606 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A07F23865C30 for ; Wed, 20 Dec 2023 19:20:39 +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+patchwork=sourceware.org@gcc.gnu.org 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); +}