diff --git a/lib/fluent/plugin/parser_regexp.rb b/lib/fluent/plugin/parser_regexp.rb index c0636ebf34..79ad558269 100644 --- a/lib/fluent/plugin/parser_regexp.rb +++ b/lib/fluent/plugin/parser_regexp.rb @@ -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) diff --git a/test/compat/test_parser.rb b/test/compat/test_parser.rb index 4d5ba27dec..f5261640ba 100644 --- a/test/compat/test_parser.rb +++ b/test/compat/test_parser.rb @@ -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 = "(?.*)" parser = Fluent::TextParser::RegexpParser.new(Regexp.new(source, options), { "dummy" => "dummy" }) regexp = parser.instance_variable_get("@regexp") assert_equal(options, regexp.options) diff --git a/test/plugin/test_parser_regexp.rb b/test/plugin/test_parser_regexp.rb index b8ed02125c..7fc5a98604 100644 --- a/test/plugin/test_parser_regexp.rb +++ b/test/plugin/test_parser_regexp.rb @@ -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!/^(?[^ ]*) [^ ]* (?[^ ]*) \[(?