From 236cbcaadebf713f316b75b9b9d8218c251c18d6 Mon Sep 17 00:00:00 2001 From: Andrea Selva Date: Wed, 12 Jan 2022 17:59:33 +0100 Subject: [PATCH] [backport 7.17] Avoid to itereate with each_index (#13603) (#13609) Clean backport of #13603 to branch 7.17 (cherry picked from commit e27fdeb25221419ffd2051f77ae3e7240e6378a7) ---- Original commit message: Sometime the deep_replace could be invoked by plugins, using the LogStash::Config::Mixin#validate. This method receives a Ruby hash which could contains Java ArrayList instead of Ruby Array. The iteration method `each_index` is not available for ArrayList, so resort to some form of "plain old way". The reason why an ArrayList is recognized as a Ruby Array is due to the override classes, like RubyJavaIntegration.JavaCollectionOverride that monkey patches Ruby Array, so that a Java Collection could be seen as a RubyArray but it doesn't implement all the abstractions, like `each_index`. Co-authored-by: Karol Bucek Co-authored-by: Ry Biesemeyer --- logstash-core/lib/logstash/util/substitution_variables.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/logstash-core/lib/logstash/util/substitution_variables.rb b/logstash-core/lib/logstash/util/substitution_variables.rb index 90c8962f0..baa47f331 100644 --- a/logstash-core/lib/logstash/util/substitution_variables.rb +++ b/logstash-core/lib/logstash/util/substitution_variables.rb @@ -37,8 +37,9 @@ module ::LogStash::Util::SubstitutionVariables end else if value.is_a?(Array) - value.each_index do | valueArrayIndex| - value[valueArrayIndex] = deep_replace(value[valueArrayIndex]) + value_array_index = 0 + value.each_with_index do |single_value, i| + value[i] = deep_replace(single_value) end else return replace_placeholders(value)