Convert config framework to use std::regex

This commit is contained in:
Tim Wojtulewicz 2022-06-30 16:33:58 -07:00
parent 1d2c12e980
commit 65ee2287e9

View file

@ -2,11 +2,11 @@
#include "zeek/input/readers/config/Config.h" #include "zeek/input/readers/config/Config.h"
#include <regex.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <cerrno> #include <cerrno>
#include <regex>
#include <sstream> #include <sstream>
#include <unordered_set> #include <unordered_set>
@ -181,28 +181,32 @@ bool Config::DoUpdate()
unseen_options.insert(i.first); unseen_options.insert(i.first);
} }
regex_t re; std::regex re;
if ( regcomp(&re, "^([^[:blank:]]+)[[:blank:]]+([^[:blank:]](.*[^[:blank:]])?)?[[:blank:]]*$", try
REG_EXTENDED) )
{ {
Error(Fmt("Failed to compile regex.")); re.assign("^([^[:blank:]]+)[[:blank:]]+([^[:blank:]](.*[^[:blank:]])?)?[[:blank:]]*$",
std::regex::extended);
}
catch ( const std::regex_error& e )
{
Error(Fmt("Failed to compile regex: %s", e.what()));
return true; return true;
} }
while ( GetLine(line) ) while ( GetLine(line) )
{ {
regmatch_t match[3]; std::smatch match;
if ( regexec(&re, line.c_str(), 3, match, 0) ) if ( ! std::regex_match(line, match, re) )
{ {
Warning( Warning(
Fmt("Could not parse '%s'; line has invalid format. Ignoring line.", line.c_str())); Fmt("Could not parse '%s'; line has invalid format. Ignoring line.", line.c_str()));
continue; continue;
} }
std::string key = line.substr(match[1].rm_so, match[1].rm_eo - match[1].rm_so); std::string key = match[1].str();
std::string value; std::string value;
if ( match[2].rm_so > 0 ) if ( match.size() > 2 )
value = line.substr(match[2].rm_so, match[2].rm_eo - match[2].rm_so); value = match[2].str();
auto typeit = option_types.find(key); auto typeit = option_types.find(key);
if ( typeit == option_types.end() ) if ( typeit == option_types.end() )
@ -283,8 +287,6 @@ bool Config::DoUpdate()
} }
} }
regfree(&re);
if ( Info().mode != MODE_STREAM ) if ( Info().mode != MODE_STREAM )
EndCurrentSend(); EndCurrentSend();