Catch errors in regex_match and compile

This commit is contained in:
nik012003
2022-06-21 21:35:56 +02:00
committed by DomySh
parent a2861c386a
commit 006790edc6

View File

@@ -52,18 +52,30 @@ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pa
#endif #endif
for (pair<string,boost::regex> ele:blacklist){ for (pair<string,boost::regex> ele:blacklist){
boost::cmatch what; boost::cmatch what;
if (boost::regex_match(reinterpret_cast<const char*>(data), try{
reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){ if (boost::regex_match(reinterpret_cast<const char*>(data),
cout << "BLOCKED " << ele.first << endl; reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){
return false; cout << "BLOCKED " << ele.first << endl;
return false;
}
} catch(...){
#ifdef DEBUG
cout << "Error while matching regex: " << ele.first << endl;
#endif
} }
} }
for (pair<string,boost::regex> ele:whitelist){ for (pair<string,boost::regex> ele:whitelist){
boost::cmatch what; boost::cmatch what;
if (!boost::regex_match(reinterpret_cast<const char*>(data), try{
reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){ if (!boost::regex_match(reinterpret_cast<const char*>(data),
cout << "BLOCKED " << ele.first << endl; reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){
return false; cout << "BLOCKED " << ele.first << endl;
return false;
}
} catch(...){
#ifdef DEBUG
cout << "Error while matching regex: " << ele.first << endl;
#endif
} }
} }
#ifdef DEBUG #ifdef DEBUG
@@ -317,20 +329,24 @@ void push_regex(char* arg, bool case_sensitive, vector<pair<string,boost::regex>
size_t expr_len = (strlen(arg)-2)/2; size_t expr_len = (strlen(arg)-2)/2;
char expr[expr_len]; char expr[expr_len];
unhexlify(arg+2, arg+strlen(arg)-1, expr); unhexlify(arg+2, arg+strlen(arg)-1, expr);
if (case_sensitive){ try{
boost::regex regex(reinterpret_cast<char*>(expr), if (case_sensitive){
reinterpret_cast<char*>(expr) + expr_len); boost::regex regex(reinterpret_cast<char*>(expr),
#ifdef DEBUG reinterpret_cast<char*>(expr) + expr_len);
cout << "Added case sensitive regex " << expr << endl; #ifdef DEBUG
#endif cout << "Added case sensitive regex " << expr << endl;
v.push_back(make_pair(string(arg), regex)); #endif
} else { v.push_back(make_pair(string(arg), regex));
boost::regex regex(reinterpret_cast<char*>(expr), } else {
reinterpret_cast<char*>(expr) + expr_len, boost::regex::icase); boost::regex regex(reinterpret_cast<char*>(expr),
#ifdef DEBUG reinterpret_cast<char*>(expr) + expr_len, boost::regex::icase);
cout << "Added case insensitive regex " << expr << endl; #ifdef DEBUG
#endif cout << "Added case insensitive regex " << expr << endl;
v.push_back(make_pair(string(arg), regex)); #endif
v.push_back(make_pair(string(arg), regex));
}
} catch(...){
std::cerr << "Regex " << expr << " was not compiled successfully" << std::endl;
} }
} }