Add missing shrunk check in integer_at - #212
Merged
Merged
Conversation
matzbot
pushed a commit
to ruby/ruby
that referenced
this pull request
Jul 27, 2026
youdie006
added a commit
to youdie006/strscan
that referenced
this pull request
Sep 14, 2026
matched_size returned the raw register width, so after the stored
string was shrunk under the scanner it disagreed with matched:
s = StringScanner.new(+"before 29 after")
s.skip_until(" ")
s.scan(/\d+/) #=> "29"
s.string.replace("before ")
s.matched #=> ""
s.matched_size #=> 2, should be 0
matched answers correctly because extract_range clamps to the current
length of the string. integer_at was given the same two lines in ruby#212,
charpos in 7b77f30, and bol? carries its own guard. matched_size was
missed in all three implementations.
Clamp it the same way, and return nil once the match start is past the
end of the string, which is what extract_range does.
kou
added a commit
that referenced
this pull request
Sep 14, 2026
`matched_size` returns the raw register width without clamping it to the
current length of the stored string, so shrinking the string leaves it
reporting the old match:
```ruby
s = StringScanner.new("abcdef")
s.scan(/abcdef/)
s.string = "abc"
s.matched_size # => 6, should be 3
s.matched # => "abc" (this one is already correct)
```
`extract_range` (`ext/strscan/strscan.c:168-169`) already clamps, which
is why `matched` answers correctly. #212 added the same two lines to
`integer_at`, `charpos` got them in `7b77f30`, and `bol?` carries its
own guard — `matched_size` was missed.
Fixed in all three backends (CRuby, JRuby, TruffleRuby) with a shared
regression test.
---------
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
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.
No description provided.