Skip to content

Commit 773d140

Browse files
committed
[Bug #20787] Check the separator in IO#readline as well as 3.2
1 parent ec526c6 commit 773d140

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

io.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4396,23 +4396,31 @@ rb_io_set_lineno(VALUE io, VALUE lineno)
43964396
static VALUE
43974397
io_readline(rb_execution_context_t *ec, VALUE io, VALUE sep, VALUE lim, VALUE chomp)
43984398
{
4399+
long limit = -1;
43994400
if (NIL_P(lim)) {
4401+
VALUE tmp = Qnil;
44004402
// If sep is specified, but it's not a string and not nil, then assume
44014403
// it's the limit (it should be an integer)
4402-
if (!NIL_P(sep) && NIL_P(rb_check_string_type(sep))) {
4404+
if (!NIL_P(sep) && NIL_P(tmp = rb_check_string_type(sep))) {
44034405
// If the user has specified a non-nil / non-string value
44044406
// for the separator, we assume it's the limit and set the
44054407
// separator to default: rb_rs.
44064408
lim = sep;
4409+
limit = NUM2LONG(lim);
44074410
sep = rb_rs;
44084411
}
4412+
else {
4413+
sep = tmp;
4414+
}
44094415
}
4410-
4411-
if (!NIL_P(sep)) {
4412-
StringValue(sep);
4416+
else {
4417+
if (!NIL_P(sep)) StringValue(sep);
4418+
limit = NUM2LONG(lim);
44134419
}
44144420

4415-
VALUE line = rb_io_getline_1(sep, NIL_P(lim) ? -1L : NUM2LONG(lim), RTEST(chomp), io);
4421+
check_getline_args(&sep, &limit, io);
4422+
4423+
VALUE line = rb_io_getline_1(sep, limit, RTEST(chomp), io);
44164424
rb_lastline_set_up(line, 1);
44174425

44184426
if (NIL_P(line)) {

test/ruby/test_io.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,14 @@ def test_readline_chomp_true
20022002
end
20032003
end
20042004

2005+
def test_readline_incompatible_rs
2006+
first_line = File.open(__FILE__, &:gets).encode("utf-32le")
2007+
File.open(__FILE__, encoding: "utf-8:utf-32le") {|f|
2008+
assert_equal first_line, f.readline
2009+
assert_raise(ArgumentError) {f.readline("\0")}
2010+
}
2011+
end
2012+
20052013
def test_set_lineno_readline
20062014
pipe(proc do |w|
20072015
w.puts "foo"

0 commit comments

Comments
 (0)