]> OCCT Git - occt.git/commitdiff
0033560: PARASOLID Import - XT importer raises exception SIGFPE Arithmetic Exception IR-2024-03-29
authoroan <oan@opencascade.com>
Wed, 3 Jan 2024 02:39:35 +0000 (02:39 +0000)
committerVadim Glukhikh <vadim.glukhikh@opencascade.com>
Thu, 28 Mar 2024 13:58:36 +0000 (13:58 +0000)
Prevent division by zero in exceptional cases when vector of parameters contains only a single value.

src/Approx/Approx_BSplComputeLine.gxx
src/BSplCLib/BSplCLib_2.cxx

index 75315e89a8202889bd32384b58f31dc2152b57e2..36930088eabbf2fdb7c9232a49d661eece1929ad 100644 (file)
@@ -808,8 +808,11 @@ void Approx_BSplComputeLine::Parameters(const MultiLine& Line,
   const Standard_Integer aNbp = lastP - firstP + 1;
 
 
+  // The first parameter should always be zero according to all the logic below,
+  // so division by any value will give zero anyway, so it should never be scaled
+  // to avoid case when there is only one parameter in the array thus division by zero happens.
+  TheParameters(firstP) = 0.0;
   if (aNbp == 2) {
-    TheParameters(firstP) = 0.0;
     TheParameters(lastP) = 1.0;
   }
   else if (Par == Approx_ChordLength || Par == Approx_Centripetal)
@@ -820,7 +823,6 @@ void Approx_BSplComputeLine::Parameters(const MultiLine& Line,
     if (nbP3d == 0) mynbP3d = 1;
     if (nbP2d == 0) mynbP2d = 1;
 
-    TheParameters(firstP) = 0.0;
     dist = 0.0;
     TColgp_Array1OfPnt tabP(1, mynbP3d);
     TColgp_Array1OfPnt tabPP(1, mynbP3d);
@@ -861,10 +863,10 @@ void Approx_BSplComputeLine::Parameters(const MultiLine& Line,
         TheParameters(i) = TheParameters(i - 1) + Sqrt(dist);
       }
     }
-    for (i = firstP; i <= lastP; i++) TheParameters(i) /= TheParameters(lastP);
+    for (i = firstP + 1; i <= lastP; i++) TheParameters(i) /= TheParameters(lastP);
   }
   else {
-    for (i = firstP; i <= lastP; i++) {
+    for (i = firstP + 1; i <= lastP; i++) {
       TheParameters(i) = (Standard_Real(i) - firstP) /
         (Standard_Real(lastP - Standard_Real(firstP)));
     }
index 0ae8628e5c8d7b37777d659570cbb90e68390456..b6a4496af1c6136feb7a47a10ac66d599cdc20d3 100644 (file)
@@ -515,8 +515,13 @@ BSplCLib::EvalBsplineBasis
       //
       // this should be always invertible if ii is correctly computed 
       //
-      Factor = (Parameter - FlatKnots(ii - qq + pp + 1)) 
-       / (FlatKnots(ii + pp)   - FlatKnots(ii - qq + pp + 1)) ; 
+      const Standard_Real aScale = (FlatKnots(ii + pp) - FlatKnots(ii - qq + pp + 1));
+      if (Abs (aScale) < gp::Resolution())
+      {
+        return 2;
+      }
+
+      Factor = (Parameter - FlatKnots(ii - qq + pp + 1)) / aScale;
       Saved = Factor *    BsplineBasis(1,pp) ;
       BsplineBasis(1,pp) *= (1.0e0 - Factor) ;
       BsplineBasis(1,pp) += BsplineBasis(1,qq) ;
@@ -536,7 +541,13 @@ BSplCLib::EvalBsplineBasis
     }
 
     for (pp = 1 ; pp <= qq - 1 ; pp++) {
-      Inverse = 1.0e0 / (FlatKnots(ii + pp)  - FlatKnots(ii - qq + pp + 1)) ;
+      const Standard_Real aScale = (FlatKnots(ii + pp) - FlatKnots(ii - qq + pp + 1));
+      if (Abs (aScale) < gp::Resolution())
+      {
+        return 2;
+      }
+
+      Inverse = 1.0e0 / aScale;
       Factor  =  (Parameter - FlatKnots(ii - qq + pp + 1)) * Inverse ;
       Saved = Factor *                 BsplineBasis(1,pp) ;
       BsplineBasis(1,pp) *= (1.0e0 - Factor) ;