mruby-sprintf: protect format string from mutation during callbacks#6781
Merged
Conversation
mrb_str_format captured raw C pointers (p, end) into the format string's buffer before the main loop. The %s and %p specifiers call to_s and inspect, which can invoke Ruby code that mutates the format string via String#replace, freeing or reallocating its buffer. The loop then continued iterating with dangling pointers, reading freed memory and potentially leaking adjacent heap contents into the result. Duplicate the format string with mrb_str_dup() before the loop. This is O(1) because mrb_str_dup shares the underlying buffer; if the original is later mutated via String#replace, str_replace decrements the shared refcount, leaving our duplicate's buffer intact. Co-authored-by: Claude <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Code Review
This pull request addresses a potential memory safety issue in mrb_str_format where callbacks could mutate the format string and invalidate internal pointers. It introduces a string duplication step to ensure the format string remains stable during processing and adds a corresponding test case to verify the fix. I have no feedback to provide.
matz
added a commit
that referenced
this pull request
Apr 12, 2026
dearblue
added a commit
to dearblue/mruby
that referenced
this pull request
Apr 13, 2026
`mrb_str_dup()` always duplicates string objects in an unfrozen state, and the class is also set. Therefore, it can be observed and modified from the Ruby side using the `ObjectSpace.each_object` method. By using `mrb_str_dup_frozen()`, unnecessary duplication can be avoided, and modifications to the string can also be prevented.
dearblue
added a commit
to dearblue/mruby
that referenced
this pull request
Apr 13, 2026
`mrb_str_dup()` always duplicates string objects in an unfrozen state, and the class is also set. Therefore, it can be observed and modified from the Ruby side using the `ObjectSpace.each_object` method. By using `mrb_str_dup_frozen()`, unnecessary duplication can be avoided, and modifications to the string can also be prevented.
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_formatcaptured raw C pointers (p,end) into the format string's buffer before the main loop. The%sand%pspecifiers callto_sandinspect, which can invoke Ruby code that mutates the format string viaString#replace, freeing or reallocating its buffer. The loop then continued iterating with dangling pointers, reading freed memory and potentially leaking adjacent heap contents into the result string.Fix
Duplicate the format string with
mrb_str_dup()before the loop. This is O(1) becausemrb_str_dupshares the underlying buffer; if the original is later mutated viaString#replace,str_replacedecrements the shared refcount, leaving our duplicate's buffer intact.Test plan
rake CONFIG=host-debug all test:run:serial— 1810 lib + 100 bintest OKto_scallback calling$fmt.replace("Z")no longer corrupts sprintf output