1.4.0: Fix crash when using RE2::Scanner#scan with an invalid regular expression
As reported by @serch in #52, fix a crash when using RE2::Scanner#scan
with an invalid regular expression.
This was caused by the underlying re2 library returning -1
as the number of capturing groups for an invalid regular expression and using that value to initialize memory when incrementally scanning with RE2::Scanner
. We now explicitly check an expression is valid and return nil
if it isn't.
> pattern = RE2::Regexp.new('???')
/tmp/re2-20210214-21445-1a81zn5/re2-2021-02-02/re2/re2.cc:205: Error parsing '???': no argument for repetition operator: ??
=> #<RE2::Regexp /???/>
> scanner = pattern.scan('Some text')
=> #<RE2::Scanner:0x00007f951d981840>
> scanner.scan
/tmp/re2-20210214-21445-1a81zn5/re2-2021-02-02/re2/re2.cc:890: Invalid RE2: no argument for repetition operator: ??
=> nil
> scanner.to_a
/tmp/re2-20210214-21445-1a81zn5/re2-2021-02-02/re2/re2.cc:890: Invalid RE2: no argument for repetition operator: ??
=> []
This also fixes an edge case when using RE2::Regexp#match
and specifying a negative number of matches which would previously raise a NoMemoryError
but will now raise a more informative ArgumentError
:
> RE2::Regexp.new('(\d+)').match('1 2 3', -1)
ArgumentError: number of matches should be >= 0
from (pry):5:in `match'