From 0281bfe05eb5ac178b25ad3389743884c503a707 Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <seth@kipro-pcb.com>
Date: Tue, 21 Jan 2025 19:57:29 -0800
Subject: [PATCH] Fix missing display pads on MacOS
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The distance from any point in the negative quadrant to
std::numeric_limits::max will be larger than std::numeric_limits::max,
meaning that if we take the square of this value, we will overflow the
extended_type.  Overflows are undefined behavior when dealing with
signed integers (c.f. C99 ยง3.4.3/1) so while most compilers retain a
positive value, Apple, in release mode, overflows into the sign bit,
resulting in a negative number for the squared distance.

This prevented us from displaying certain shapes in the negative
quadrant on MacOS, only in release mode builds

Fixes https://gitlab.com/kicad/code/kicad/-/issues/19424
---
 libs/kimath/src/geometry/vertex_set.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libs/kimath/src/geometry/vertex_set.cpp b/libs/kimath/src/geometry/vertex_set.cpp
index 5b8768f311..3b17a0f366 100644
--- a/libs/kimath/src/geometry/vertex_set.cpp
+++ b/libs/kimath/src/geometry/vertex_set.cpp
@@ -38,16 +38,18 @@ VERTEX* VERTEX_SET::createList( const SHAPE_LINE_CHAIN& points, VERTEX* aTail, v
         sum += ( ( p2.x - p1.x ) * ( p2.y + p1.y ) );
     }
 
-    VECTOR2I last_pt{ std::numeric_limits<int>::max(), std::numeric_limits<int>::max() };
+    VECTOR2L last_pt;
+    bool first = true;
 
     auto addVertex = [&]( int i )
     {
         const VECTOR2I& pt = points.CPoint( i );
-        VECTOR2I diff = pt - last_pt;
-        if( diff.SquaredEuclideanNorm() > m_simplificationLevel )
+
+        if( first || pt.SquaredDistance( last_pt ) > m_simplificationLevel )
         {
             tail = insertVertex( i, pt, tail, aUserData );
             last_pt = pt;
+            first = false;
         }
     };