diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index fb885e3..d960df8 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1289,7 +1289,7 @@ strio_unget_bytes(struct StringIO *ptr, const char *cp, rb_len_t cl) if (rest > cl) memset(s + len, 0, rest - cl); pos -= cl; } - memcpy(s + pos, (cp ? cp : s), cl); + memmove(s + pos, (cp ? cp : s), cl); ptr->pos = pos; return Qnil; } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 5de2e03..2359da2 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -1011,6 +1011,24 @@ def test_ungetc_same_string assert_match(/\A(?:abc){30}bc(?:abc){6}\z/, s.string) end + def test_ungetc_same_string_past_end + s = StringIO.new("abc", "r+") + s.pos = 4 + s.ungetc(s.string) + assert_equal("aabc", s.string) + assert_equal(1, s.pos) + end + + def test_ungetc_same_string_after_reallocation + original = "abc" * 1024 + expected = original + original.byteslice(1..) + s = StringIO.new(original, "r+") + s.pos = 1 + s.ungetc(s.string) + assert_equal(expected, s.string) + assert_equal(0, s.pos) + end + def test_ungetbyte_pos b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011' s = StringIO.new( b )