From ea520da1c8295ee8c700c020f075981c81a1b04e Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <seth@kipro-pcb.com>
Date: Mon, 24 Feb 2025 10:12:37 -0800
Subject: [PATCH] Protect search from mis-attributed LIB/SCH instance

The parent of fields might be a LIB_SYMBOL, not SCH_SYMBOL.  We need to
watch for and avoid these in the search/replace for schematics

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20079
---
 eeschema/sch_field.cpp | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/eeschema/sch_field.cpp b/eeschema/sch_field.cpp
index 4c6de90687..af694161f5 100644
--- a/eeschema/sch_field.cpp
+++ b/eeschema/sch_field.cpp
@@ -657,8 +657,13 @@ bool SCH_FIELD::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) co
         if( searchAndReplace && !replaceReferences )
             return false;
 
-        SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
-        wxASSERT( parentSymbol );
+        SCH_SYMBOL* parentSymbol = dyn_cast<SCH_SYMBOL*>( m_parent );
+
+        // The parent might be a LIB_SYMBOL, in which case, we don't
+        // have a sheet path to resolve the reference.
+        if( !parentSymbol )
+            return false;
+
         wxASSERT( aAuxData );
 
         // Take sheet path into account which effects the reference field and the unit for