当我尝试使用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的输出创建一个新字符串,然后将其分配给输出但我仍然会收到此错误
有什么想法吗?
答案 0 :(得分:0)
要删除所有这些错误,我只需要在每次更改时重新启动循环,因为字符串的结尾和开头不一样。
所以我只是将循环改为
bool flag = true;
while (flag)
{
flag = false;
start = input.begin();
end = input.end();
while (boost::regex_search(start, end, extend, ex, boost::match_default))
{
try
{
const std::string *ptr = tools::visitor_fct<std::string>(param.at(extend[1]));
if (ptr != nullptr)
input = boost::regex_replace(input, ex, *ptr, boost::match_default | boost::format_first_only);
}
catch (const std::out_of_range& err)
{
input = boost::regex_replace(input, ex, std::string(""), boost::match_default | boost::format_first_only);
}
flag = true;
break;
}
}
一切正常。