mudge / re2

Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python".
http://mudge.name/re2/
BSD 3-Clause "New" or "Revised" License
130 stars 13 forks source link

Fix crash when using RE2::Scanner#scan with an invalid regular expression #53

Closed mudge closed 3 years ago

mudge commented 3 years ago

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.

Following that, ensure we always check a pattern is OK before using its number of capturing groups as a positive integer.

This also fixes an edge case in RE2::Regexp#match when a user passes a negative number for the number of intended matches. Previously, this would throw a misleading NoMemoryError, now it throws a more informative ArgumentError.

Take this opportunity to document the behaviour of the library when given invalid regular expressions in the specs.