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,19 +52,31 @@ 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;
try{
if (boost::regex_match(reinterpret_cast<const char*>(data), if (boost::regex_match(reinterpret_cast<const char*>(data),
reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){ reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){
cout << "BLOCKED " << ele.first << endl; cout << "BLOCKED " << ele.first << endl;
return false; 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;
try{
if (!boost::regex_match(reinterpret_cast<const char*>(data), if (!boost::regex_match(reinterpret_cast<const char*>(data),
reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){ reinterpret_cast<const char*>(data) + bytes_transferred, what, ele.second)){
cout << "BLOCKED " << ele.first << endl; cout << "BLOCKED " << ele.first << endl;
return false; return false;
} }
} catch(...){
#ifdef DEBUG
cout << "Error while matching regex: " << ele.first << endl;
#endif
}
} }
#ifdef DEBUG #ifdef DEBUG
cout << "Packet Accepted!" << endl; cout << "Packet Accepted!" << endl;
@@ -317,6 +329,7 @@ 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);
try{
if (case_sensitive){ if (case_sensitive){
boost::regex regex(reinterpret_cast<char*>(expr), boost::regex regex(reinterpret_cast<char*>(expr),
reinterpret_cast<char*>(expr) + expr_len); reinterpret_cast<char*>(expr) + expr_len);
@@ -332,6 +345,9 @@ void push_regex(char* arg, bool case_sensitive, vector<pair<string,boost::regex>
#endif #endif
v.push_back(make_pair(string(arg), regex)); v.push_back(make_pair(string(arg), regex));
} }
} catch(...){
std::cerr << "Regex " << expr << " was not compiled successfully" << std::endl;
}
} }