Project

General

Profile

Actions

Bug #22718

open

String#each_line with a broken separator returns lines with a stale valid coderange

Bug #22718: String#each_line with a broken separator returns lines with a stale valid coderange

Added by saddestmartian (sad mart) 1 day ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-12-03T23:00:19Z master bcd243e8e8) +PRISM [x86_64-linux]
[ruby-core:127846]

Description

String#each_line and String#lines with a broken separator return lines that report valid_encoding? == true although they are not valid:

s = "あい"; s.valid_encoding?
s.lines("\xE3")                         #=> ["\xE3", "\x81\x82\xE3", "\x81\x84"]
s.lines("\xE3").map(&:valid_encoding?)  #=> [true, true, true], expected [false, false, false]

The end of a separator match is not a character boundary when the separator is broken, and rb_str_subseq copies the receiver's coderange (callers are expected to cut on boundaries, as in Bug #22700).

The pull request below clears the coderange of the lines in that case and leaves the lines themselves unchanged. Whether a broken separator should match at all (chomp/index treat it as no match, split raises) is left open.

It reproduces on master and 4.0.10.

https://github.com/ruby/ruby/pull/20475

No data to display

Actions

Also available in: PDF Atom