From patchwork Wed Jan 17 04:53:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandra Loosemore X-Patchwork-Id: 188682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:42cf:b0:101:a8e8:374 with SMTP id q15csp696492dye; Tue, 16 Jan 2024 20:54:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzxA4E9I2QY9J0D52V3vBzUbowr8o3bghL9h3IihX2A8c1c+gn/uachvnvToRRarsjzTxy X-Received: by 2002:a05:6214:2a84:b0:681:24d2:bb24 with SMTP id jr4-20020a0562142a8400b0068124d2bb24mr11242473qvb.43.1705467273485; Tue, 16 Jan 2024 20:54:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705467273; cv=pass; d=google.com; s=arc-20160816; b=N0pZR3O9rnVM7RJmLG41aTh2Cc1QiJNeJhwQMVStnpdywl+BahDwEtzgTA260NFkGy L0WN81/NC60r+t0vsPYRBTck1yJ9O49Yrad/6D99RwYCSb8vRnriTw+sLBAujcJEZX+6 lAxdDdtIxi/TSoQ9hI3DhDl/nywkM4gB6hGjQZ1XavT1UqEWVJ+y+BXtkf1Jw1tJUG6z cvt5XKfSNbEmS1JO6Mqc2CNLj6inrcnUHGVZ7Gb3B6nLtWRR7+Ox0zASuYhT2nQo2N3B V6ZcJyGEem586tx5tzMRZdtYo2rOys66a5cCPanIYLQ0DCdySb0ikoyxH5DUwNXVrhfV k9Vw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:ironport-sdr :arc-filter:dmarc-filter:delivered-to; bh=zS91qUr/NZM9AreL0QWwC8CFzLu6luyqcraRsk9BB8c=; fh=gk9A5O7Gbglvz1MjrRQ4hZ2i3c68kYkpOZJg1emXh+g=; b=rThmejZloN/flVx8PfqbtDTMY/6617QR9al9D1FIuwvs6AKV3bOfPQxGdu4KQuaXMU c6vFuArWQPPyCqt9TaSBgn4l17xT07JWiNzigCRnt9bk8EQfq9+hyfI8TIvOAgK8Dgq+ cVS01ab/GKFTunx9G5z89N1Q3g3iMhrMO71dfbsrR6wMVThpvVzrbnJ77+DSEby466+H qATvLh7aGxjkxnAOckMo7g9Z3xTFG66UW2Cz1ot9vRfQTVLXjXq6P0xbH2uuQq0J10ZT DvcxH5wXYU51CoSGcDKMk1zHIsWhsElQc84yoGScHNV3api/ACb1nT8es5vu71I9lwg1 RhBw== ARC-Authentication-Results: i=2; mx.google.com; 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" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j13-20020a0c9ccd000000b0067f803fe79esi11264995qvf.363.2024.01.16.20.54.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jan 2024 20:54:33 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 387393858C33 for ; Wed, 17 Jan 2024 04:54:33 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa1.mentor.iphmx.com (esa1.mentor.iphmx.com [68.232.129.153]) by sourceware.org (Postfix) with ESMTPS id 132613858CDB for ; Wed, 17 Jan 2024 04:53:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 132613858CDB Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 132613858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=68.232.129.153 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705467232; cv=none; b=MyjSBV3DEoQucvIjJWa4KltQ2JbMJVJpsEZ2qxveFBSs44WAnKiFviVXPZ+9bm6iKYw9xGB+Zkc0uofWzKq7ap3xjQBnLSZKeOOhTFpPc5j8mT6qRe5jVWIv0b8vJef73YHFZyU6TfU1oTffVro7TMesC6gqrWPlXMElj8soKM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705467232; c=relaxed/simple; bh=IrLKnelvtZFFNgvYWX1xBS5uZMUCkvKsTEQDI2cNzGc=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=w9ypzzoMQ/9FZxNC9xEKie+nG1YuKtytoXYK2/R0CiLOzpK7h1jbe/GIKzouRFQk2HJG75eCKnJMfytZ08DnNtz7lUTUSARLgQWx4751ZeL+GVLe8V802ZQFnK5XRaZroGTQSo17wWtvEX7u0Y8c8m//zXo/aUhF2UpD4Wq2mWw= ARC-Authentication-Results: i=1; server2.sourceware.org X-CSE-ConnectionGUID: gjoFP7eVSrGFPcFcYfd0Tg== X-CSE-MsgGUID: eHMQIRMOSgm3DP78rMwV8A== X-IronPort-AV: E=Sophos;i="6.05,200,1701158400"; d="scan'208";a="31774160" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa1.mentor.iphmx.com with ESMTP; 16 Jan 2024 20:53:48 -0800 IronPort-SDR: IBQ1/6e1CeaPHej3oKVa0cGnvFvs6N6gFm2jv1VWyzPYyOjI65sVap3Wx3uMX2+NqRR1dXgJDu sFUDxDh/FZ7+OGN+Xscite4Qpr/tnDrsyj6YUHUm2JSK07ep/3gUf9hb4U/4ks2wKugqHAg9pn Qeb0lGreEU6/nIxagzUC3OSl4Jb9Bx7VTP+At3sn0TXp0Tff6VVHpFrJSXkyej403x8rzddumo bVCJjkBwK2SZ+r5mNqW9qXCC0aty3Zy2mKCrfBLTNMsgJPbIx3F9QYrcLsXdrx8dZKkrfkscdA zcA= From: Sandra Loosemore To: Subject: [COMMITTED] Move docs for -Wuse-after-free and -Wuseless-cast [PR111693] Date: Tue, 16 Jan 2024 21:53:33 -0700 Message-ID: <20240117045333.469361-1-sandra@codesourcery.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-ClientProxiedBy: svr-orw-mbx-15.mgc.mentorg.com (147.34.90.215) To svr-orw-mbx-13.mgc.mentorg.com (147.34.90.213) X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788312051791993627 X-GMAIL-MSGID: 1788312051791993627 These options were categorized as C++ options, but they apply to all C-family languages. gcc/ChangeLog PR c/111693 * doc/invoke.texi (Option Summary): Move -Wuseless-cast from C++ Language Options to Warning Options. Add entry for -Wuse-after-free. (C++ Dialect Options): Move -Wuse-after-free and -Wuseless-cast from here.... (Warning Options): ...to here. Minor copy-editing to fix typo and grammar. --- gcc/doc/invoke.texi | 158 ++++++++++++++++++++++---------------------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 16e31a3c6db..43fd3c3a3cd 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -271,8 +271,7 @@ in the following sections. -Woverloaded-virtual -Wno-pmf-conversions -Wself-move -Wsign-promo -Wsized-deallocation -Wsuggest-final-methods -Wsuggest-final-types -Wsuggest-override --Wno-terminate -Wuseless-cast -Wno-vexing-parse --Wvirtual-inheritance +-Wno-terminate -Wno-vexing-parse -Wvirtual-inheritance -Wno-virtual-move-assign -Wvolatile -Wzero-as-null-pointer-constant} @item Objective-C and Objective-C++ Language Options @@ -420,6 +419,7 @@ Objective-C and Objective-C++ Dialects}. -Wunused-macros -Wunused-parameter -Wno-unused-result -Wunused-value -Wunused-variable +-Wuse-after-free -Wuse-after-free=@var{n} -Wuseless-cast -Wno-varargs -Wvariadic-macros -Wvector-operation-performance -Wvla -Wvla-larger-than=@var{byte-size} -Wno-vla-larger-than @@ -4814,83 +4814,6 @@ annotations. Warn about overriding virtual functions that are not marked with the @code{override} keyword. -@opindex Wuse-after-free -@opindex Wno-use-after-free -@item -Wuse-after-free -@itemx -Wuse-after-free=@var{n} -Warn about uses of pointers to dynamically allocated objects that have -been rendered indeterminate by a call to a deallocation function. -The warning is enabled at all optimization levels but may yield different -results with optimization than without. - -@table @gcctabopt -@item -Wuse-after-free=1 -At level 1 the warning attempts to diagnose only unconditional uses -of pointers made indeterminate by a deallocation call or a successful -call to @code{realloc}, regardless of whether or not the call resulted -in an actual reallocatio of memory. This includes double-@code{free} -calls as well as uses in arithmetic and relational expressions. Although -undefined, uses of indeterminate pointers in equality (or inequality) -expressions are not diagnosed at this level. -@item -Wuse-after-free=2 -At level 2, in addition to unconditional uses, the warning also diagnoses -conditional uses of pointers made indeterminate by a deallocation call. -As at level 2, uses in equality (or inequality) expressions are not -diagnosed. For example, the second call to @code{free} in the following -function is diagnosed at this level: -@smallexample -struct A @{ int refcount; void *data; @}; - -void release (struct A *p) -@{ - int refcount = --p->refcount; - free (p); - if (refcount == 0) - free (p->data); // warning: p may be used after free -@} -@end smallexample -@item -Wuse-after-free=3 -At level 3, the warning also diagnoses uses of indeterminate pointers in -equality expressions. All uses of indeterminate pointers are undefined -but equality tests sometimes appear after calls to @code{realloc} as -an attempt to determine whether the call resulted in relocating the object -to a different address. They are diagnosed at a separate level to aid -legacy code gradually transition to safe alternatives. For example, -the equality test in the function below is diagnosed at this level: -@smallexample -void adjust_pointers (int**, int); - -void grow (int **p, int n) -@{ - int **q = (int**)realloc (p, n *= 2); - if (q == p) - return; - adjust_pointers ((int**)q, n); -@} -@end smallexample -To avoid the warning at this level, store offsets into allocated memory -instead of pointers. This approach obviates needing to adjust the stored -pointers after reallocation. -@end table - -@option{-Wuse-after-free=2} is included in @option{-Wall}. - -@opindex Wuseless-cast -@opindex Wno-useless-cast -@item -Wuseless-cast @r{(C, Objective-C, C++ and Objective-C++ only)} -Warn when an expression is cast to its own type. This warning does not -occur when a class object is converted to a non-reference type as that -is a way to create a temporary: - -@smallexample -struct S @{ @}; -void g (S&&); -void f (S&& arg) -@{ - g (S(arg)); // make arg prvalue so that it can bind to S&& -@} -@end smallexample - @opindex Wconversion-null @opindex Wno-conversion-null @item -Wno-conversion-null @r{(C++ and Objective-C++ only)} @@ -7739,6 +7662,83 @@ In order to get a warning about an unused function parameter, you must either specify @option{-Wextra -Wunused} (note that @option{-Wall} implies @option{-Wunused}), or separately specify @option{-Wunused-parameter}. +@opindex Wuse-after-free +@opindex Wno-use-after-free +@item -Wuse-after-free @r{(C, Objective-C, C++ and Objective-C++ only)} +@itemx -Wuse-after-free=@var{n} +Warn about uses of pointers to dynamically allocated objects that have +been rendered indeterminate by a call to a deallocation function. +The warning is enabled at all optimization levels but may yield different +results with optimization than without. + +@table @gcctabopt +@item -Wuse-after-free=1 +At level 1 the warning attempts to diagnose only unconditional uses +of pointers made indeterminate by a deallocation call or a successful +call to @code{realloc}, regardless of whether or not the call resulted +in an actual reallocation of memory. This includes double-@code{free} +calls as well as uses in arithmetic and relational expressions. Although +undefined, uses of indeterminate pointers in equality (or inequality) +expressions are not diagnosed at this level. +@item -Wuse-after-free=2 +At level 2, in addition to unconditional uses, the warning also diagnoses +conditional uses of pointers made indeterminate by a deallocation call. +As at level 2, uses in equality (or inequality) expressions are not +diagnosed. For example, the second call to @code{free} in the following +function is diagnosed at this level: +@smallexample +struct A @{ int refcount; void *data; @}; + +void release (struct A *p) +@{ + int refcount = --p->refcount; + free (p); + if (refcount == 0) + free (p->data); // warning: p may be used after free +@} +@end smallexample +@item -Wuse-after-free=3 +At level 3, the warning also diagnoses uses of indeterminate pointers in +equality expressions. All uses of indeterminate pointers are undefined +but equality tests sometimes appear after calls to @code{realloc} as +an attempt to determine whether the call resulted in relocating the object +to a different address. They are diagnosed at a separate level to aid +gradually transitioning legacy code to safe alternatives. For example, +the equality test in the function below is diagnosed at this level: +@smallexample +void adjust_pointers (int**, int); + +void grow (int **p, int n) +@{ + int **q = (int**)realloc (p, n *= 2); + if (q == p) + return; + adjust_pointers ((int**)q, n); +@} +@end smallexample +To avoid the warning at this level, store offsets into allocated memory +instead of pointers. This approach obviates needing to adjust the stored +pointers after reallocation. +@end table + +@option{-Wuse-after-free=2} is included in @option{-Wall}. + +@opindex Wuseless-cast +@opindex Wno-useless-cast +@item -Wuseless-cast @r{(C, Objective-C, C++ and Objective-C++ only)} +Warn when an expression is cast to its own type. This warning does not +occur when a class object is converted to a non-reference type as that +is a way to create a temporary: + +@smallexample +struct S @{ @}; +void g (S&&); +void f (S&& arg) +@{ + g (S(arg)); // make arg prvalue so that it can bind to S&& +@} +@end smallexample + @opindex Wuninitialized @opindex Wno-uninitialized @item -Wuninitialized