Actions
Bug #22635
closedString#byterindex can return an offset inside a character
Bug #22635:
String#byterindex can return an offset inside a character
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux]
Description
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 \:
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.
Actions