ruby-changes:72006
From: Nobuyoshi <ko1@a...>
Date: Mon, 30 May 2022 12:51:54 +0900 (JST)
Subject: [ruby-changes:72006] bb6357cddd (master): [ruby/stringio] Fix expanding size at ungetc/ungetbyte
https://git.ruby-lang.org/ruby.git/commit/?id=bb6357cddd From bb6357cddd5af2c244348e96388a10f7cc6f25b4 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Tue, 16 Nov 2021 17:39:32 +0900 Subject: [ruby/stringio] Fix expanding size at ungetc/ungetbyte https://github.com/ruby/stringio/commit/a35268a3ac --- ext/stringio/stringio.c | 2 +- test/stringio/test_stringio.rb | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 3f37c7c66d..f452bd0da0 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -984,7 +984,7 @@ strio_unget_bytes(struct StringIO *ptr, const char *cp, long cl) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L984 len = RSTRING_LEN(str); rest = pos - len; if (cl > pos) { - long ex = (rest < 0 ? cl-pos : cl+rest); + long ex = cl - (rest < 0 ? pos : len); rb_str_modify_expand(str, ex); rb_str_set_len(str, len + ex); s = RSTRING_PTR(str); diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index e0b4504b54..144a9f4292 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -757,6 +757,15 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L757 assert_equal("b""\0""a", s.string) end + def test_ungetc_fill + count = 100 + s = StringIO.new + s.print 'a' * count + s.ungetc('b' * (count * 5)) + assert_equal((count * 5), s.string.size) + assert_match(/\Ab+\z/, s.string) + end + def test_ungetbyte_pos b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011' s = StringIO.new( b ) @@ -782,6 +791,15 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L791 assert_equal("b""\0""a", s.string) end + def test_ungetbyte_fill + count = 100 + s = StringIO.new + s.print 'a' * count + s.ungetbyte('b' * (count * 5)) + assert_equal((count * 5), s.string.size) + assert_match(/\Ab+\z/, s.string) + end + def test_frozen s = StringIO.new s.freeze @@ -825,18 +843,17 @@ class TestStringIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L843 end def test_overflow - omit if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"] + return if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"] limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10 assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}") begin; limit = #{limit} ary = [] - while true + begin x = "a"*0x100000 break if [x].pack("p").unpack("i!")[0] < 0 ary << x - omit if ary.size > 100 - end + end while ary.size <= 100 s = StringIO.new(x) s.gets("xxx", limit) assert_equal(0x100000, s.pos) -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/