diff --git i/ext/stringio/stringio.c w/ext/stringio/stringio.c index 75f8cd7..7edf711 100644 --- i/ext/stringio/stringio.c +++ w/ext/stringio/stringio.c @@ -954,7 +954,7 @@ strio_getline(int argc, VALUE *argv, struct StringIO *ptr) e = s + RSTRING_LEN(ptr->string); s += ptr->pos; if (limit > 0 && s + limit < e) { - e = s + limit; + e = rb_enc_right_char_head(s, s + limit, e, rb_enc_get(ptr->string)); } if (NIL_P(str)) { str = strio_substr(ptr, ptr->pos, e - s); diff --git i/test/stringio/test_stringio.rb w/test/stringio/test_stringio.rb index 30284fa..fee9abd 100644 --- i/test/stringio/test_stringio.rb +++ w/test/stringio/test_stringio.rb @@ -378,6 +378,11 @@ class TestStringIO < Test::Unit::TestCase assert_equal("a" * 10000 + "zz", f.gets("zz")) f = StringIO.new("a" * 10000 + "zz!") assert_equal("a" * 10000 + "zz!", f.gets("zzz")) + + ["a".encode("utf-16be"), "\u3042"].each do |s| + assert_equal(s, StringIO.new(s).gets(1)) + assert_equal(s, StringIO.new(s).gets(nil, 1)) + end end def test_each