diff --git a/lib/fluent/plugin_helper/record_accessor.rb b/lib/fluent/plugin_helper/record_accessor.rb index 9a37a60ff4..3db05965db 100644 --- a/lib/fluent/plugin_helper/record_accessor.rb +++ b/lib/fluent/plugin_helper/record_accessor.rb @@ -140,7 +140,7 @@ def self.parse_dot_array_op(key, param) if i = key.index(']') index_value = key[0..i - 1] raise Fluent::ConfigError, "missing array index in '[]'. Invalid syntax: #{param}" if index_value == ']' - result << Integer(index_value) + result << parse_array_index(index_value, param) key = key[i + 1..-1] in_bracket = false else @@ -159,6 +159,12 @@ def self.parse_dot_array_op(key, param) result end + def self.parse_array_index(index_value, param) + Integer(index_value) + rescue ArgumentError + raise Fluent::ConfigError, "array index must be an integer: '#{index_value}'. Invalid syntax: #{param}" + end + def self.parse_bracket_notation(param) orig_param = param result = [] @@ -180,7 +186,7 @@ def self.parse_bracket_notation(param) if i = param.index(']') index_value = param[0..i - 1] raise Fluent::ConfigError, "missing array index in '[]'. Invalid syntax: #{param}" if index_value == ']' - result << Integer(index_value) + result << parse_array_index(index_value, orig_param) param = param[i + 1..-1] in_bracket = false else diff --git a/test/plugin_helper/test_record_accessor.rb b/test/plugin_helper/test_record_accessor.rb index 4d2b9a4198..5a89d75e17 100644 --- a/test/plugin_helper/test_record_accessor.rb +++ b/test/plugin_helper/test_record_accessor.rb @@ -49,6 +49,8 @@ class Dummy < Fluent::Plugin::TestBase data("missing ']'" => "$['key1'", "missing array index with dot" => "$.hello[]", "missing array index with bracket" => "$[]", + "non-integer array index with dot" => "$.key1[x]", + "non-integer array index with bracket" => "$['key1'][x]", "more chars" => "$.key1[0]foo", "whitespace char included key in dot notation" => "$.key[0].ke y", "empty keys with dot" => "$.", @@ -142,6 +144,8 @@ class Dummy < Fluent::Plugin::TestBase data("missing ']'" => "$['key1'", "missing array index with dot" => "$.hello[]", "missing array index with bracket" => "$['hello'][]", + "non-integer array index with dot" => "$.key1[x]", + "non-integer array index with bracket" => "$['key1'][x]", "whitespace char included key in dot notation" => "$.key[0].ke y", "more chars" => "$.key1[0]foo", "empty keys with dot" => "$.",