Project

General

Profile

Actions

Bug #20421

closed

String#index and String#byteindex don't clear `$~` when offset > size (or bytesize)

Added by andrykonchin (Andrew Konchin) 28 days ago. Updated about 15 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:117494]

Description

When String#index and String#byteindex are called with Regexp and offset they clear $~ (that contains result of the last Regexp matching) when offset < -size. But they don't clear $~ when offset > size what seems inconsistent:

"a".index(/a/)
p $~ # => #<MatchData "a">

string = "blablabla"
string.index(/bla/, string.length + 1)
p $~ # => #<MatchData "a">
Actions #1

Updated by andrykonchin (Andrew Konchin) 28 days ago

  • Description updated (diff)

Updated by jeremyevans0 (Jeremy Evans) 2 days ago

  • Status changed from Open to Closed
  • Backport changed from 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN to 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: DONTNEED

The issue doesn't affect 3.3 and master. It appears fixed by 9dcdffb8bf8a3654fd78bf1a58b30c8e13888a7a. If you would like the fix backported to 3.2, please respond.

Updated by Eregon (Benoit Daloze) 2 days ago

  • Backport changed from 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: DONTNEED to 3.0: REQUIRED, 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONTNEED

Given this is a clear bug (which can break programs' logic) I think it's good to backport.

Updated by Eregon (Benoit Daloze) 2 days ago

We should also add a test so this doesn't regress on 3.3 & master, that seems fine as a ruby_bug spec in ruby/spec.

Actions #5

Updated by hsbt (Hiroshi SHIBATA) 2 days ago

  • Backport changed from 3.0: REQUIRED, 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONTNEED to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONTNEED

Updated by byroot (Jean Boussier) about 15 hours ago

I got a ruby-bug spec at https://github.com/ruby/ruby/pull/10750

As for 9dcdffb8bf8a3654fd78bf1a58b30c8e13888a7a, it might make sense to make a more targeted fix for backport, as this is a more general refactor.

Updated by byroot (Jean Boussier) about 15 hours ago

Here's more targeted patch for Ruby 3.2: https://github.com/ruby/ruby/pull/10751

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0