[ruby/stringio] Fix expanding size at ungetc/ungetbyte
https://github.com/ruby/stringio/commit/a35268a3ac
This commit is contained in:
parent
11793f897c
commit
bb6357cddd
@ -984,7 +984,7 @@ strio_unget_bytes(struct StringIO *ptr, const char *cp, long cl)
|
|||||||
len = RSTRING_LEN(str);
|
len = RSTRING_LEN(str);
|
||||||
rest = pos - len;
|
rest = pos - len;
|
||||||
if (cl > pos) {
|
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_modify_expand(str, ex);
|
||||||
rb_str_set_len(str, len + ex);
|
rb_str_set_len(str, len + ex);
|
||||||
s = RSTRING_PTR(str);
|
s = RSTRING_PTR(str);
|
||||||
|
@ -757,6 +757,15 @@ class TestStringIO < Test::Unit::TestCase
|
|||||||
assert_equal("b""\0""a", s.string)
|
assert_equal("b""\0""a", s.string)
|
||||||
end
|
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
|
def test_ungetbyte_pos
|
||||||
b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
|
b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
|
||||||
s = StringIO.new( b )
|
s = StringIO.new( b )
|
||||||
@ -782,6 +791,15 @@ class TestStringIO < Test::Unit::TestCase
|
|||||||
assert_equal("b""\0""a", s.string)
|
assert_equal("b""\0""a", s.string)
|
||||||
end
|
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
|
def test_frozen
|
||||||
s = StringIO.new
|
s = StringIO.new
|
||||||
s.freeze
|
s.freeze
|
||||||
@ -825,18 +843,17 @@ class TestStringIO < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_overflow
|
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
|
limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
|
||||||
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
|
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
|
||||||
begin;
|
begin;
|
||||||
limit = #{limit}
|
limit = #{limit}
|
||||||
ary = []
|
ary = []
|
||||||
while true
|
begin
|
||||||
x = "a"*0x100000
|
x = "a"*0x100000
|
||||||
break if [x].pack("p").unpack("i!")[0] < 0
|
break if [x].pack("p").unpack("i!")[0] < 0
|
||||||
ary << x
|
ary << x
|
||||||
omit if ary.size > 100
|
end while ary.size <= 100
|
||||||
end
|
|
||||||
s = StringIO.new(x)
|
s = StringIO.new(x)
|
||||||
s.gets("xxx", limit)
|
s.gets("xxx", limit)
|
||||||
assert_equal(0x100000, s.pos)
|
assert_equal(0x100000, s.pos)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user