ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "nagachika (Tomoyuki Chikanaga)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109837] [Ruby master Bug#18631] Range check breaks multiplex backreferences in regular expressions
Date: Sun, 04 Sep 2022 07:25:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-99074.20220904072538.46524@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18631.20220314154405.46524@ruby-lang.org

Issue #18631 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 2.7: REQUIRED, 3.0: REQUIRED, 3.1: REQUIRED to 2.7: REQUIRED, 3.0: REQUIRED, 3.1: DONE

ruby_3_1 6b8bf6ba5d1893b3c933c2de3ada1eb59a94b644 merged revision(s) 6d3f447aecfb56f7d3edbdf9cc68e748e150d7d8.

----------------------------------------
Bug #18631: Range check breaks multiplex backreferences in regular expressions
https://bugs.ruby-lang.org/issues/18631#change-99074

* Author: jirkamarsik (Jirka Marsik)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 2.7.5p203 (2021-11-24) [x86_64-linux]
* Backport: 2.7: REQUIRED, 3.0: REQUIRED, 3.1: DONE
----------------------------------------
The regular expression engine can sometimes produce wrong results when using multiplex backreferences near the end of the input string.

``` ruby
irb(main):001:0> /(?<x>a)(?<x>aa)\k<x>/.match("aaaaa")
=> #<MatchData "aaaaa" x:"a" x:"aa">
irb(main):002:0> /(?<x>a)(?<x>aa)\k<x>/.match("aaaa")
=> nil
irb(main):003:0> /(?<x>a)(?<x>aa)\k<x>/.match("aaaab")
=> #<MatchData "aaaa" x:"a" x:"aa">
```

The second and third calls to `match` should produce the same result.

The cause is the `DATA_ENSURE(n)` macro in the `OP_BACKREF_MULTI` case in `regexec.c` (https://github.com/ruby/ruby/blob/master/regexec.c#L2646). Instead of using `continue` to try to match the other referents for the backref (as all the other branches do), the `DATA_ENSURE` macro uses `goto fail` and so skips the other referents of the multiplex backref. This means that after failing the range check, no other referent can match. By extending the input string in the third example above, we have avoided the bug and got the correct result.



-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2022-09-04  7:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-14 15:44 [ruby-core:107892] [Ruby master Bug#18631] Range check breaks multiplex backreferences in regular expressions jirkamarsik (Jirka Marsik)
2022-03-24 20:27 ` [ruby-core:108057] " jeremyevans0 (Jeremy Evans)
2022-09-04  7:25 ` nagachika (Tomoyuki Chikanaga) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-99074.20220904072538.46524@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).