From 7fe80abdff1e71fc620b9bb5dd0a420c74d3fcd1 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Fri, 11 Aug 2023 17:58:33 +0200 Subject: [PATCH] PCB_GRID_HELPER::BestSnapAnchor() fix overflow due to use of int. GRID_HELPER::GetVisibleGrid() needs double, does not work with int (overflow). Fixes #15389 https://gitlab.com/kicad/code/kicad/-/issues/15389 --- common/tool/grid_helper.cpp | 6 ++---- include/tool/grid_helper.h | 2 +- pcbnew/tools/pcb_grid_helper.cpp | 6 ++++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/common/tool/grid_helper.cpp b/common/tool/grid_helper.cpp index 46906c60bb..90677fabe8 100644 --- a/common/tool/grid_helper.cpp +++ b/common/tool/grid_helper.cpp @@ -56,11 +56,9 @@ VECTOR2I GRID_HELPER::GetGrid() const } -VECTOR2I GRID_HELPER::GetVisibleGrid() const +VECTOR2D GRID_HELPER::GetVisibleGrid() const { - VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetVisibleGridSize(); - - return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) ); + return m_toolMgr->GetView()->GetGAL()->GetVisibleGridSize(); } diff --git a/include/tool/grid_helper.h b/include/tool/grid_helper.h index 045b7360e6..313c025f4c 100644 --- a/include/tool/grid_helper.h +++ b/include/tool/grid_helper.h @@ -40,7 +40,7 @@ public: virtual ~GRID_HELPER(); VECTOR2I GetGrid() const; - VECTOR2I GetVisibleGrid() const; + VECTOR2D GetVisibleGrid() const; VECTOR2I GetOrigin() const; void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) ); diff --git a/pcbnew/tools/pcb_grid_helper.cpp b/pcbnew/tools/pcb_grid_helper.cpp index fb9dd230d6..9ffcb48aa6 100644 --- a/pcbnew/tools/pcb_grid_helper.cpp +++ b/pcbnew/tools/pcb_grid_helper.cpp @@ -286,8 +286,10 @@ VECTOR2I PCB_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& a // see https://gitlab.com/kicad/code/kicad/-/issues/5638 // see https://gitlab.com/kicad/code/kicad/-/issues/7125 // see https://gitlab.com/kicad/code/kicad/-/issues/12303 - int snapScale = KiROUND( snapSize / m_toolMgr->GetView()->GetGAL()->GetWorldScale() ); - int snapRange = m_enableGrid ? std::min( snapScale, GetVisibleGrid().x ) : snapScale; + double snapScale = snapSize / m_toolMgr->GetView()->GetGAL()->GetWorldScale(); + // warning: GetVisibleGrid().x sometimes returns a value > INT_MAX. Intermediate calculation + // needs double. + int snapRange = KiROUND( m_enableGrid ? std::min( snapScale, GetVisibleGrid().x ) : snapScale ); int snapDist = snapRange; //Respect limits of coordinates representation