當我試圖用boost :: regex搜索和替換字符串中的一些數據時,我有很多錯誤valgrind。 我得到正確的輸出,但我有點害怕這個錯誤boost :: regex_search和替換valgrind錯誤
有趣的事情是,當我用替換註釋行,我沒有得到任何錯誤來自行搜索。
所以,我有以下功能
static void replaceVar(std::string & input, std::map<std::string, TEMPLATE_PARAM> ¶m)
{
boost::regex ex(REGEX_TAG);
boost::match_results<std::string::const_iterator> extend;
std::string::const_iterator start, end;
boost::match_flag_type flags;
start = input.begin();
end = input.end();
flags = boost::match_default;
while (boost::regex_search(start, end, extend, ex, flags)) // line 78
{
try
{
const std::string *ptr = tools::visitor_fct<std::string>(param.at(extend[1])); // this line is working fine
if (ptr != nullptr)
input = boost::regex_replace(input, ex, *ptr, boost::match_default | boost::format_first_only); //line 84
}
catch (const std::out_of_range& err)
{
input = boost::regex_replace(input, ex, std::string(""), boost::match_default | boost::format_first_only);
}
start = extend[0].second;
end = input.end();
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
}
我碰到的valgrind以下輸出
==21743== Thread 2:
==21743== Invalid read of size 1
==21743== at 0x54C24B0: boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::find_restart_any() (in /usr/local/lib/libboost_regex.so.1.53.0)
==21743== by 0x54D83BA: boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::find_imp() (in /usr/local/lib/libboost_regex.so.1.53.0)
==21743== by 0x4BADB7: bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::string>) (regex_search.hpp:56)
==21743== by 0x4F40D7: bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags) (regex_search.hpp:42)
==21743== by 0x4F2BB4: ... line 78
和
==21743== Address 0x6543315 is 197 bytes inside a block of size 293 free'd
==21743== at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21743== by 0x57EBC12: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21743== by 0x4F2B0C: (l:84)
我認爲這是在創造的所有作業這個錯誤,因爲當我只把
boost::regex_replace(input, ex, *ptr, boost::match_default | boost::format_first_only); //line 84
我沒有得到任何的valgrind錯誤
我也試圖從regex_replace的輸出創建一個新的字符串,然後將其分配到輸出中,但我仍然得到這個錯誤
有任何想法嗎 ?
我將不勝感激,理解爲什麼我被低估了 – Alexis
對我來說,這似乎是一個合理的問題Alexis,只需要一個人在沒有評論的情況下對其進行降級。無論如何,雖然我無法幫助您解決問題,但我確實注意到,在我的程序中使用valgrind時,valgrind廣泛使用boost(特別是日期/時間),因此發現了數千個錯誤。我不知道valgrind是否知道這是過度的錯誤檢測,還是我的boost使用不正確(雖然我一直小心地按照示例文件中的說明使用它)。我想知道boost和valgrind是否可以一起製作有意義的信息。 – Pete855217