[MDEV-27968] GCC 12 -Og -Wmaybe-uninitialized in udf_handler::fix_fields() Created: 2022-03-01  Updated: 2022-03-01  Resolved: 2022-03-01

Status: Closed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9
Fix Version/s: 10.2.44, 10.3.35, 10.4.25, 10.5.16, 10.6.8, 10.7.4, 10.8.3

Type: Bug Priority: Major
Reporter: Marko Mäkelä Assignee: Marko Mäkelä
Resolution: Fixed Votes: 0
Labels: None


 Description   

The following compiler is issuing a warning that I cannot determine whether it is bogus or not:

gcc-12 (Debian 12-20220222-1) 12.0.1 20220222 (experimental) [master r12-7325-g2f59f067610]

10.2 ed691eca99da36f3c20558edd08786224539a7ab

/mariadb/10.2o/sql/item_func.cc: In member function ‘bool udf_handler::fix_fields(THD*, Item_func_or_sum*, uint, Item**)’:
/mariadb/10.2o/sql/item_func.cc:3507:39: error: ‘<anonymous>’ may be used uninitialized [-Werror=maybe-uninitialized]
 3507 |     if (!(buffers=new String[arg_count]) ||
      |                                       ^
/mariadb/10.2o/sql/item_func.cc:3507:39: note: ‘<anonymous>’ was declared here
 3507 |     if (!(buffers=new String[arg_count]) ||
      |                                       ^
cc1plus: all warnings being treated as errors

The 10.6 version of the same looks a little different:

10.6 fd5a6d0f75f1435e6b15409516ccba54b8fee145

/mariadb/10.6/sql/item_func.cc: In member function ‘bool udf_handler::fix_fields(THD*, Item_func_or_sum*, uint, Item**)’:
/mariadb/10.6/sql/item_func.cc:3513:55: error: ‘<anonymous>’ may be used uninitialized [-Werror=maybe-uninitialized]
 3513 |     if (!(buffers=new (thd->mem_root) String[arg_count]) ||
      |                                                       ^
/mariadb/10.6/sql/item_func.cc:3513:55: note: ‘<anonymous>’ was declared here
 3513 |     if (!(buffers=new (thd->mem_root) String[arg_count]) ||
      |                                                       ^

The following patch (below, it is for 10.6) would work around it:

diff --git a/sql/item_func.cc b/sql/item_func.cc
index 92f6255d958..c5f14dcd64f 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -3448,6 +3448,10 @@ void udf_handler::cleanup()
   }
 }
 
+#if defined __GNUC__ && __GNUC__ == 12
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#endif
 
 bool
 udf_handler::fix_fields(THD *thd, Item_func_or_sum *func,
@@ -3612,6 +3616,9 @@ udf_handler::fix_fields(THD *thd, Item_func_or_sum *func,
   }
   DBUG_RETURN(FALSE);
 }
+#if defined __GNUC__ && __GNUC__ == 12
+#pragma GCC diagnostic pop
+#endif
 
 
 bool udf_handler::get_arguments()

If I replace the -Og with -O2 or -O3 or omit it altogether, no warning will be issued.
Here is a slightly truncated compiler invocation that will issue this warning but also some others:

g++-12 -DHAVE_CONFIG_H -DMYSQL_SERVER -I/mariadb/10.6/wsrep-lib/include -I/mariadb/10.6/wsrep-lib/wsrep-API/v26 -I/dev/shm/10.6/include -I/mariadb/10.6/include -I/mariadb/10.6/sql -Og -Wall -Wno-unused-parameter -std=gnu++11 -MD -MT sql/CMakeFiles/sql.dir/item_func.cc.o -MF sql/CMakeFiles/sql.dir/item_func.cc.o.d -o sql/CMakeFiles/sql.dir/item_func.cc.o -c /mariadb/10.6/sql/item_func.cc



 Comments   
Comment by Marko Mäkelä [ 2022-03-01 ]

I tried and failed to create a simple test program for this. But, like in the past with some -Og -Wmaybe-unitialized, moving the offending piece of code out of the if helps also here:

@@ -3504,7 +3504,8 @@ udf_handler::fix_fields(THD *thd, Item_func_or_sum *func,
       f_args.arg_type[i]=item->result_type();
     }
     //TODO: why all following memory is not allocated with 1 thd->alloc() call?
-    if (!(buffers=new String[arg_count]) ||
+    buffers= new String[arg_count];
+    if (!buffers ||
 	!(f_args.args= (char**) thd->alloc(arg_count * sizeof(char *))) ||
 	!(f_args.lengths= (ulong*) thd->alloc(arg_count * sizeof(long))) ||
 	!(f_args.maybe_null= (char*) thd->alloc(arg_count * sizeof(char))) ||

Generated at Thu Feb 08 09:57:01 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.