← Back to team overview

kicad-developers team mailing list archive

[PATCH] pcbnew crashes when loading file with bad net ID

 

If you try to load a kicad_pcb into pcbnew that refers to an invalid net 
ID, pcbnew crashes after failing an assertion. It should display an 
error indicating that the file is corrupt instead. I've changed 
BOARD_CONNECTED_ITEM::SetNetCode() to be able to indicate whether the 
code was valid, rather than just asserting, and then changed 
PCB_PARSER::parseTRACK() to check for this and complain using 
Expecting().

I'd rather have used an exception to do this, but we don't have any 
exception types that look suitable. Thoughts?

Here's a board file that causes this: 
http://misc.c4757p.com/2VB701E.kicad_pcb

--
Chris
diff --git a/pcbnew/class_board_connected_item.cpp b/pcbnew/class_board_connected_item.cpp
index 797e2ee..82a2153 100644
--- a/pcbnew/class_board_connected_item.cpp
+++ b/pcbnew/class_board_connected_item.cpp
@@ -48,7 +48,7 @@ BOARD_CONNECTED_ITEM::BOARD_CONNECTED_ITEM( const BOARD_CONNECTED_ITEM& aItem )
 }
 
 
-void BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode )
+bool BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode, bool aNoAssert )
 {
     // if aNetCode < 0 ( typically NETINFO_LIST::FORCE_ORPHANED )
     // or no parent board,
@@ -61,7 +61,9 @@ void BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode )
     else
         m_netinfo = &NETINFO_LIST::ORPHANED;
 
-    assert( m_netinfo );
+    if( !aNoAssert )
+        assert( m_netinfo );
+    return m_netinfo;
 }
 
 
diff --git a/pcbnew/class_board_connected_item.h b/pcbnew/class_board_connected_item.h
index 15dfe31..fea4554 100644
--- a/pcbnew/class_board_connected_item.h
+++ b/pcbnew/class_board_connected_item.h
@@ -104,9 +104,11 @@ public:
      * Function SetNetCode
      * sets net using a net code.
      * @param aNetCode is a net code for the new net. It has to exist in NETINFO_LIST held by BOARD.
+     * @param aNoAssert if true, do not assert that the net exists.
      * Otherwise, item is assigned to the unconnected net.
+     * @return true on success, false if the net did not exist
      */
-    void SetNetCode( int aNetCode );
+    bool SetNetCode( int aNetCode, bool aNoAssert=false );
 
     /**
      * Function GetSubNet
diff --git a/pcbnew/pcb_parser.cpp b/pcbnew/pcb_parser.cpp
index c9a2e42..a7ede89 100644
--- a/pcbnew/pcb_parser.cpp
+++ b/pcbnew/pcb_parser.cpp
@@ -2408,7 +2408,8 @@ TRACK* PCB_PARSER::parseTRACK() throw( IO_ERROR, PARSE_ERROR )
             break;
 
         case T_net:
-            track->SetNetCode( getNetCode( parseInt( "net number" ) ) );
+            if( ! track->SetNetCode( getNetCode( parseInt( "net number" ) ), /* aNoAssert */ true ) )
+                Expecting( "valid net ID" );
             break;
 
         case T_tstamp:

Follow ups