当我尝试使用 boost::regex 搜索和替换字符串中的某些数据时,我遇到了很多错误 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 的输出创建一个新字符串,然后将其分配给输出,但我仍然收到此错误
有任何想法吗 ?