[前][次][番号順一覧][スレッド一覧]

ruby-changes:71378

From: nagachika <ko1@a...>
Date: Sat, 12 Mar 2022 16:54:24 +0900 (JST)
Subject: [ruby-changes:71378] 7c05379063 (ruby_3_0): merge revision(s) fdf39963490cf2cf95b30d91bb9b35964c2c2350: [Backport #18421]

https://git.ruby-lang.org/ruby.git/commit/?id=7c05379063

From 7c0537906314f0c2a317b37661ccdec8dddc6277 Mon Sep 17 00:00:00 2001
From: nagachika <nagachika@r...>
Date: Sat, 12 Mar 2022 16:30:52 +0900
Subject: 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(-)
---
 io.c                 |  8 ++++++--
 test/ruby/test_io.rb | 21 +++++++++++++++++++++
 version.h            |  2 +-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/io.c b/io.c
index 521b0b0e9a..9720aed06e 100644
--- a/io.c
+++ b/io.c
@@ -2982,8 +2982,10 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int no_exception, int nonblock) https://github.com/ruby/ruby/blob/trunk/io.c#L2982
     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);
@@ -3126,8 +3128,10 @@ io_read_nonblock(rb_execution_context_t *ec, VALUE io, VALUE length, VALUE str, https://github.com/ruby/ruby/blob/trunk/io.c#L3128
     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 58a739be22..5b84fd6a4d 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -1482,6 +1482,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1482
     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 = ""
@@ -1527,6 +1534,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1534
     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 = ""
@@ -1564,6 +1578,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1578
     }
   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 924fe5a599..aeaeaeb323 100644
--- a/version.h
+++ b/version.h
@@ -12,7 +12,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L12
 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
 #define RUBY_VERSION_TEENY 4
 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 186
+#define RUBY_PATCHLEVEL 187
 
 #define RUBY_RELEASE_YEAR 2022
 #define RUBY_RELEASE_MONTH 3
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]