aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--string.c2
-rw-r--r--test/ruby/test_string.rb12
2 files changed, 13 insertions, 1 deletions
diff --git a/string.c b/string.c
index 8515773a84..fd17355346 100644
--- a/string.c
+++ b/string.c
@@ -7409,7 +7409,7 @@ chomp_newline(const char *p, const char *e, rb_encoding *enc)
if (rb_enc_is_newline(prev, e, enc)) {
e = prev;
prev = rb_enc_prev_char(p, e, e, enc);
- if (rb_enc_ascget(prev, e, NULL, enc) == '\r')
+ if (prev && rb_enc_ascget(prev, e, NULL, enc) == '\r')
e = prev;
}
return e;
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 39477be220..d2fbd00a1d 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -843,6 +843,18 @@ CODE
assert_equal "hello", S("hello\nworld").each_line(chomp: true).next
assert_equal "hello\nworld", S("hello\nworld").each_line(nil, chomp: true).next
+
+ res = []
+ S("").each_line(chomp: true) {|x| res << x}
+ assert_equal([], res)
+
+ res = []
+ S("\n").each_line(chomp: true) {|x| res << x}
+ assert_equal([S("")], res)
+
+ res = []
+ S("\r\n").each_line(chomp: true) {|x| res << x}
+ assert_equal([S("")], res)
end
def test_lines