aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNAKAMURA Usaku <usa@ruby-lang.org>2021-12-31 19:48:24 +0900
committerNAKAMURA Usaku <usa@ruby-lang.org>2021-12-31 19:48:24 +0900
commit426266af2cbfba8d42a82abcce1bb3ca6c039e87 (patch)
treee76143fff5a4ec330c1752f95bdca2beef27ee23
parent7d3cff6e41d73a00f3d0fdb337743f654e42f1a8 (diff)
downloadruby-426266af2cbfba8d42a82abcce1bb3ca6c039e87.tar.gz
merge revision(s) fdf39963490cf2cf95b30d91bb9b35964c2c2350: [Backport #18421]
Empty and return the buffer if zero size is given [Bug #18421] In `IO#readpartial` and `IO#read_nonblock`, as well as `IO#read`. --- io.c | 8 ++++++-- test/ruby/test_io.rb | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-)
-rw-r--r--io.c8
-rw-r--r--test/ruby/test_io.rb21
-rw-r--r--version.h2
3 files changed, 28 insertions, 3 deletions
diff --git a/io.c b/io.c
index 868756ffc5..4d32e2ee3b 100644
--- a/io.c
+++ b/io.c
@@ -2822,8 +2822,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);
@@ -2965,8 +2967,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) {
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 306f0bcce0..1e4d88ef1e 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -1446,6 +1446,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 = ""
@@ -1491,6 +1498,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 = ""
@@ -1528,6 +1542,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)
diff --git a/version.h b/version.h
index 6e13ad932f..cf0de1076e 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 206
+#define RUBY_PATCHLEVEL 207
#define RUBY_RELEASE_YEAR 2021
#define RUBY_RELEASE_MONTH 12