From b97c05176a215c9bc1822bc00b96022a05f944af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hejl?= <hejl.lukas@gmail.com>
Date: Mon, 28 Mar 2022 12:56:56 +0200
Subject: [PATCH] Modified method Line::intersection_infinite() to return that
 the intersection was not found if the input lines are near parallel, and an
 integer overflow would occur when saving the intersection coordinates.

---
 src/libslic3r/Line.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/libslic3r/Line.cpp b/src/libslic3r/Line.cpp
index 3a180f747..68a7449c7 100644
--- a/src/libslic3r/Line.cpp
+++ b/src/libslic3r/Line.cpp
@@ -29,7 +29,14 @@ bool Line::intersection_infinite(const Line &other, Point* point) const
     if (std::fabs(denom) < EPSILON)
         return false;
     double t1 = cross2(v12, v2) / denom;
-    *point = (a1 + t1 * v1).cast<coord_t>();
+    Vec2d result = (a1 + t1 * v1);
+    if (result.x() > std::numeric_limits<coord_t>::max() || result.x() < std::numeric_limits<coord_t>::lowest() ||
+        result.y() > std::numeric_limits<coord_t>::max() || result.y() < std::numeric_limits<coord_t>::lowest()) {
+        // Intersection has at least one of the coordinates much bigger (or smaller) than coord_t maximum value (or minimum).
+        // So it can not be stored into the Point without integer overflows. That could mean that input lines are parallel or near parallel.
+        return false;
+    }
+    *point = (result).cast<coord_t>();
     return true;
 }