Skip to content

Add missing shrunk check in integer_at - #212

Merged
kou merged 1 commit into
ruby:masterfrom
kou:integer-at-shrink
Jul 27, 2026
Merged

kou merged 1 commit into
ruby:masterfrom
kou:integer-at-shrink

Conversation

@kou

@kou kou commented Jul 27, 2026

Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings July 27, 2026 03:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kou
kou force-pushed the integer-at-shrink branch from 8fd3aeb to dee7b92 Compare July 27, 2026 06:18
Copilot AI review requested due to automatic review settings July 27, 2026 06:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kou
kou merged commit 3032e38 into ruby:master Jul 27, 2026
36 of 43 checks passed
@kou
kou deleted the integer-at-shrink branch July 27, 2026 06:47
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants