Skip to content

Commit

Permalink
Make IO::Buffered#buffer_size= idempotent (#14855)
Browse files Browse the repository at this point in the history
The purpose of raising an exception here is to prevent the caller from doing something unsafe. _Changing_ the value is unsafe, but setting `buffer_size` to the same value is a safe operation.
  • Loading branch information
jgaskins committed Sep 4, 2024
1 parent 24b243b commit 1055af2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ describe "IO::Buffered" do
end
end

it "can set buffer_size to the same value after first use" do
io = BufferedWrapper.new(IO::Memory.new("hello\r\nworld\n"))
io.buffer_size = 16_384
io.gets

io.buffer_size = 16_384
io.buffer_size.should eq(16_384)
end

it "does gets" do
io = BufferedWrapper.new(IO::Memory.new("hello\r\nworld\n"))
io.gets.should eq("hello")
Expand Down
2 changes: 1 addition & 1 deletion src/io/buffered.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module IO::Buffered
# Set the buffer size of both the read and write buffer
# Cannot be changed after any of the buffers have been allocated
def buffer_size=(value)
if @in_buffer || @out_buffer
if (@in_buffer || @out_buffer) && (buffer_size != value)
raise ArgumentError.new("Cannot change buffer_size after buffers have been allocated")
end
@buffer_size = value
Expand Down

0 comments on commit 1055af2

Please sign in to comment.