Project

General

Profile

Actions

Bug #19028

closed

GCC12 Introduces new warn flags `-Wuse-after-free`

Added by eightbitraptor (Matthew Valentine-House) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:110133]

Description

GCC 12 introduced a new warning flag -Wuse-after-free 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.

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 - thank you @nobu (Nobuyoshi Nakada) 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.

I've opened a PR that sets -Wuse-after-free=1 only for GCC versions > 11. An alternative approach might be to use #pragma GCC diagnostic to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

Github PR #6465

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0