mruby-string-ext: fix String#prepend with self-referencing arguments#6780
Merged
Conversation
String#prepend(s, s) read RSTRING_LEN(argv[i]) in the copy loop after
mrb_str_resize had already updated the receiver's length, causing the
memcpy to write past the allocated buffer.
Detect self-references with mrb_obj_eq() and read from the memmoved
original data at p + total_prepend_len using the captured self_len.
This also handles mixed cases like s.prepend("X", s) where earlier
writes would otherwise corrupt the source of later reads.
Co-authored-by: Claude <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Code Review
This pull request fixes a bug in String#prepend where self-referencing arguments (e.g., s.prepend(s)) could lead to incorrect data being copied because the original string's content was being overwritten during the prepend operation. The fix ensures that if an argument is the string itself, the source pointer is adjusted to the new location of the original data. New test cases have been added to verify this behavior. I have no feedback to provide.
matz
added a commit
that referenced
this pull request
Apr 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.
Summary
String#prepend(s, s)readRSTRING_LEN(argv[i])in the copy loop aftermrb_str_resizehad already updated the receiver's length, causingmemcpyto write past the allocated buffer. Introduced in2cd8eb386, only on master/stable (not yet released).Detect self-references with
mrb_obj_eq()and read from the memmoved original data atp + total_prepend_lenusing the capturedself_len. This also handles mixed cases likes.prepend("X", s)where earlier writes would otherwise corrupt the source of later reads.Test plan
rake CONFIG=host-debug all test:run:serial— 1809 lib + 100 bintest OKs.prepend(s, s),s.prepend("XYZ", s), ands.prepend(s, "X", s)