1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Empty and return the buffer if zero size is given [Bug #18421]

In `IO#readpartial` and `IO#read_nonblock`, as well as `IO#read`.
This commit is contained in:
Nobuyoshi Nakada 2021-12-22 15:33:12 +09:00
parent 9902398d86
commit fdf3996349
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2021-12-22 18:25:01 +09:00
2 changed files with 27 additions and 2 deletions

8
io.c
View file

@ -3161,8 +3161,10 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int no_exception, int nonblock)
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
if (len == 0)
if (len == 0) {
io_set_read_length(str, 0, shrinkable);
return str;
}
if (!nonblock)
READ_CHECK(fptr);
@ -3305,8 +3307,10 @@ io_read_nonblock(rb_execution_context_t *ec, VALUE io, VALUE length, VALUE str,
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
if (len == 0)
if (len == 0) {
io_set_read_length(str, 0, shrinkable);
return str;
}
n = read_buffered_data(RSTRING_PTR(str), len, fptr);
if (n <= 0) {

View file

@ -1494,6 +1494,13 @@ class TestIO < Test::Unit::TestCase
end)
end
def test_readpartial_zero_size
File.open(IO::NULL) do |r|
assert_empty(r.readpartial(0, s = "01234567"))
assert_empty(s)
end
end
def test_readpartial_buffer_error
with_pipe do |r, w|
s = ""
@ -1539,6 +1546,13 @@ class TestIO < Test::Unit::TestCase
end)
end
def test_read_zero_size
File.open(IO::NULL) do |r|
assert_empty(r.read(0, s = "01234567"))
assert_empty(s)
end
end
def test_read_buffer_error
with_pipe do |r, w|
s = ""
@ -1576,6 +1590,13 @@ class TestIO < Test::Unit::TestCase
}
end
def test_read_nonblock_zero_size
File.open(IO::NULL) do |r|
assert_empty(r.read_nonblock(0, s = "01234567"))
assert_empty(s)
end
end
def test_write_nonblock_simple_no_exceptions
pipe(proc do |w|
w.write_nonblock('1', exception: false)