Require regular expressions to be explicit.

Normally this means surrounded by forward slashes, but we also still
allow any regular expression in a netclass match to keep from breaking
existing documents.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/14944
This commit is contained in:
Jeff Young 2023-06-16 10:06:58 +01:00
parent 285f8a0e59
commit d8debfa0ee
3 changed files with 25 additions and 9 deletions

View File

@ -77,18 +77,34 @@ public:
bool EDA_PATTERN_MATCH_REGEX::SetPattern( const wxString& aPattern )
{
if( aPattern.StartsWith( "^" ) && aPattern.EndsWith( "$" ) )
{
m_pattern = aPattern;
}
else if( aPattern.StartsWith( "/" ) )
{
// Requiring a '/' on the end means they get no feedback while they type
m_pattern = aPattern.Mid( 1 );
if( m_pattern.EndsWith( "/" ) )
m_pattern = m_pattern.Left( m_pattern.length() - 1 );
}
else
{
// For now regular expressions must be explicit
return false;
}
// Evil and undocumented: wxRegEx::Compile calls wxLogError on error, even
// though it promises to just return false. Silence the error.
WX_LOGLEVEL_CONTEXT ctx( wxLOG_FatalError );
return m_regex.Compile( aPattern, wxRE_ADVANCED );
return m_regex.Compile( m_pattern, wxRE_ADVANCED );
}
bool EDA_PATTERN_MATCH_REGEX_EXPLICIT::SetPattern( const wxString& aPattern )
bool EDA_PATTERN_MATCH_REGEX_ANCHORED::SetPattern( const wxString& aPattern )
{
wxString pattern( aPattern );
@ -186,7 +202,7 @@ EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_WILDCARD::Find( const wxString&
}
bool EDA_PATTERN_MATCH_WILDCARD_EXPLICIT::SetPattern( const wxString& aPattern )
bool EDA_PATTERN_MATCH_WILDCARD_ANCHORED::SetPattern( const wxString& aPattern )
{
m_wildcard_pattern = aPattern;
@ -386,8 +402,8 @@ EDA_COMBINED_MATCHER::EDA_COMBINED_MATCHER( const wxString& aPattern,
break;
case CTX_NETCLASS:
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_EXPLICIT>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_EXPLICIT>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_ANCHORED>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
break;
case CTX_SIGNAL:

View File

@ -202,7 +202,7 @@ void FOOTPRINT_FILTER::FilterByFootprintFilters( const wxArrayString& aFilters )
for( const wxString& each_pattern : aFilters )
{
m_footprint_filters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_EXPLICIT>() );
m_footprint_filters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
m_footprint_filters.back()->SetPattern( each_pattern.Lower() );
}

View File

@ -132,7 +132,7 @@ protected:
};
class EDA_PATTERN_MATCH_REGEX_EXPLICIT : public EDA_PATTERN_MATCH_REGEX
class EDA_PATTERN_MATCH_REGEX_ANCHORED : public EDA_PATTERN_MATCH_REGEX
{
public:
virtual bool SetPattern( const wxString& aPattern ) override;
@ -151,7 +151,7 @@ protected:
};
class EDA_PATTERN_MATCH_WILDCARD_EXPLICIT : public EDA_PATTERN_MATCH_WILDCARD
class EDA_PATTERN_MATCH_WILDCARD_ANCHORED : public EDA_PATTERN_MATCH_WILDCARD
{
public:
virtual bool SetPattern( const wxString& aPattern ) override;