Bug #109325 compilation with gcc-11 or gcc-12 in RelWithDebInfo reports many warnings
Submitted: 9 Dec 2022 10:58 Modified: 20 Dec 2022 15:08
Reporter: Przemysław Skibiński (OCA) Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:8.0 OS:Ubuntu (Ubuntu 22.04.1 LTS)
Assigned to: CPU Architecture:x86

[9 Dec 2022 10:58] Przemysław Skibiński
Description:
Building MySQL Server 8.0.31 with the gcc-12 shows a lot of warnings:

1.
/data/mysql-server/mysql-8.0/mysys/crypt_genhash_impl.cc: In function ‘my_make_scrambled_password’:
/data/mysql-server/mysql-8.0/mysys/crypt_genhash_impl.cc:383:16: warning: ‘__builtin_strncat’ output may be truncated copying between 0 and 20 bytes from a string of length 20 [-Wstringop-truncation]
  383 |   (void)strncat(ctbuffer, (const char *)salt, salt_len);

2.
/data/mysql-server/mysql-8.0/include/my_time.h: In function ‘convert_str_to_timestamp’:
/data/mysql-server/mysql-8.0/include/my_time.h:200:19: warning: ‘__builtin_strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
  200 |       std::strncpy(m_deprecation.m_arg, arg, size);
      |                   ^
/data/mysql-server/mysql-8.0/client/mysqlbinlog.cc:2169:22: note: length computed here
 2169 |   if (str_to_datetime(str, strlen(str), &l_time, 0, &status) ||

3.
/data/mysql-server/mysql-8.0/sql/parser_yystype.h:348: warning: type ‘union YYSTYPE’ violates the C++ One Definition Rule [-Wodr]
  348 | union YYSTYPE {
      | 
/data/mysql-server/mysql-8.0/storage/innobase/include/fts0pars.h:50: note: a different type is defined in another translation unit
   50 | typedef union YYSTYPE
      | 
/data/mysql-server/mysql-8.0/sql/parser_yystype.h:349: note: the first difference of corresponding definitions is field ‘lexer’
  349 |   Lexer_yystype lexer;  // terminal values from the lexical scanner
      | 
storage/innobase/fts0pars.y:62: note: a field with different name is defined in another translation unit
/data/mysql-server/mysql-8.0/sql/parser_yystype.h:348:7: warning: type ‘union YYSTYPE’ violates the C++ One Definition Rule [-Wodr]
  348 | union YYSTYPE {
      |       ^
/data/mysql-server/mysql-8.0/storage/innobase/include/fts0pars.h:50: note: a different type is defined in another translation unit
   50 | typedef union YYSTYPE
      | 
/data/mysql-server/mysql-8.0/sql/parser_yystype.h:349:17: note: the first difference of corresponding definitions is field ‘lexer’
  349 |   Lexer_yystype lexer;  // terminal values from the lexical scanner
      |                 ^
../storage/innobase/fts0pars.y:62: note: a field with different name is defined in another translation unit
./sql/sql_yacc.cc:570: warning: type ‘yysymbol_kind_t’ violates the C++ One Definition Rule [-Wodr]
./sql/sql_hints.yy.cc:137: note: an enum with different value name is defined in another translation unit
./sql/sql_yacc.cc:576: note: name ‘YYSYMBOL_ABORT_SYM’ differs from name ‘YYSYMBOL_MAX_EXECUTION_TIME_HINT’ defined in another translation unit
./sql/sql_hints.yy.cc:143: note: mismatching definition
./sql/sql_yacc.h:51: warning: type ‘yytokentype’ violates the C++ One Definition Rule [-Wodr]
/data/mysql-server/mysql-8.0/storage/innobase/include/pars0grm.h:46: note: an enum with different value name is defined in another translation unit
   46 | enum yytokentype {
      | 
./sql/sql_yacc.h:53: note: name ‘YYEMPTY’ differs from name ‘PARS_INT_LIT’ defined in another translation unit
/data/mysql-server/mysql-8.0/storage/innobase/include/pars0grm.h:47: note: mismatching definition
   47 |   PARS_INT_LIT = 258,

4.
In member function ‘__ct ’,
    inlined from ‘__ct ’ at /data/mysql-server/_deps/boost_1_77_0/boost/geometry/srs/projections/proj/ob_tran.hpp:410:23,
    inlined from ‘__ct ’ at /data/mysql-server/_deps/boost_1_77_0/boost/geometry/srs/projections/proj/ob_tran.hpp:549:80,
    inlined from ‘__ct ’ at /data/mysql-server/_deps/boost_1_77_0/boost/geometry/srs/projections/impl/base_dynamic.hpp:116:38,
    inlined from ‘__ct ’ at /data/mysql-server/_deps/boost_1_77_0/boost/geometry/srs/projections/impl/base_dynamic.hpp:147:33,
    inlined from ‘create_new’ at /data/mysql-server/_deps/boost_1_77_0/boost/geometry/srs/projections/proj/ob_tran.hpp:618:24:
/data/mysql-server/_deps/boost_1_77_0/boost/geometry/srs/projections/proj/ob_tran.hpp:182:20: warning: ‘proj_parm.sphip’ may be used uninitialized [-Wmaybe-uninitialized]
  182 |             struct par_ob_tran
      |                    ^

5. 
In member function ‘__ct ’,
    inlined from ‘unpack_varbinary’ at /data/mysql-server/mysql-8.0/storage/ndb/plugin/ndb_util_table.cc:436:55:
/usr/include/c++/12/bits/basic_string.h:617:17: warning: ‘unpacked_str’ may be used uninitialized [-Wmaybe-uninitialized]
  617 |         if (__s == 0 && __n > 0)
      |                 ^
/data/mysql-server/mysql-8.0/storage/ndb/plugin/ndb_util_table.cc: In member function ‘unpack_varbinary’:
/data/mysql-server/mysql-8.0/storage/ndb/plugin/ndb_util_table.cc:431:15: note: ‘unpacked_str’ was declared here
  431 |   const char *unpacked_str;
      |               ^

6.
In function ‘make_unique’,
    inlined from ‘operator()’ at /data/mysql-server/mysql-8.0/unittest/gunit/innodb/sync0rw-t.cc:169:48:
/usr/include/c++/12/bits/unique_ptr.h:1080:30: warning: argument 1 value ‘18446744073709551615’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
 1080 |     { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
      |                              ^
/usr/include/c++/12/new: In member function ‘operator()’:
/usr/include/c++/12/new:128:26: note: in a call to allocation function ‘operator new []’ declared here
  128 | _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)

How to repeat:
git clone https://github.com/mysql/mysql-server.git
mkdir mysql-build
cd mysql-build
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 -DBUILD_CONFIG=mysql_release

Suggested fix:
There is not a single warning when adding "-DWITH_PACKAGE_FLAGS=OFF".
Why? All those issues are related to using "-flto=auto -ffat-lto-objects" in CMAKE_CXX_FLAGS.

I didn't use "-DWITH_LTO=ON". Who sets "-flto" then?
ADD_LINUX_DEB_FLAGS() calls "dpkg-buildflags --get CXXFLAGS" and appends a result to CMAKE_CXX_FLAGS. 

Ubuntu 22.04 Jammy returns:
$ dpkg-buildflags --get CXXFLAGS
-g -O2 -ffile-prefix-map=/data/mysql-server=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security

Suggested fix:
Auto-remove "-flto=auto -ffat-lto-objects" from a dpkg-buildflags result.
[9 Dec 2022 11:25] Przemysław Skibiński
The following patch works fine for me:

diff --git a/cmake/compile_flags.cmake b/cmake/compile_flags.cmake
index fe81e48fd58..10f0553b76b 100644
--- a/cmake/compile_flags.cmake
+++ b/cmake/compile_flags.cmake
@@ -127,6 +127,12 @@ FUNCTION(ADD_LINUX_DEB_FLAGS)
       MESSAGE(FATAL_ERROR
         "WITH_PACKAGE_FLAGS=on but dpkg-buildflags --get failed")
     ENDIF()
+
+    # remove "-flto=auto -ffat-lto-objects" returned on Ubuntu 22.04
+    REMOVE_CMAKE_COMPILER_FLAGS(GET_CFLAGS   "-flto(=[0-9a-z]+)?|-ffat-lto-objects")
+    REMOVE_CMAKE_COMPILER_FLAGS(GET_CXXFLAGS "-flto(=[0-9a-z]+)?|-ffat-lto-objects")
+    REMOVE_CMAKE_COMPILER_FLAGS(GET_LDFLAGS  "-flto(=[0-9a-z]+)?|-ffat-lto-objects")
+
     SET(CMAKE_C_FLAGS   "${GET_CFLAGS}   ${GET_CPPFLAGS}" PARENT_SCOPE)
     SET(CMAKE_CXX_FLAGS "${GET_CXXFLAGS} ${GET_CPPFLAGS}" PARENT_SCOPE)
     SET(CMAKE_EXE_LINKER_FLAGS    "${GET_LDFLAGS}" PARENT_SCOPE)
[9 Dec 2022 13:17] MySQL Verification Team
Hi Mr. Skibinski,

Thank you for your bug report.

Can you, please, let us know the outputs of the following commands:

gcc --version 

and 

gcc -V

Thanks a lot in advance.
[9 Dec 2022 13:34] Przemysław Skibiński
Hi, there are warnings because Ubuntu 22.04 Jammy returns:
$ dpkg-buildflags --get CXXFLAGS
-g -O2 -ffile-prefix-map=/data/mysql-server=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security

The same compilers on Ubuntu 20.04 will be fine because:
$ dpkg-buildflags --get CXXFLAGS
-g -O2 -fdebug-prefix-map=/data/docker=. -fstack-protector-strong -Wformat -Werror=format-security

My gcc versions:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)

$ gcc-12 -v
Using built-in specs.
COLLECT_GCC=gcc-12
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 12.1.0-2ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-12-sZcx2y/gcc-12-12.1.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-sZcx2y/gcc-12-12.1.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (Ubuntu 12.1.0-2ubuntu1~22.04)
[9 Dec 2022 14:23] MySQL Verification Team
Hi,

Since we are supporting Ubuntu 22.10, we are verifying this report.

Verified.
[20 Dec 2022 15:08] Tor Didriksen
Posted by developer:
 
Our automated build system builds RPM or DEB packages, and most modern
platforms have some builtin flags determined by dpkg-buildflags or
'rpm --eval %optflags'

Developers build standalone builds, and by default we add
WITH_PACKAGE_FLAGS to RelWithDebInfo builds, because we want the same
flags as the automated build, otherwise we migth break it.

Having a clean -flto build (on all platforms) is in practice
impossible, since this is a moving target: change some code and the
linker moves some other code around, and discovers some new 'may be
unused'. But we *do* want to see those warnings, so that we can fix
them.

If you don't like the warnings, then simply turn WITH_PACKAGE_FLAGS OFF.
Note that you will lose some performance if you do.
[21 Dec 2022 12:42] MySQL Verification Team
Thank you, Tor .....