From fa7debf49d0f8f850a64fb2a5a4cf71810612a3c Mon Sep 17 00:00:00 2001
From: Vojtech Bubnik <bubnikv@gmail.com>
Date: Tue, 2 Aug 2022 13:22:33 +0200
Subject: [PATCH] Clipper: Verify range of int32 coordinates on input.

---
 src/clipper/clipper.cpp | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/clipper/clipper.cpp b/src/clipper/clipper.cpp
index 84109398a..6fde80906 100644
--- a/src/clipper/clipper.cpp
+++ b/src/clipper/clipper.cpp
@@ -602,9 +602,18 @@ bool HorzSegmentsOverlap(cInt seg1a, cInt seg1b, cInt seg2a, cInt seg2b)
 // ClipperBase class methods ...
 //------------------------------------------------------------------------------
 
-#ifndef CLIPPERLIB_INT32
+#ifdef CLIPPERLIB_INT32
+static inline void RangeTest(const IntPoint &pt)
+{
+#ifndef NDEBUG
+    static constexpr const int32_t hi = 65536 * 16383;
+    if (pt.x() > hi || pt.y() > hi || -pt.x() > hi || -pt.y() > hi) 
+      throw clipperException("Coordinate outside allowed range");
+#endif // NDEBUG
+}
+#else // CLIPPERLIB_INT32
 // Called from ClipperBase::AddPath() to verify the scale of the input polygon coordinates.
-inline void RangeTest(const IntPoint& Pt, bool& useFullRange)
+static inline void RangeTest(const IntPoint& Pt, bool& useFullRange)
 {
   if (useFullRange)
   {
@@ -798,7 +807,10 @@ bool ClipperBase::AddPathInternal(const Path &pg, int highI, PolyType PolyTyp, b
   try
   {
     edges[1].Curr = pg[1];
-#ifndef CLIPPERLIB_INT32
+#ifdef CLIPPERLIB_INT32
+    RangeTest(pg[0]);
+    RangeTest(pg[highI]);
+#else
     RangeTest(pg[0], m_UseFullRange);
     RangeTest(pg[highI], m_UseFullRange);
 #endif // CLIPPERLIB_INT32
@@ -806,7 +818,9 @@ bool ClipperBase::AddPathInternal(const Path &pg, int highI, PolyType PolyTyp, b
     InitEdge(&edges[highI], &edges[0], &edges[highI-1], pg[highI]);
     for (int i = highI - 1; i >= 1; --i)
     {
-#ifndef CLIPPERLIB_INT32
+#ifdef CLIPPERLIB_INT32
+      RangeTest(pg[i]);
+#else
       RangeTest(pg[i], m_UseFullRange);
 #endif // CLIPPERLIB_INT32
       InitEdge(&edges[i], &edges[i+1], &edges[i-1], pg[i]);