Merge remote-tracking branch 'origin/topic/timw/636-config-commas'

* origin/topic/timw/636-config-commas:
  GH-636: Fix regex to handle commas at the end of config parser lines
  Convert config framework to use std::regex
This commit is contained in:
Tim Wojtulewicz 2022-07-18 08:54:30 -07:00
commit 33f97fc3fb
5 changed files with 26 additions and 14 deletions

View file

@ -1,3 +1,9 @@
5.1.0-dev.265 | 2022-07-18 08:54:30 -0700
* GH-636: Fix regex to handle commas at the end of config parser lines (Tim Wojtulewicz, Corelight)
* Convert config framework to use std::regex (Tim Wojtulewicz, Corelight)
5.1.0-dev.261 | 2022-07-14 15:46:29 -0700
* Management framework: fix an agent boot-time race condition plus minor tweaks (Christian Kreibich, Corelight)

View file

@ -1 +1 @@
5.1.0-dev.261
5.1.0-dev.265

View file

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

View file

@ -23,7 +23,8 @@ XXXXXXXXXX.XXXXXX testsub 2607:f8b0:4001::/48 2607:f8b0:4002::/48 ../configfile
XXXXXXXXXX.XXXXXX testinterval 1.0 sec 1.0 min ../configfile
XXXXXXXXXX.XXXXXX testtime 0.0 XXXXXXXXXX.XXXXXX ../configfile
XXXXXXXXXX.XXXXXX test_set (empty) a,d,b,c,erdbeerschnitzel ../configfile
XXXXXXXXXX.XXXXXX test_set a,d,b,c,erdbeerschnitzel a,c,b ../configfile
XXXXXXXXXX.XXXXXX test_vector (empty) 1,2,3,4,5,6 ../configfile
XXXXXXXXXX.XXXXXX test_set a,d,b,c,erdbeerschnitzel (empty) ../configfile
XXXXXXXXXX.XXXXXX test_set a,c,b (empty) ../configfile
XXXXXXXXXX.XXXXXX test_set (empty) \x2d ../configfile
#close XXXX-XX-XX-XX-XX-XX

View file

@ -27,6 +27,7 @@ testsub [2607:f8b0:4002:801::]/48
testinterval 60
testtime 1507321987
test_set a,b,c,d,erdbeerschnitzel
test_set a,b,c,
test_vector 1,2,3,4,5,6
test_set
test_set -