Skip to content

Commit

Permalink
parser_regexp: Check named capture. ref #2330
Browse files Browse the repository at this point in the history
Signed-off-by: Masahiro Nakagawa <[email protected]>
  • Loading branch information
repeatedly committed Mar 15, 2019
1 parent eae8611 commit ab3b452
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/fluent/plugin/parser_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def configure(conf)
@expression = Regexp.compile(@expression.source, options)
end
@regexp = @expression # For backward compatibility

if @expression.named_captures.empty?
raise Fluent::ConfigError, "No named captures in 'expression' parameter. The regexp must have at least one named capture"
end
end

def parse(text)
Expand Down
9 changes: 9 additions & 0 deletions test/plugin/test_parser_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ def create_driver(conf)
end

sub_test_case "configure" do
def test_bad_expression
conf = {
'expression' => %q!/.*/!,
}
assert_raise Fluent::ConfigError do
create_driver(conf)
end
end

def test_default_options
conf = {
'expression' => %q!/^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] \[(?<date>[^\]]*)\] "(?<flag>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)$/!,
Expand Down

0 comments on commit ab3b452

Please sign in to comment.