Bug #120602 Memory leak in fil_ibd_same_as_default_path()
Submitted: 2 Jun 11:44 Modified: 3 Jun 7:52
Reporter: Dmitry Lenev (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S2 (Serious)
Version:8.0.46, 8.4.9, 9.7.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: Leak, memory leak, regression

[2 Jun 11:44] Dmitry Lenev
Description:
There is a memory leak in recently added fil_ibd_same_as_default_path() function which can be easily detected by Valgrind or ASAN when running some of MTR tests - for example main.partition_innodb_tablespace test.

How to repeat:
Make ASAN build of the server and run the ./mtr --mem partition_innodb_tablespace command. The test itself will succeed,
but post test error log check will detect memory leak report from LeakSanitizer:

==============================================================================
                  TEST NAME                       RESULT  TIME (ms) COMMENT
------------------------------------------------------------------------------
[ 50%] main.partition_innodb_tablespace          [ pass ]   4601
...
=================================================================
==430952==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 51 byte(s) in 1 object(s) allocated from:
    #0 0x7f26a16b4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5dd5c470fce0 in ut::detail::malloc(unsigned long) ...src/git/mysql-server/storage/innobase/include/detail/ut/allocator_traits.h:72
    #2 0x5dd5c470fd24 in ut::detail::Alloc_fn::malloc(unsigned long) ...src/git/mysql-server/storage/innobase/include/detail/ut/allocator_traits.h:89
    #3 0x5dd5c471b558 in void* ut::detail::Alloc_fn::alloc<false>(unsigned long) ...src/git/mysql-server/storage/innobase/include/detail/ut/allocator_traits.h:98
    #4 0x5dd5c471b5f6 in void* ut::detail::Alloc_pfs::alloc<false>(unsigned long, unsigned int) ...src/git/mysql-server/storage/innobase/include/detail/ut/alloc.h:276
    #5 0x5dd5c471b714 in std::enable_if<ut::detail::Alloc_pfs::is_pfs_instrumented_v, void*>::type ut::detail::Alloc_<ut::detail::Alloc_pfs>::alloc<false, ut::detail::Alloc_pfs>(unsigned long, unsigned int) ...src/git/mysql-server/storage/innobase/include/detail/ut/alloc.h:439
    #6 0x5dd5c471b7ad in ut::malloc_withkey(ut::PSI_memory_key_t, unsigned long) ...src/git/mysql-server/storage/innobase/include/ut0new.h:600
    #7 0x5dd5c502f3a3 in mem_strdup ...src/git/mysql-server/storage/innobase/include/mem0mem.ic:495
    #8 0x5dd5c503e4c5 in Fil_path::make(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ib_file_suffix, bool) ...src/git/mysql-server/storage/innobase/fil/fil0fil.cc:4988
    #9 0x5dd5c503ec21 in fil_ibd_same_as_default_path ...src/git/mysql-server/storage/innobase/fil/fil0fil.cc:9885
    #10 0x5dd5c50427f3 in fil_tablespace_path_equals(unsigned int, char const*, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) ...src/git/mysql-server/storage/innobase/fil/fil0fil.cc:10083
...

Suggested fix:
fil_ibd_same_as_default_path() seems to forget to free memory allocated for default_path string on some of return paths from this function.

I will contribute simple fix for this.
[2 Jun 11:48] Dmitry Lenev
Simple fix for memory leak in fil_ibd_same_as_default_path().

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

Contribution: bug120602-contrib.patch (text/x-patch), 1.50 KiB.

[3 Jun 7:52] MySQL Verification Team
Hi Dmitry, 

Thanks for the report! Verified on 8.0.46, 8.4.9, 9.7.0.