Skip to content

Commit

Permalink
rules: print error message upon invalid regex
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsmit committed Jan 16, 2022
1 parent efbfe86 commit f254bb6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,15 @@ static inline bool rule_field_matches_string(const char *value, const char *patt
regex_t regex;

// TODO compile each regex only once
if (regcomp(&regex, pattern, REG_NEWLINE | REG_EXTENDED | REG_NOSUB))
int err = regcomp(&regex, pattern, REG_NEWLINE | REG_EXTENDED | REG_NOSUB);
if (err) {
size_t err_size = regerror(err, &regex, NULL, 0);
char *err_buf = malloc(err_size);
regerror(err, &regex, err_buf, err_size);
LOG_W("%s: \"%s\"", err_buf, pattern);
free(err_buf);
return false;
}

for (int i = 0; ; i++) {
if (regexec(&regex, value, 0, NULL, 0))
Expand Down
2 changes: 2 additions & 0 deletions test/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ TEST test_pattern_match(void) {

// Special characters
ASSERT_FALSE(rule_field_matches_string("{", "{"));
ASSERT(rule_field_matches_string("{", "\\{"));
ASSERT(rule_field_matches_string("a", "(a)"));
} else {
// Single character matching
ASSERT(rule_field_matches_string("a", "?"));
Expand Down

0 comments on commit f254bb6

Please sign in to comment.