From 8966cbc58dda3536ae400b83d28a7f75b052da4f Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Thu, 5 Nov 2026 13:06:18 -0300 Subject: [PATCH] record_accessor: raise ConfigError for a non-integer array index A key such as `$.a[x]` or `$['a'][x]` made Integer() raise ArgumentError, so Fluentd stopped with an unexpected error instead of reporting a config error with the file name. Convert it to Fluent::ConfigError like the other syntax errors next to it. Signed-off-by: Lucas Barros --- lib/fluent/plugin_helper/record_accessor.rb | 10 ++++++++-- test/plugin_helper/test_record_accessor.rb | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) 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" => "$.",