ChristianPanov/lwlog

Invalid memory access reported by valgrind

polter-rnd opened this issue · 1 comments

Valgrind reports invalid memory read in pattern::request_flag_formatters() while running lwlog_sandbox:

$ valgrind ./lwlog_sandbox
==207789== Command: ./lwlog_sandbox
==207789== 
==207789== Invalid read of size 1
==207789==    at 0x484E95D: bcmp (vg_replace_strmem.c:1229)
==207789==    by 0x40E57C: std::__detail::_Map_base<std::basic_string_view<char, std::char_traits<char> >, std::pair<std::basic_string_view<char, std::char_traits<char> > const, std::shared_ptr<lwlog::details::formatter> >, std::allocator<std::pair<std::basic_string_view<char, std::char_traits<char> > const, std::shared_ptr<lwlog::details::formatter> > >, std::__detail::_Select1st, std::equal_to<std::basic_string_view<char, std::char_traits<char> > >, std::hash<std::basic_string_view<char, std::char_traits<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true>, true>::operator[](std::basic_string_view<char, std::char_traits<char> > const&) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x4097EB: lwlog::details::pattern::request_flag_formatters() (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x4085DF: lwlog::sinks::sink<true, lwlog::single_threaded_policy>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x40789E: lwlog::logger<lwlog::configuration<lwlog::enable_time, lwlog::disable_local_time, lwlog::disable_precise_units, lwlog::disable_thread_id, lwlog::disable_process_id>, lwlog::asynchronous_policy<1024ul, lwlog::block_overflow_policy>, lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x4061B4: main (in /lwlog/build/lwlog_sandbox)
==207789==  Address 0x4ddcc7f is 15 bytes inside a block of size 31 free'd
==207789==    at 0x48463F3: operator delete(void*) (vg_replace_malloc.c:1051)
==207789==    by 0x49C2504: UnknownInlinedFun (new_allocator.h:172)
==207789==    by 0x49C2504: UnknownInlinedFun (alloc_traits.h:517)
==207789==    by 0x49C2504: UnknownInlinedFun (basic_string.h:289)
==207789==    by 0x49C2504: UnknownInlinedFun (basic_string.h:283)
==207789==    by 0x49C2504: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:342)
==207789==    by 0x49C368A: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:548)
==207789==    by 0x4085CF: lwlog::sinks::sink<true, lwlog::single_threaded_policy>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x40789E: lwlog::logger<lwlog::configuration<lwlog::enable_time, lwlog::disable_local_time, lwlog::disable_precise_units, lwlog::disable_thread_id, lwlog::disable_process_id>, lwlog::asynchronous_policy<1024ul, lwlog::block_overflow_policy>, lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x4061B4: main (in /lwlog/build/lwlog_sandbox)
==207789==  Block was alloc'd at
==207789==    at 0x4842F95: operator new(unsigned long) (vg_replace_malloc.c:483)
==207789==    by 0x49C248D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:332)
==207789==    by 0x49C368A: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:548)
==207789==    by 0x40837B: lwlog::sinks::sink<true, lwlog::single_threaded_policy>::sink() (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x40820B: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<lwlog::sinks::stdout_sink<lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy>, std::allocator<void>>(lwlog::sinks::stdout_sink<lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy>*&, std::_Sp_alloc_shared_tag<std::allocator<void> >) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x407566: lwlog::logger<lwlog::configuration<lwlog::enable_time, lwlog::disable_local_time, lwlog::disable_precise_units, lwlog::disable_thread_id, lwlog::disable_process_id>, lwlog::asynchronous_policy<1024ul, lwlog::block_overflow_policy>, lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink>::logger<>(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789==    by 0x406188: main (in /lwlog/build/lwlog_sandbox)
==207789==

Hi @polter-rnd, thank you for opening the issue, will be looking into it this week when I have some spare time.

As a side note, the short-term plan for lwlog from here on is to integrate static analysis(Clang Static Analyzer, Cppcheck and Coverity) and runtime analysis(Valgrind, Helgrind and the sanitizers) into the CI pipeline, to ensure a clean, warning-free build and catch such errors before delivering.

Thank you again for finding and addressing the issue, will close it asap,
Have a nice day