Skip to content

Commit 3eef15a

Browse files
author
Ary Borenszweig
committedApr 9, 2017
IO:::Sized: don't peek underlying IO if read_remaining is zero. Fixes #4261
1 parent d3ae522 commit 3eef15a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

Diff for: ‎spec/std/io/sized_spec.cr

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
require "spec"
22

3+
private class NoPeekIO
4+
include IO
5+
6+
def read(bytes : Bytes)
7+
0
8+
end
9+
10+
def write(bytes : Bytes)
11+
0
12+
end
13+
14+
def peek
15+
raise "shouldn't be invoked"
16+
end
17+
end
18+
319
describe "IO::Sized" do
420
describe "#read" do
521
it "doesn't read past the limit when reading char-by-char" do
@@ -117,6 +133,11 @@ describe "IO::Sized" do
117133
sized.peek.should eq(Bytes.empty)
118134
end
119135

136+
it "doesn't peek when remaining = 0 (#4261)" do
137+
sized = IO::Sized.new(NoPeekIO.new, read_size: 0)
138+
sized.peek.should eq(Bytes.empty)
139+
end
140+
120141
it "skips" do
121142
io = IO::Memory.new "123456789"
122143
sized = IO::Sized.new(io, read_size: 6)

Diff for: ‎src/io/sized.cr

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ module IO
5252
def peek
5353
check_open
5454

55+
return Bytes.empty if @read_remaining == 0 # EOF
56+
5557
peek = @io.peek
5658
return nil unless peek
5759

0 commit comments

Comments
 (0)
Please sign in to comment.