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: 84227 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 2C9D53858C31 for ; Wed, 17 Jan 2024 04:54:18 +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+patchwork=sourceware.org@gcc.gnu.org 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