Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,8 @@ private IRubyObject getline(ThreadContext context, final IRubyObject rs, int lim
int w = 0;

if (limit > 0 && s + limit < e) {
e = getEncoding().rightAdjustCharHead(stringBytes, s, s + limit, e);
// a fixed-width encoding may step past a truncated last character
e = Math.min(e, getEncoding().rightAdjustCharHead(stringBytes, s, s + limit, e));
}
if (rs == context.nil) {
if (chomp) {
Expand Down
3 changes: 3 additions & 0 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,10 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
e = s + RSTRING_LEN(ptr->string);
s += ptr->pos;
if (limit > 0 && (size_t)limit < (size_t)(e - s)) {
const char *const end = e;
e = rb_enc_right_char_head(s, s + limit, e, get_enc(ptr));
/* a fixed-width encoding may step past a truncated last character */
if (e > end) e = end;
}
if (NIL_P(str)) {
if (arg->chomp) {
Expand Down
11 changes: 11 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ def test_gets_utf_16
assert_nil(stringio.gets)
end

def test_gets_limit_in_truncated_fixed_width_char
# 6 bytes: one UTF-32 code unit and a truncated one. The limit
# falls inside the truncated unit, and the line must stop at the
# end of the string, not beyond it.
stringio = StringIO.new("abcdef".b.freeze)
stringio.set_encoding(Encoding::UTF_32LE)
assert_equal("abcdef".b, stringio.gets(5).b)
assert_equal(6, stringio.pos)
assert_nil(stringio.gets(5))
end

def test_gets_chomp
assert_equal(nil, StringIO.new("").gets(chomp: true))
assert_equal("", StringIO.new("\n").gets(chomp: true))
Expand Down