From 6fd14ac945f4931cc8954d6c2ee661d67cb61b15 Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@craftyjon.com>
Date: Tue, 31 May 2022 20:18:16 -0400
Subject: [PATCH] [PNS] Fix invalid assumption in IsLineCorner

(cherry picked from commit a4aa447b95a4a21567ba3ef5200e33c4c2dd3186)
---
 pcbnew/router/pns_joint.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/pcbnew/router/pns_joint.h b/pcbnew/router/pns_joint.h
index d8dad464d2..fdcb3015ac 100644
--- a/pcbnew/router/pns_joint.h
+++ b/pcbnew/router/pns_joint.h
@@ -112,11 +112,20 @@ public:
                         && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2
                         && m_linkedItems.Count( VIA_T ) == 1 )
             {
-                assert( static_cast<const ITEM*>( m_linkedItems[2] )->Kind() == VIA_T );
+                const VIA* via = nullptr;
 
-                const VIA* via = static_cast<const VIA*>( m_linkedItems[2] );
+                for( const ITEM* item : m_linkedItems.CItems() )
+                {
+                    if( item->Kind() == VIA_T )
+                    {
+                        via = static_cast<const VIA*>( item );
+                        break;
+                    }
+                }
 
-                if( !via->IsVirtual() )
+                assert( via );
+
+                if( !via || !via->IsVirtual() )
                     return false;
             }
             else