Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program terminates with uncaught exception if an invalid regex is used with RE2::Scanner#scan #52

Closed
serch opened this issue Mar 29, 2021 · 3 comments
Assignees
Labels

Comments

@serch
Copy link

serch commented Mar 29, 2021

If we compile an invalid regex and try to use it the program terminates:

irb(main):001:0> RE2::Regexp.new('???').scan("test").to_a
/tmp/re2-20210214-21445-1a81zn5/re2-2021-02-02/re2/re2.cc:205: Error parsing '???': no argument for repetition operator: ??
libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector

Ruby would instead raise:

irb(main):001:0> Regexp.new('???')
Traceback (most recent call last):
        3: from (irb):1
        2: from (irb):1:in `new'
        1: from (irb):1:in `initialize'
RegexpError (target of repeat operator is not specified: /???/)

I ran into this problem when I tried to compile some ruby-style regexes with RE2 and one had a negative look-behind: RE2::Regexp.new('(?<!foo)test').scan("test").to_a

@mudge
Copy link
Owner

mudge commented Mar 29, 2021

Hi @serch,

Thanks for reporting this, it looks like it is a problem with RE2::Scanner specifically when using an invalid re2 pattern, e.g. using the RE2::Regexp instance alone does not crash:

> pattern = RE2::Regexp.new('???')
re2/re2.cc:205: Error parsing '???': no argument for repetition operator: ??
=> #<RE2::Regexp /???/>
> pattern.ok?
=> false
> pattern.error
=> "no argument for repetition operator: ??"
> pattern.error_arg
=> "??"
> pattern.match('foo')
re2/re2.cc:624: Invalid RE2: no argument for repetition operator: ??
=> nil
> pattern =~ 'foo'
re2/re2.cc:624: Invalid RE2: no argument for repetition operator: ??
=> false

But as soon as we try to use RE2::Scanner#scan:

> scanner = pattern.scan('foo')
=> #<RE2::Scanner:0x00007fcebf175328>
> scanner.eof?
=> false
> scanner.rewind
=> #<RE2::Scanner:0x00007fcebf175328>
> scanner.scan
libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector
zsh: abort      pry -Ilib

Let me investigate this further and I'll keep you apprised.

@mudge mudge self-assigned this Mar 29, 2021
@mudge mudge added the bug label Mar 29, 2021
@mudge mudge changed the title Program terminates with uncaught exception if an invalid regex is used Program terminates with uncaught exception if an invalid regex is used with RE2::Scanner#scan Mar 29, 2021
mudge added a commit that referenced this issue Mar 29, 2021
GitHub: #52

As re2 will report the number of capturing groups for an invalid regular
expression as -1, we need to check whether a pattern is OK or not before
using the number to initialize any vectors, etc.

This was causing a crash when using RE2::Scanner#scan with an invalid
regular expression as we would attempt to initialize vectors with a
length of -1. Instead, set the number of capturing groups to 0 and rely
on re2's FindAndConsumeN returning false so we return nil to the user to
indicate no match was made.
@mudge
Copy link
Owner

mudge commented Mar 29, 2021

Hi @serch,

This should be fixed in https://github.com/mudge/re2/releases/tag/v1.4.0

Could you please try upgrading and let me know if that fixes your issue?

@serch
Copy link
Author

serch commented Mar 29, 2021

It does, thanks for the prompt reply and for the fix!

@serch serch closed this as completed Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants