Skip to content

Commit a35268a

Browse files
committed
Fix expanding size at ungetc/ungetbyte
1 parent 8655342 commit a35268a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

ext/stringio/stringio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ strio_unget_bytes(struct StringIO *ptr, const char *cp, long cl)
984984
len = RSTRING_LEN(str);
985985
rest = pos - len;
986986
if (cl > pos) {
987-
long ex = (rest < 0 ? cl-pos : cl+rest);
987+
long ex = cl - (rest < 0 ? pos : len);
988988
rb_str_modify_expand(str, ex);
989989
rb_str_set_len(str, len + ex);
990990
s = RSTRING_PTR(str);

test/stringio/test_stringio.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,15 @@ def test_ungetc_padding
757757
assert_equal("b""\0""a", s.string)
758758
end
759759

760+
def test_ungetc_fill
761+
count = 100
762+
s = StringIO.new
763+
s.print 'a' * count
764+
s.ungetc('b' * (count * 5))
765+
assert_equal((count * 5), s.string.size)
766+
assert_match(/\Ab+\z/, s.string)
767+
end
768+
760769
def test_ungetbyte_pos
761770
b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
762771
s = StringIO.new( b )
@@ -782,6 +791,15 @@ def test_ungetbyte_padding
782791
assert_equal("b""\0""a", s.string)
783792
end
784793

794+
def test_ungetbyte_fill
795+
count = 100
796+
s = StringIO.new
797+
s.print 'a' * count
798+
s.ungetbyte('b' * (count * 5))
799+
assert_equal((count * 5), s.string.size)
800+
assert_match(/\Ab+\z/, s.string)
801+
end
802+
785803
def test_frozen
786804
s = StringIO.new
787805
s.freeze
@@ -825,18 +843,17 @@ def test_new_block_warning
825843
end
826844

827845
def test_overflow
828-
omit if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
846+
return if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
829847
limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
830848
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
831849
begin;
832850
limit = #{limit}
833851
ary = []
834-
while true
852+
begin
835853
x = "a"*0x100000
836854
break if [x].pack("p").unpack("i!")[0] < 0
837855
ary << x
838-
omit if ary.size > 100
839-
end
856+
end while ary.size <= 100
840857
s = StringIO.new(x)
841858
s.gets("xxx", limit)
842859
assert_equal(0x100000, s.pos)

0 commit comments

Comments
 (0)