From 79ec93da4c460c476a716d2883d2d6c471a22937 Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Wed, 14 Aug 2024 15:43:09 -0600
Subject: [PATCH] Ensure that EDA_PATTERN_MATCH_RELATIONAL is thread-safe.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/18548
---
 common/eda_pattern_match.cpp | 8 ++++----
 include/eda_pattern_match.h  | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/common/eda_pattern_match.cpp b/common/eda_pattern_match.cpp
index 288a0b4491..cf04347efe 100644
--- a/common/eda_pattern_match.cpp
+++ b/common/eda_pattern_match.cpp
@@ -245,7 +245,7 @@ bool EDA_PATTERN_MATCH_WILDCARD_ANCHORED::SetPattern( const wxString& aPattern )
 
 bool EDA_PATTERN_MATCH_RELATIONAL::SetPattern( const wxString& aPattern )
 {
-    bool matches = m_regex_search.Matches( aPattern );
+    bool matches = m_regex_search.IsValid() && m_regex_search.Matches( aPattern );
 
     if( !matches || m_regex_search.GetMatchCount() < 5 )
         return false;
@@ -326,7 +326,7 @@ EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_RELATIONAL::Find( const wxStrin
 
 int EDA_PATTERN_MATCH_RELATIONAL::FindOne( const wxString& aCandidate ) const
 {
-    bool matches = m_regex_description.Matches( aCandidate );
+    bool matches = m_regex_description.IsValid() && m_regex_description.Matches( aCandidate );
 
     if( !matches )
         return EDA_PATTERN_NOT_FOUND;
@@ -365,9 +365,9 @@ int EDA_PATTERN_MATCH_RELATIONAL::FindOne( const wxString& aCandidate ) const
 }
 
 
-wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_description(
+const wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_description(
         R"((\w+)[=:]([-+]?[\d.]+)(\w*))", wxRE_ADVANCED );
-wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search(
+const wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search(
         R"(^(\w+)(<|<=|=|>=|>)([-+]?[\d.]*)(\w*)$)", wxRE_ADVANCED );
 const std::map<wxString, double> EDA_PATTERN_MATCH_RELATIONAL::m_units = {
     { wxS( "p" ),  1e-12 },
diff --git a/include/eda_pattern_match.h b/include/eda_pattern_match.h
index 09e9b4d678..50eaa33cc8 100644
--- a/include/eda_pattern_match.h
+++ b/include/eda_pattern_match.h
@@ -192,8 +192,8 @@ protected:
     RELATION m_relation;
     double   m_value;
 
-    static wxRegEx m_regex_description;
-    static wxRegEx m_regex_search;
+    static const wxRegEx m_regex_description;
+    static const wxRegEx m_regex_search;
     static const std::map<wxString, double> m_units;
 };