diff --git a/string.c b/string.c index b7ae7069f6..75db0d22e8 100644 --- a/string.c +++ b/string.c @@ -8026,7 +8026,8 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) if (rsnewline) { pend = chomp_newline(subptr, pend, enc); } - else if (memcmp(pend - rslen, rsptr, rslen) == 0) { + else if (pend - subptr >= rslen && + memcmp(pend - rslen, rsptr, rslen) == 0) { pend -= rslen; } }