From: "eightbitraptor (Matthew Valentine-House)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
Date: Wed, 28 Sep 2022 17:12:12 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-19028.20220928171211.31800@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19028.20220928171211.31800@ruby-lang.org
Issue #19028 has been reported by eightbitraptor (Matthew Valentine-House).
----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028
* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function
Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).
Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`
```
gc.c: In function ‘objspace_xrealloc’:
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]
12213 | TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));
| ^~~~~~~~~~~~~~~~~~~~~~
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’
12123 | else if ((expr)) { \
| ^~~~
In file included from ./include/ruby/defines.h:72,
from ./include/ruby/ruby.h:25,
from constant.h:13,
from gc.c:97:
gc.c:12213:33: note: call to ‘realloc’ here
12213 | TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));
| ^~~~~~~~~~~~~~~~~~~~~~
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’
43 | # define RB_LIKELY(x) (__builtin_expect(!!(x), 1))
| ^
gc.c:12116:13: note: in expansion of macro ‘LIKELY’
12116 | if (LIKELY((expr))) { \
| ^~~~~~
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 | TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));
| ^~~~~~~~~~~
```
My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).
Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.
--
https://bugs.ruby-lang.org/
next parent reply other threads:[~2022-09-28 17:12 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-28 17:12 eightbitraptor (Matthew Valentine-House) [this message]
2022-09-28 18:48 ` [ruby-core:110137] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` mame (Yusuke Endoh)
2022-09-28 23:52 ` [ruby-core:110143] " nobu (Nobuyoshi Nakada)
2022-09-29 1:42 ` [ruby-core:110144] " mame (Yusuke Endoh)
2022-09-29 12:50 ` [ruby-core:110147] " eightbitraptor (Matthew Valentine-House)
2022-09-30 6:34 ` [ruby-core:110152] " nobu (Nobuyoshi Nakada)
2022-09-30 8:00 ` [ruby-core:110153] " eightbitraptor (Matthew Valentine-House)
2022-10-04 2:12 ` [ruby-core:110175] " nobu (Nobuyoshi Nakada)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.issue-19028.20220928171211.31800@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).