Project

General

Profile

Actions

Bug #22635

closed

String#byterindex can return an offset inside a character

Bug #22635: String#byterindex can return an offset inside a character

Added by jhawthorn (John Hawthorn) about 21 hours ago. Updated about 4 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux]
[ruby-core:127705]

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 \:

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.

Actions

Also available in: PDF Atom