string.c: check the append size before mrb_str_cat() modifies - #7038
Merged
Conversation
`mrb_str_cat()` takes `len` as a `size_t` and hands it to `mrb_int_add_overflow()`, whose parameters are `mrb_int`. A `len` above `MRB_INT_MAX` is therefore converted before the check can see it, and the conversion yields a negative value on the builds mruby targets: the addition does not overflow, `total` comes out below the current length, the capacity branch is skipped, and `memcpy()` still runs with the original `size_t` count and writes past the buffer. Nothing inside mruby reaches this. Every internal caller passes a length that came from a string, and `str_check_length()` keeps those below `MRB_INT_MAX`. An extension calling the public `mrb_str_cat()` with a length it computed itself can. Reject a `len` that does not fit before it is converted. The whole size check moves ahead of `mrb_str_modify()` as well, so a string that is about to raise is no longer unshared first.
📝 WalkthroughWalkthrough
ChangesString append validation
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 9, 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.
Summary
mrb_str_cat()takeslenas asize_tand hands it tomrb_int_add_overflow(), whoseparameters are
mrb_int. AlenaboveMRB_INT_MAXis therefore converted before thecheck can see it, and the conversion yields a negative value on the builds mruby targets.
The addition then does not overflow,
totalcomes out below the current length, thecapacity branch is skipped, and
memcpy()still runs with the originalsize_tcount,writing past the buffer.
Nothing inside mruby reaches this. Every internal caller passes a length that came from a
string, and
str_check_length()keeps those belowMRB_INT_MAX. An extension calling thepublic
mrb_str_cat()with a length it computed itself can, and a length arriving fromoutside is exactly the case a size check is there for.
The change
Reject a
lenthat does not fit before it is converted:The comparison is correct in both directions of the type mismatch. Where
size_tisnarrower than
mrb_int,MRB_INT_MAXis all ones in its low bits, so the conversionlands on
SIZE_MAXand the condition is never true. That is the right answer there, sinceno
size_tcan exceedMRB_INT_MAXon such a build in the first place.The whole size check moves ahead of
mrb_str_modify()as well.RSTR_LEN(s)is the samebefore and after that call, so the check reads the same value, and a string that is about
to raise is no longer unshared first.
Relation to #7039 and #7043
Both touch the same handful of lines at the top of
mrb_str_cat(). #7039 carries thiscommit as its first of two, since it depends on the check being in place. #7043 is
independent of both. Whichever lands first, I will rebase the others onto it.
Testing
rake testwithbuild_config/default.rb,MRUBY_CONFIG=asan rake test(
address,undefined), andMRUBY_CONFIG=ci/gcc-clang rake test, which coversMRB_GC_STRESSwithMRB_USE_DEBUG_HOOK,MRB_GC_FIXED_ARENA, and the C++ ABI build.KO: 0andCrash: 0on all of them.