diff --git a/spec/std/io/sized_spec.cr b/spec/std/io/sized_spec.cr index 36175f296575..531b6cb15df5 100644 --- a/spec/std/io/sized_spec.cr +++ b/spec/std/io/sized_spec.cr @@ -1,5 +1,21 @@ require "spec" +private class NoPeekIO + include IO + + def read(bytes : Bytes) + 0 + end + + def write(bytes : Bytes) + 0 + end + + def peek + raise "shouldn't be invoked" + end +end + describe "IO::Sized" do describe "#read" do it "doesn't read past the limit when reading char-by-char" do @@ -117,6 +133,11 @@ describe "IO::Sized" do sized.peek.should eq(Bytes.empty) end + it "doesn't peek when remaining = 0 (#4261)" do + sized = IO::Sized.new(NoPeekIO.new, read_size: 0) + sized.peek.should eq(Bytes.empty) + end + it "skips" do io = IO::Memory.new "123456789" sized = IO::Sized.new(io, read_size: 6) diff --git a/src/io/sized.cr b/src/io/sized.cr index 43723367833b..68d35a8bdfe3 100644 --- a/src/io/sized.cr +++ b/src/io/sized.cr @@ -52,6 +52,8 @@ module IO def peek check_open + return Bytes.empty if @read_remaining == 0 # EOF + peek = @io.peek return nil unless peek