Skip to content

Commit

Permalink
fix: generate regex only once else regex.assign can crash
Browse files Browse the repository at this point in the history
  • Loading branch information
elsamuko committed Jan 6, 2023
1 parent 8619935 commit 62cf1bd
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/searcher/searcherfactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@

namespace searcherfactory {

namespace {
std::once_flag rxInitialized;
}

std::function<Searcher*()> searcherFunc( SearchOptions& opts ) {

if( opts.isRegex ) {
return [&opts] {
rx::regex::flag_type flags = rx::regex::normal;

if( opts.ignoreCase ) { flags ^= rx::regex::icase; }

try {
opts.regex.assign( opts.term, flags );
} catch( const rx::regex_error& e ) {
LOG( "Invalid regex: " << e.what() );
exit( EXIT_FAILURE );
}
std::call_once( rxInitialized, [&opts] {
rx::regex::flag_type flags = rx::regex::normal;

if( opts.ignoreCase ) { flags ^= rx::regex::icase; }

try {
opts.regex.assign( opts.term, flags );
} catch( const rx::regex_error& e ) {
LOG( "Invalid regex: " << e.what() );
exit( EXIT_FAILURE );
}
} );

RegexSearcher* searcher = new RegexSearcher( opts );
return searcher;
Expand Down

0 comments on commit 62cf1bd

Please sign in to comment.