Skip to content

Commit

Permalink
IO:::Sized: don't peek underlying IO if read_remaining is zero. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Apr 9, 2017
1 parent d3ae522 commit 3eef15a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/std/io/sized_spec.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/io/sized.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3eef15a

Please sign in to comment.