← Back to team overview

kicad-developers team mailing list archive

[PATCH] Crash with zero size pads in module editor

 

Currently, the module editor crashes if the pad size is set to zero (e.g.,
while backspacing the current value to replace it). Note that this also
happens if the entry is cleared as that is also treated as zero. Basically,
the previewer tries to set the drawing context scale to zero and the
drawing library doesn't like this.

Also, the editor accepts zero as a pad size for surface mount pads.

The attached patch adds checks for the zero entry case and sets the scale
to (what I think is) a sensible default -- it uses the drill size rather
than the pad size, or if the drill size is also zero, it arbitrarily uses a
size of 1mm. It also adds an error message for zero-size pads when checking
if the dialog is valid before accepting it.

This closes lp:1378917.

Regards,
Blair
=== modified file 'pcbnew/dialogs/dialog_pad_properties.cpp'
--- pcbnew/dialogs/dialog_pad_properties.cpp	2014-09-10 15:18:42 +0000
+++ pcbnew/dialogs/dialog_pad_properties.cpp	2014-11-19 03:41:19 +0000
@@ -247,12 +247,39 @@
     // Calculate a suitable scale to fit the available draw area
     int dim = m_dummyPad->GetSize().x + std::abs( m_dummyPad->GetDelta().y);
 
+    // Invalid x size. User could enter zero, or have deleted all text prior to
+    // entering a new value; this is also treated as zero. If dim is left at
+    // zero, the drawing scale is zero and we get a crash.
+    if( dim == 0 )
+    {
+        // If drill size has been set, use that. Otherwise default to 1mm.
+        dim = m_dummyPad->GetDrillSize().x;
+        if( dim == 0 )
+            dim = 1000000;
+    }
+
     if( m_dummyPad->GetLocalClearance() > 0 )
         dim += m_dummyPad->GetLocalClearance() * 2;
 
     double scale = (double) dc_size.x / dim;
 
-    dim = m_dummyPad->GetSize().y + std::abs( m_dummyPad->GetDelta().x);
+    // If the pad is a circle, use the x size here instead.
+    int ysize;
+    if( m_dummyPad->GetShape() == PAD_CIRCLE )
+        ysize = m_dummyPad->GetSize().x;
+    else
+        ysize = m_dummyPad->GetSize().y;
+        
+    dim = ysize + std::abs( m_dummyPad->GetDelta().x );
+
+    // Invalid y size. See note about x size above.
+    if( dim == 0 )
+    {
+        dim = m_dummyPad->GetDrillSize().y;
+        if( dim == 0 )
+            dim = 1000000;
+    }
+
     if( m_dummyPad->GetLocalClearance() > 0 )
         dim += m_dummyPad->GetLocalClearance() * 2;
 
@@ -710,6 +737,12 @@
     wxString msg;
 
     // Test for incorrect values
+    if( (m_dummyPad->GetSize().x <= 0) ||
+       ((m_dummyPad->GetSize().y <= 0) && (m_dummyPad->GetShape() != PAD_CIRCLE)) )
+    {
+        error_msgs.Add( _("Pad size must be greater than zero" ) );
+    }
+
     if( (m_dummyPad->GetSize().x < m_dummyPad->GetDrillSize().x) ||
         (m_dummyPad->GetSize().y < m_dummyPad->GetDrillSize().y) )
     {


Follow ups