From ee0e106ecc53fa2e759155c3f61fed97ff429237 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 17 Nov 2026 12:38:00 +0000 Subject: [PATCH] THRIFT-6562: Report EOF from Ruby BufferedTransport when the wrapped read ends Client: rb BufferedTransport stored the wrapped transport's read result as its buffer without checking it. IOStreamTransport returns nil at the end of its input, as IO#read does, so read_all, read_byte and read_into_buffer raised NoMethodError there instead of a TransportException. An empty string from the wrapped transport made read_byte return nil and read_into_buffer raise TypeError. A nil refill now becomes an empty buffer, so read returns an empty string and read_all reports END_OF_FILE. read_byte and read_into_buffer raise END_OF_FILE when a refill is empty instead of reading past the buffer. Generated-By: amp/0.0.1794916837-g564d50 (model: anthropic/claude-opus-5-5; operator: kpumuk@apache.org) --- .../thrift/transport/buffered_transport.rb | 21 ++++++----- lib/rb/spec/buffered_transport_spec.rb | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lib/rb/lib/thrift/transport/buffered_transport.rb b/lib/rb/lib/thrift/transport/buffered_transport.rb index 09e89e81857..6cbecf760e0 100644 --- a/lib/rb/lib/thrift/transport/buffered_transport.rb +++ b/lib/rb/lib/thrift/transport/buffered_transport.rb @@ -72,7 +72,8 @@ def read(sz) ret = @rbuf.slice(@index - sz, sz) || Bytes.empty_byte_buffer if ret.length == 0 - @rbuf = @transport.read([sz, DEFAULT_BUFFER].max) + # An empty result tells read_all that the input ended. + @rbuf = @transport.read([sz, DEFAULT_BUFFER].max) || Bytes.empty_byte_buffer @index = sz ret = @rbuf.slice(0, sz) || Bytes.empty_byte_buffer end @@ -90,10 +91,7 @@ def read_byte begin # If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it. - if @index >= @rbuf.size - @rbuf = @transport.read(DEFAULT_BUFFER) - @index = 0 - end + fill_read_buffer if @index >= @rbuf.size # The read buffer has some data now, read a single byte. Using get_string_byte() avoids # allocating a temp string of size 1 unnecessarily. @@ -114,10 +112,7 @@ def read_into_buffer(buffer, size) i = 0 while i < size # If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it. - if @index >= @rbuf.size - @rbuf = @transport.read(DEFAULT_BUFFER) - @index = 0 - end + fill_read_buffer if @index >= @rbuf.size # The read buffer has some data now, so copy bytes over to the output buffer. byte = Bytes.get_string_byte(@rbuf, @index) @@ -156,6 +151,14 @@ def to_s private + # Raises at the end of the input instead of reading past the buffer. The + # underlying transport may return nil or an empty string there. + def fill_read_buffer + @rbuf = @transport.read(DEFAULT_BUFFER) || Bytes.empty_byte_buffer + @index = 0 + raise TransportException.new(TransportException::END_OF_FILE, "No more data available") if @rbuf.empty? + end + # Replace exhausted storage; clearing it in place could change returned slices. def clear_consumed_read_buffer unless @rbuf.empty? diff --git a/lib/rb/spec/buffered_transport_spec.rb b/lib/rb/spec/buffered_transport_spec.rb index d9b0626fc7f..32d918157ec 100644 --- a/lib/rb/spec/buffered_transport_spec.rb +++ b/lib/rb/spec/buffered_transport_spec.rb @@ -265,6 +265,41 @@ def flush expect(sink).to receive(:close).ordered.and_raise(IOError, "close failed") expect { transport.close }.to raise_error(IOError, "close failed") end + + describe "at the end of the underlying input" do + # IOStreamTransport#read returns nil at EOF, as IO#read does; other transports may return "". + def each_input + ["", "ab"].each do |data| + stream = Thrift::IOStreamTransport.new(StringIO.new(data.b), StringIO.new) + custom = double("Transport") + allow(custom).to receive(:read).and_return(data.b, "".b) + [stream, custom].each { |underlying| yield data, described_class.new(underlying) } + end + end + + def raise_eof + raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::END_OF_FILE) } + end + + it "returns an empty string from read, so read_all reports EOF" do + each_input do |data, buffered| + expect(buffered.read(4)).to eq(data) + expect(buffered.read(4)).to eq("") + end + each_input { |_, buffered| expect { buffered.read_all(4) }.to raise_eof } + end + + it "raises EOF from read_byte after the last byte" do + each_input do |data, buffered| + expect(Array.new(data.bytesize) { buffered.read_byte }).to eq(data.bytes) + expect { buffered.read_byte }.to raise_eof + end + end + + it "raises EOF from read_into_buffer when the input ends early" do + each_input { |_, buffered| expect { buffered.read_into_buffer(Thrift::Bytes.empty_byte_buffer(4), 4) }.to raise_eof } + end + end end describe Thrift::BufferedTransportFactory do