使用 boost::smatch 在 valgrind 中读取大小 1 无效
Invalid read size 1 in valgrind using boost::smatch
我正在使用 boost::smatch 对象从 boost::regex_match 获取匹配模式,并使用该匹配模式字符串进行进一步的操作。它在正常 运行 中工作正常,但是当我使用 Valgrind 进行内存检查时,它在我正在读取匹配字符串的行中显示错误,即 match.str(1)
我试图通过添加对 smatch 对象大小的检查来消除此错误,但仍然出现错误。
//[Modified function name because of policy]
if(boost::regex_match(arg_str,match,dcv::func_name_regex)
&& match.size() > 1) {
std::string func_name = match.str(1);
std::string modified_arg_str = replaceText(arg_str,func_name);
lines.replace(
func_lines.find(arg_str),
arg_str.length(),
modified_arg_str
);
continue;
}
Valgrind 输出为:
[Modified some filename and path because of policy]
3201 ==7723== Invalid read of size 1
3202 ==7723== at 0x517259: snps_boost_1_72_0::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::str() const (../../include/boost/regex/v4/sub_match.hpp:84)
3203 ==7723== by 0x516205: snps_boost_1_72_0::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<snps_boost_1_72_0::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::str(int) const (../../include/boost/regex/v4/match_results.hpp:207)
3204 ==7723== by 0x50DDF7: Modifier::replaceText(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (/path/to/file.cpp:634)
3205 ==7723== by 0x50D227: Modifier::functionArgument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (/path/to/file.cpp:577)
无效读取与读取匹配字符串的行相同。
std::string func_name = match.str(1);
我正在使用 gcc-7.3.0 和 boost_1_72_0。
感谢 ASAN/UBSAN/Valgrind 的联合。你明白为什么它很重要:)
您正在读取未成功匹配的子组。您可能想检查它是否完成,检查 size()>1
是不够的:
#include <boost/regex.hpp>
#include <iostream>
#include <iomanip>
namespace dcv {
boost::regex func_name_regex("z|(.)");
}
int main() {
boost::smatch match;
for (std::string arg_str: {"a","z"}) {
if(boost::regex_match(arg_str, match, dcv::func_name_regex)) {
std::cout << "Matched: " << std::quoted(arg_str) << " (" << match.size() << ")\n";
if (match[1].matched) {
std::string func_name = match.str(1);
std::cout << "Subgroup matched: " << std::quoted(func_name) << "\n";
} else {
std::cout << "Subgroup NOT matched.\n";
}
}
}
}
版画
Matched: "a" (2)
Subgroup matched: "a"
Matched: "z" (2)
Subgroup NOT matched.
我正在使用 boost::smatch 对象从 boost::regex_match 获取匹配模式,并使用该匹配模式字符串进行进一步的操作。它在正常 运行 中工作正常,但是当我使用 Valgrind 进行内存检查时,它在我正在读取匹配字符串的行中显示错误,即 match.str(1)
我试图通过添加对 smatch 对象大小的检查来消除此错误,但仍然出现错误。
//[Modified function name because of policy]
if(boost::regex_match(arg_str,match,dcv::func_name_regex)
&& match.size() > 1) {
std::string func_name = match.str(1);
std::string modified_arg_str = replaceText(arg_str,func_name);
lines.replace(
func_lines.find(arg_str),
arg_str.length(),
modified_arg_str
);
continue;
}
Valgrind 输出为:
[Modified some filename and path because of policy]
3201 ==7723== Invalid read of size 1
3202 ==7723== at 0x517259: snps_boost_1_72_0::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::str() const (../../include/boost/regex/v4/sub_match.hpp:84)
3203 ==7723== by 0x516205: snps_boost_1_72_0::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<snps_boost_1_72_0::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::str(int) const (../../include/boost/regex/v4/match_results.hpp:207)
3204 ==7723== by 0x50DDF7: Modifier::replaceText(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (/path/to/file.cpp:634)
3205 ==7723== by 0x50D227: Modifier::functionArgument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (/path/to/file.cpp:577)
无效读取与读取匹配字符串的行相同。
std::string func_name = match.str(1);
我正在使用 gcc-7.3.0 和 boost_1_72_0。
感谢 ASAN/UBSAN/Valgrind 的联合。你明白为什么它很重要:)
您正在读取未成功匹配的子组。您可能想检查它是否完成,检查 size()>1
是不够的:
#include <boost/regex.hpp>
#include <iostream>
#include <iomanip>
namespace dcv {
boost::regex func_name_regex("z|(.)");
}
int main() {
boost::smatch match;
for (std::string arg_str: {"a","z"}) {
if(boost::regex_match(arg_str, match, dcv::func_name_regex)) {
std::cout << "Matched: " << std::quoted(arg_str) << " (" << match.size() << ")\n";
if (match[1].matched) {
std::string func_name = match.str(1);
std::cout << "Subgroup matched: " << std::quoted(func_name) << "\n";
} else {
std::cout << "Subgroup NOT matched.\n";
}
}
}
}
版画
Matched: "a" (2)
Subgroup matched: "a"
Matched: "z" (2)
Subgroup NOT matched.