Bug #112191 When we compile MySQL 8.0 with DEBUG, we have some warning of memory leak
Submitted: 28 Aug 2023 2:52 Modified: 29 Aug 2023 9:15
Reporter: linkang zhang (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:8.0.31 OS:Any
Assigned to: CPU Architecture:Any
Tags: compilation warning, Contribution

[28 Aug 2023 2:52] linkang zhang
Description:
When we compile MySQL 8.0 with DEBUG, we have some warning of memory leak

How to repeat:

mysql_80/storage/innobase/buf/buf0buf.cc:2700:13: warning: ‘void* memcpy(void*, const void*, size_t)’ writing to an object of type ‘struct buf_chunk_t’ with no trivial copy-assignment [-Wclass-memaccess]
 2700 |       memcpy(new_chunks, buf_pool->chunks, n_chunks_copy * sizeof(*chunk));
      |       ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from mysql_80/storage/innobase/include/buf0buf.h:2685,
                 from mysql_80/storage/innobase/include/dict0mem.h:62,
                 from mysql_80/storage/innobase/include/dict0dict.h:43,
                 from mysql_80/storage/innobase/include/btr0btr.h:39,
                 from mysql_80/storage/innobase/buf/buf0buf.cc:42:
mysql_80/storage/innobase/include/buf0buf.ic:52:8: note: ‘struct buf_chunk_t’ declared here

Suggested fix:
diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc
index 49dbb399f8a..d90a491a51e 100644
--- a/storage/innobase/buf/buf0buf.cc
+++ b/storage/innobase/buf/buf0buf.cc
@@ -2697,7 +2697,12 @@ withdraw_retry:
       ulint n_chunks_copy =
           std::min(buf_pool->n_chunks_new, buf_pool->n_chunks);

-      memcpy(new_chunks, buf_pool->chunks, n_chunks_copy * sizeof(*chunk));
+      memcpy((void *)new_chunks, (void *)buf_pool->chunks,
+             n_chunks_copy * sizeof(*chunk));

       for (ulint j = 0; j < n_chunks_copy; j++) {
         buf_pool_register_chunk(&new_chunks[j]);
[28 Aug 2023 2:54] linkang zhang
The compiler think it maybe memory leak, but in fact there is no memory leak, so we add void* to avoid that. 

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: bugfix_for_warning_of_compiler.diff (application/octet-stream, text), 511 bytes.

[28 Aug 2023 4:34] linkang zhang
// You can repeat this by:

cmake --DWITH_DEBUG=1

make -j `cat /proc/cpuinfo | grep processor| wc -l`
[29 Aug 2023 5:00] MySQL Verification Team
Hello linkang zhang,

Thank you for the report and contribution.
I quickly attempted 8.0.34(even 8.0.31) source build on OL7 with the provided cmake(cmake 3.19, gcc 11 along with bundled boost with -DWITH_BOOST) but still not seeing the warnings that you mentioned. Could you please share exact cmake options used, gcc version and OS to reproduce the issue at our end? Thank you.

regards,
Umesh
[29 Aug 2023 5:39] linkang zhang
// Can you repeat this problem by this option ?

build_type=Debug
GCOV=0
mach_tpye=x86_64
os_type=Linux
cmake_version=3.25.1
gcc_version=11.2.1
gcc_path=/opt/rh/devtoolset-11/root/bin/gcc
cxx_path=/opt/rh/devtoolset-11/root/bin/g++
CFLAGS=-O0 -g3 -gdwarf-2 -fexceptions -fno-strict-aliasing -fno-omit-frame-pointer -D_GLIBCXX_USE_CXX11_ABI=0
CXXFLAGS=-O0 -g3 -gdwarf-2 -fexceptions -fno-strict-aliasing -fno-omit-frame-pointer -D_GLIBCXX_USE_CXX11_ABI=0

    cmake .                                \
        -DFORCE_INSOURCE_BUILD=ON          \
        -DMINIMAL_RELWITHDEBINFO=0         \
        -DWITH_DEBUG=$debug                \
        -DINSTALL_LAYOUT=STANDALONE        \
        -DMYSQL_MAINTAINER_MODE=0          \
        -DWITH_EMBEDDED_SERVER=0           \
        -DWITH_JEMALLOC=bundled            \
        -DWITH_MYISAM_STORAGE_ENGINE=1     \
        -DWITH_INNOBASE_STORAGE_ENGINE=1   \
        -DWITH_CSV_STORAGE_ENGINE=1        \
        -DWITH_ARCHIVE_STORAGE_ENGINE=1    \
        -DWITH_BLACKHOLE_STORAGE_ENGINE=1  \
        -DWITH_FEDERATED_STORAGE_ENGINE=1  \
        -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
        -DWITH_TEMPTABLE_STORAGE_ENGINE=1  \
        -DWITH_XENGINE_STORAGE_ENGINE=1    \
        -DENABLED_PROFILING=1              \
        -DENABLED_LOCAL_INFILE=1           \
        -DWITH_BOOST="extra/boost/boost_1_77_0.tar.bz2"
[29 Aug 2023 6:01] linkang zhang
// Maybe you can add some DCMAKE_C_FLAGS of gcc

    cmake .                                \
        -DCMAKE_C_FLAGS="-O0 -g3 -gdwarf-2 -fexceptions -fno-strict-aliasing -fno-omit-frame-pointer -D_GLIBCXX_USE_CXX11_ABI=0" \
        -DFORCE_INSOURCE_BUILD=ON          \
        -DCMAKE_BUILD_TYPE="debug"         \
        -DMINIMAL_RELWITHDEBINFO=0         \
        -DWITH_UNIT_TESTS=0                \
        -DWITH_DEBUG=1                     \
        -DINSTALL_LAYOUT=STANDALONE        \
        -DMYSQL_MAINTAINER_MODE=0          \
        -DWITH_EMBEDDED_SERVER=0           \
        -DWITH_EXTRA_CHARSETS=all          \
        -DWITH_MYISAM_STORAGE_ENGINE=1     \
        -DWITH_INNOBASE_STORAGE_ENGINE=1   \
        -DWITH_CSV_STORAGE_ENGINE=1        \
        -DWITH_ARCHIVE_STORAGE_ENGINE=1    \
        -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
        -DWITH_EXAMPLE_STORAGE_ENGINE=0    \
        -DWITH_TEMPTABLE_STORAGE_ENGINE=1  \
        -DWITH_XENGINE_STORAGE_ENGINE=1    \
        -DWITH_QUERY_TRACE=1               \
        -DWITH_EXTRA_CHARSETS=all          \
        -DDEFAULT_COLLATION=utf8mb4_0900_ai_ci \
        -DENABLED_PROFILING=1              \
        -DENABLED_LOCAL_INFILE=1           \
        -DWITH_BOOST="extra/boost/boost_1_77_0.tar.bz2"
[29 Aug 2023 6:13] MySQL Verification Team
Thank you for the details, let me give it a try and let you know if anything further needed.

regards,
Umesh Shastry
[29 Aug 2023 9:15] MySQL Verification Team
Thank you for the report and contribution.

regards,
Umesh Shastry