Skip to content

Commit

Permalink
Merge pull request #2331 from fluent/check-parser_regexp-named-capture
Browse files Browse the repository at this point in the history
parser_regexp: Check named capture. ref #2330
  • Loading branch information
repeatedly authored Mar 15, 2019
2 parents eae8611 + 6a263b1 commit d18f9b7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion test/compat/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_setting_estimate_current_event_value
multiline: Regexp::MULTILINE,
both: Regexp::IGNORECASE & Regexp::MULTILINE)
def test_regexp_parser_config(options)
source = "a"
source = "(?<test>.*)"
parser = Fluent::TextParser::RegexpParser.new(Regexp.new(source, options), { "dummy" => "dummy" })
regexp = parser.instance_variable_get("@regexp")
assert_equal(options, regexp.options)
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 d18f9b7

Please sign in to comment.