From: "nobu (Nobuyoshi Nakada) via ruby-core" Date: 2026-11-25T07:26:09+00:00 Subject: [ruby-core:127707] [Ruby Bug#22635] String#byterindex can return an offset inside a character Issue #22635 has been updated by nobu (Nobuyoshi Nakada). Doesn't **byte** mean it never considers the encoding information? ---------------------------------------- Bug #22635: String#byterindex can return an offset inside a character https://bugs.ruby-lang.org/issues/22635#change-120221 * Author: jhawthorn (John Hawthorn) * Status: Open * ruby -v: ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: REQUIRED, 4.0: REQUIRED ---------------------------------------- `String#byterindex` can return the offset of a match that starts in the middle of a character. In Shift_JIS the second byte of `���` is `0x5C`, the same as `\`: ```ruby s = "C:\\data\\���".encode("Shift_JIS") s.byterindex("\\") # => 9, expected 7 ``` `byterindex` clamps its starting offset to `bytesize - sub.bytesize`, which can land inside a character, and `str_rindex` compares the substring there with `memcmp` without checking that it's a character boundary. Every later candidate is checked with `rb_enc_left_char_head`. This is broken on Ruby 3.4 and 4.0. Ruby 3.2 and 3.3 and are a bit more confusing because there were different branches per-OS. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/