Skip to content

Don't match from the middle of a character - #219

Merged
kou merged 1 commit into
ruby:masterfrom
kou:fix-scan-non-character-boundary
Sep 24, 2026
Merged

kou merged 1 commit into
ruby:masterfrom
kou:fix-scan-non-character-boundary

Conversation

@kou

@kou kou commented Sep 20, 2026

Copy link
Copy Markdown
Member

#pos= accepts an arbitrary byte offset, so the scan position can fall in the middle of a multi-byte character such as UTF-16BE. Matching from there made Onigmo read a whole character starting at that position, reading past the end of the string.

On CRuby this is an out-of-bounds read.

On JRuby a search such as #scan_until could loop without terminating.

On TruffleRuby it aborts with an internal error while building the matched substring. But we don't change the TruffleRuby implementation because we don't have a fast implementation for this.

Refuse to match unless the current position is the head of a character.

Copilot AI lite review requested due to automatic review settings September 20, 2026 11:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kou
kou force-pushed the fix-scan-non-character-boundary branch from e874771 to 685585a Compare September 20, 2026 21:55
Copilot AI review requested due to automatic review settings September 20, 2026 21:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread lib/strscan/truffleruby.rb Outdated
return true if pos == 0 || pos == @string.bytesize
return true if @string.bytesize == @string.length
begin
@string.b.byteslice(0, pos).force_encoding(@string.encoding).valid_encoding?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eregon This implementation is slow. Is there any better way to detect character boundary in TruffleRuby?

@eregon eregon Sep 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we validate on pos= instead maybe? That would avoid adding overhead on the fast path.

This seems indeed very slow, and I think between the existing exception and this expensive check, the existing exception is better, because users are very unlikely to hit it.
So I think no changes on TruffleRuby + skipping that test is better.

The exception is fatal though as we can see here, that's something we should fix in TruffleRuby to be some Ruby exception instead: truffleruby/truffleruby#4474

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(FWIW there is Primitive.string_is_character_head? but that's a private Primitive so it cannot be used outside TruffleRuby's core library.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move the validation to pos=, we can't avoid other cases such as get_byte.

I'll not change the TruffleRuby implementation in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think not changing TruffleRuby is the best trade-off in this PR.
I'll consider exposing Primitive.string_is_character_head? as a public Primitive for the next release.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Could you work on it as a follow-up PR?
I'll merge this PR as-is.

@eregon

eregon commented Sep 21, 2026

Copy link
Copy Markdown
Member

Returning nil might be confusing for the user as nil means "no match".
Maybe an exception would be better?
If we can move this validation to pos= I think that'd be even better.

@kou

kou commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

Returning nil might be confusing for the user as nil means "no match". Maybe an exception would be better?

This case (match from the middle of a character) must be a "no match" case. This is a fast path for this case. (This also avoids false-positive match by s = StringScanner.new("ab".encode("UTF-16BE"); s.pos = 1; s.scan("\x61\x00".force_encoding("UTF-16BE")).)

`#pos=` accepts an arbitrary byte offset, so the scan position can fall
in the middle of a multi-byte character such as UTF-16BE. Matching from
there made the regexp engine read a whole character starting at that
position, reading past the end of the string.

On CRuby this is an out-of-bounds read.

On JRuby a search such as `#scan_until` could loop without
terminating.

On TruffleRuby it aborts with an internal error while building the
matched substring. But we don't change the TruffleRuby implementation
because we don't have a fast implementation for this.

Refuse to match unless the current position is the head of a
character.
@kou
kou force-pushed the fix-scan-non-character-boundary branch from 685585a to e85effe Compare September 22, 2026 07:45
Copilot AI review requested due to automatic review settings September 22, 2026 07:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kou
kou merged commit a552ea6 into ruby:master Sep 24, 2026
43 checks passed
@kou
kou deleted the fix-scan-non-character-boundary branch September 24, 2026 00:06
matzbot pushed a commit to ruby/ruby that referenced this pull request Sep 24, 2026
(ruby/strscan#219)

`#pos=` accepts an arbitrary byte offset, so the scan position can fall
in the middle of a multi-byte character such as UTF-16BE. Matching from
there made Onigmo read a whole character starting at that position,
reading past the end of the string.

On CRuby this is an out-of-bounds read.

On JRuby a search such as `#scan_until` could loop without terminating.

On TruffleRuby it aborts with an internal error while building the
matched substring. But we don't change the TruffleRuby implementation
because we don't have a fast implementation for this.

Refuse to match unless the current position is the head of a character.

ruby/strscan@a552ea6f49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants