Skip to content

Commit 2330146

Browse files
committed
merge revision(s) 773d140: [Backport #20787]
[Bug #20787] Check the separator in `IO#readline` as well as 3.2
1 parent 732ee20 commit 2330146

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

io.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,23 +4372,31 @@ rb_io_set_lineno(VALUE io, VALUE lineno)
43724372
static VALUE
43734373
io_readline(rb_execution_context_t *ec, VALUE io, VALUE sep, VALUE lim, VALUE chomp)
43744374
{
4375+
long limit = -1;
43754376
if (NIL_P(lim)) {
4377+
VALUE tmp = Qnil;
43764378
// If sep is specified, but it's not a string and not nil, then assume
43774379
// it's the limit (it should be an integer)
4378-
if (!NIL_P(sep) && NIL_P(rb_check_string_type(sep))) {
4380+
if (!NIL_P(sep) && NIL_P(tmp = rb_check_string_type(sep))) {
43794381
// If the user has specified a non-nil / non-string value
43804382
// for the separator, we assume it's the limit and set the
43814383
// separator to default: rb_rs.
43824384
lim = sep;
4385+
limit = NUM2LONG(lim);
43834386
sep = rb_rs;
43844387
}
4388+
else {
4389+
sep = tmp;
4390+
}
43854391
}
4386-
4387-
if (!NIL_P(sep)) {
4388-
StringValue(sep);
4392+
else {
4393+
if (!NIL_P(sep)) StringValue(sep);
4394+
limit = NUM2LONG(lim);
43894395
}
43904396

4391-
VALUE line = rb_io_getline_1(sep, NIL_P(lim) ? -1L : NUM2LONG(lim), RTEST(chomp), io);
4397+
check_getline_args(&sep, &limit, io);
4398+
4399+
VALUE line = rb_io_getline_1(sep, limit, RTEST(chomp), io);
43924400
rb_lastline_set_up(line, 1);
43934401

43944402
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"

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
1212
#define RUBY_VERSION_TEENY 6
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 108
14+
#define RUBY_PATCHLEVEL 109
1515

1616
#include "ruby/version.h"
1717
#include "ruby/internal/abi.h"

0 commit comments

Comments
 (0)