From 45ca0a5ab8998f3ace9008f058f57191e8dfc317 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Wed, 15 Jan 2020 18:33:19 +0000 Subject: [PATCH] Avoid divide-by-zero. --- bitmap2component/bitmap2cmp_gui.cpp | 6 ++++++ gerbview/am_primitive.cpp | 7 ++++++- libs/kimath/src/geometry/geometry_utils.cpp | 6 ++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/bitmap2component/bitmap2cmp_gui.cpp b/bitmap2component/bitmap2cmp_gui.cpp index 3c173371bb..75f87ac047 100644 --- a/bitmap2component/bitmap2cmp_gui.cpp +++ b/bitmap2component/bitmap2cmp_gui.cpp @@ -63,6 +63,9 @@ IMAGE_SIZE::IMAGE_SIZE() void IMAGE_SIZE::SetOutputSizeFromInitialImageSize() { + // Safety-check to guarantee no divide-by-zero + m_originalDPI = std::max( 1, m_originalDPI ); + // Set the m_outputSize value from the m_originalSizePixels and the selected unit if( m_unit == EDA_UNITS::MILLIMETRES ) { @@ -97,6 +100,9 @@ int IMAGE_SIZE::GetOutputDPI() outputDPI = KiROUND( m_outputSize ); } + // Zero is not a DPI, and may cause divide-by-zero errors... + outputDPI = std::max( 1, outputDPI ); + return outputDPI; } diff --git a/gerbview/am_primitive.cpp b/gerbview/am_primitive.cpp index 2518c0bb95..6d4c4588f2 100644 --- a/gerbview/am_primitive.cpp +++ b/gerbview/am_primitive.cpp @@ -556,7 +556,12 @@ void AM_PRIMITIVE::ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent, // params = center.x (unused here), center.y (unused here), outside diam, inside diam, crosshair thickness int outerRadius = scaletoIU( params[2].GetValue( tool ), m_GerbMetric ) / 2; int innerRadius = scaletoIU( params[3].GetValue( tool ), m_GerbMetric ) / 2; - int halfthickness = scaletoIU( params[4].GetValue( tool ), m_GerbMetric ) / 2; + + // Safety checks to guarantee no divide-by-zero + outerRadius = std::max( 1, outerRadius ); + innerRadius = std::max( 1, innerRadius ); + + int halfthickness = scaletoIU( params[4].GetValue( tool ), m_GerbMetric ) / 2; double angle_start = RAD2DECIDEG( asin( (double) halfthickness / innerRadius ) ); // Draw shape in the first cadrant (X and Y > 0) diff --git a/libs/kimath/src/geometry/geometry_utils.cpp b/libs/kimath/src/geometry/geometry_utils.cpp index 74625f28cc..e4898a637f 100644 --- a/libs/kimath/src/geometry/geometry_utils.cpp +++ b/libs/kimath/src/geometry/geometry_utils.cpp @@ -42,6 +42,9 @@ int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree ) // calculate the number of segments to approximate a circle by segments // given the max distance between the middle of a segment and the circle + // avoid divide-by-zero + aRadius = std::max( 1, aRadius ); + // error relative to the radius value: double rel_error = (double)aErrorMax / aRadius; // minimal arc increment in degrees: @@ -67,8 +70,7 @@ double GetCircletoPolyCorrectionFactor( int aSegCountforCircle ) * therefore, to move the middle of the segment to the circle (distance = radius) * the correctionFactor is 1 /cos( PI/aSegCountforCircle ) */ - if( aSegCountforCircle < MIN_SEGCOUNT_FOR_CIRCLE ) - aSegCountforCircle = MIN_SEGCOUNT_FOR_CIRCLE; + aSegCountforCircle = std::max( MIN_SEGCOUNT_FOR_CIRCLE, aSegCountforCircle ); return 1.0 / cos( M_PI / aSegCountforCircle ); }