string.c: bound check mrb_str_byte_to_char before use - #7098
Merged
Conversation
The function formed `p + bi` before testing the offset, so a byte offset outside the string produced an out-of-bounds pointer, and the single-byte and binary fast path returned that offset instead of -1. Move the bounds test to the top, where it covers the fast path and the pointer alike. The non-UTF-8 identity function gets the same test, so the contract holds on every build: -1 when the offset lies outside the string, and on UTF-8 builds also when it lands inside a multi-byte character. Every caller clamps the offset it passes, so no behavior changes here. The check belongs in the function now that it is internal API rather than a static confined to this file.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesByte-to-character conversion bounds
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Aug 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #7097, which promoted
chars2bytes/bytes2charstomrb_str_char_to_byte/mrb_str_byte_to_char. This addresses a reviewcomment that arrived after the merge.
mrb_str_byte_to_charformedp + bibefore testing the offset, so a byteoffset outside the string produced an out-of-bounds pointer, and the
single-byte and binary fast path returned that offset unchanged instead of
-1. Moving the bounds test to the top covers the fast path and the pointer
arithmetic alike, and the
e < pivottest it replaces goes away.The non-UTF-8 identity function gets the same test, so the documented
contract holds on every build: -1 when the offset lies outside the string,
and on UTF-8 builds also when it lands inside a multi-byte character. Without
this, the same call would answer differently depending on
MRB_UTF8_STRING,which is the opposite of what promoting these functions was for.
Every caller clamps the offset it passes today (
str_rindexresults,str_index_str_by_char, andre_byte_to_charin mruby-regexp, which clampsto
RSTRING_LENbefore calling), so no behavior changes. The check belongsin the function now that it is internal API rather than a static confined to
string.c.Testing
rake testwithbuild_config/host-debug.rb(full-core,MRB_UTF8_STRING):mrbtest 2233 OK / 0 KO, bintest 116 OK.
rake testwith the default config (byte strings, identity conversion):mrbtest 2032 OK / 0 KO, bintest 105 OK.
Summary by CodeRabbit
Bug Fixes
Documentation