Skip to content

Commit 29ec638

Browse files
authored
Regular expressions does not crash anymore (cont) (#490)
* enhanced type safety on regexp implementation * fixed memory leak in b2::regex::compiler::regcomp * fixed crashing of bad regexps, due to attempt to reference a compiled regexp, which has not been compiled due to some error, by b2::regex::program::result_iterator::advance * added the b2::regex::frame variable for formatting errors when using regexps (especially compilation) and program termination (optional). * b2::regex::program::result_iterator::operator[] bug on boundary check fixed * added b2::regex::program::result_iterator::count method, for better regex usability * better cleanup on abs_workdir.py test * alternate includes scheme to aid linking * partially revert of regex include scheme
1 parent d82f426 commit 29ec638

File tree

5 files changed

+140
-64
lines changed

5 files changed

+140
-64
lines changed

doc/src/history.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* *New*: Added test for regular expressions with MATCH builtin rule.
1010
-- _Paolo Pastori_
1111

12+
* Fix various regexp issues, allowing for mandatory regular expression
13+
syntax checking, detailed error messages, and improved MATCH rule.
14+
-- _Paolo Pastori_
15+
1216
== Version 5.4.2
1317

1418
Fix detection of Visual Studio 2026 to account for non-native tools being

src/engine/builtins.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,20 +1018,23 @@ LIST * builtin_match( FRAME * frame, int flags )
10181018
b2::list_cref patterns( lol_get( frame->args, 0 ) );
10191019
for ( auto pattern : patterns )
10201020
{
1021-
b2::regex::program re( pattern->str() );
1021+
b2::regex::program re;
1022+
{
1023+
// compilation errors print a nice error message and exit
1024+
b2::regex::frame_ctx ctx(frame);
1025+
re.reset( pattern->str() );
1026+
}
10221027

10231028
/* For each text string to match against. */
10241029
b2::list_cref texts( lol_get( frame->args, 1 ) );
10251030
for ( auto text : texts )
10261031
{
10271032
if ( auto re_i = re.search( text->str() ) )
10281033
{
1029-
/* Find highest parameter */
1030-
int top = NSUBEXP-1;
1031-
while ( !re_i[top].begin() ) top -= 1;
1032-
/* And add all parameters up to highest onto list. */
1033-
/* Must have parameters to have results! */
1034-
for ( int i = 1; i <= top ; ++i )
1034+
/* Find total groups matched */
1035+
int tot = re_i.count();
1036+
/* And add all catched matches onto result list. */
1037+
for ( int i = 1; i <= tot ; ++i )
10351038
{
10361039
string_append_range( buf, re_i[i].begin(), re_i[i].end() );
10371040
result.push_back( object_new( buf->value ) );

0 commit comments

Comments
 (0)