From 33ac2790b212ee30c88a43e1c783622b46035fd9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 9 Dec 2024 09:46:49 -0800 Subject: [PATCH] ensure inputSize state value is reset during buftok.flush (#16760) (#16770) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit e36cacedc83b8af9ca90453308cb9f46c10c1aff) Co-authored-by: João Duarte --- .../spec/logstash/util/buftok_spec.rb | 29 +++++++++++++++++++ .../logstash/common/BufferedTokenizerExt.java | 3 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/logstash-core/spec/logstash/util/buftok_spec.rb b/logstash-core/spec/logstash/util/buftok_spec.rb index cccb9c476e0..acdf2097709 100644 --- a/logstash-core/spec/logstash/util/buftok_spec.rb +++ b/logstash-core/spec/logstash/util/buftok_spec.rb @@ -43,6 +43,35 @@ expect(subject.extract("\n\n\n")).to eq(["", "", ""]) end + describe 'flush' do + let(:data) { "content without a delimiter" } + before(:each) do + subject.extract(data) + end + + it "emits the contents of the buffer" do + expect(subject.flush).to eq(data) + end + + it "resets the state of the buffer" do + subject.flush + expect(subject).to be_empty + end + + context 'with decode_size_limit_bytes' do + subject { FileWatch::BufferedTokenizer.new("\n", 100) } + + it "emits the contents of the buffer" do + expect(subject.flush).to eq(data) + end + + it "resets the state of the buffer" do + subject.flush + expect(subject).to be_empty + end + end + end + context 'with delimiter' do subject { FileWatch::BufferedTokenizer.new(delimiter) } diff --git a/logstash-core/src/main/java/org/logstash/common/BufferedTokenizerExt.java b/logstash-core/src/main/java/org/logstash/common/BufferedTokenizerExt.java index 2d7b90bba7a..f704fcb0f7f 100644 --- a/logstash-core/src/main/java/org/logstash/common/BufferedTokenizerExt.java +++ b/logstash-core/src/main/java/org/logstash/common/BufferedTokenizerExt.java @@ -106,12 +106,13 @@ public RubyArray extract(final ThreadContext context, IRubyObject data) { public IRubyObject flush(final ThreadContext context) { final IRubyObject buffer = input.join(context); input.clear(); + inputSize = 0; return buffer; } @JRubyMethod(name = "empty?") public IRubyObject isEmpty(final ThreadContext context) { - return input.empty_p(); + return RubyUtil.RUBY.newBoolean(input.isEmpty() && (inputSize == 0)); } }