From d8debfa0eed7f318858e589091a79b086d0ce129 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 16 Jun 2023 10:06:58 +0100 Subject: [PATCH] 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 --- common/eda_pattern_match.cpp | 28 ++++++++++++++++++++++------ common/footprint_filter.cpp | 2 +- include/eda_pattern_match.h | 4 ++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/common/eda_pattern_match.cpp b/common/eda_pattern_match.cpp index a48ac48f1f..2f6603212f 100644 --- a/common/eda_pattern_match.cpp +++ b/common/eda_pattern_match.cpp @@ -78,17 +78,33 @@ public: bool EDA_PATTERN_MATCH_REGEX::SetPattern( const wxString& aPattern ) { - m_pattern = 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() ); - AddMatcher( aPattern, std::make_unique() ); + AddMatcher( aPattern, std::make_unique() ); + AddMatcher( aPattern, std::make_unique() ); break; case CTX_SIGNAL: diff --git a/common/footprint_filter.cpp b/common/footprint_filter.cpp index f2bcfe2328..dcca5e98a8 100644 --- a/common/footprint_filter.cpp +++ b/common/footprint_filter.cpp @@ -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() ); + m_footprint_filters.push_back( std::make_unique() ); m_footprint_filters.back()->SetPattern( each_pattern.Lower() ); } diff --git a/include/eda_pattern_match.h b/include/eda_pattern_match.h index 693642e739..5b1ad91be6 100644 --- a/include/eda_pattern_match.h +++ b/include/eda_pattern_match.h @@ -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;