From 69bf9b0a3e63e20579b10edb02bde8ed395f7251 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Sat, 1 Apr 2017 12:45:07 +0100
Subject: [PATCH] don't crash if old bezier curves (GEO) have too few control
 points

---
 Geo/GModelIO_GEO.cpp     |  4 ++++
 Geo/GeoInterpolation.cpp | 11 +++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/Geo/GModelIO_GEO.cpp b/Geo/GModelIO_GEO.cpp
index cb0a950fb0..78967d1961 100644
--- a/Geo/GModelIO_GEO.cpp
+++ b/Geo/GModelIO_GEO.cpp
@@ -258,6 +258,10 @@ bool GEO_Internals::addBezier(int num, const std::vector<int> &vertexTags)
     Msg::Error("GEO edge with tag %d already exists", num);
     return false;
   }
+  if(vertexTags.size() < 4){
+    Msg::Error("Bezier curve requires at least 4 control points");
+    return false;
+  }
   List_T *tmp = List_Create(2, 2, sizeof(int));
   for(unsigned int i = 0; i < vertexTags.size(); i++){
     int t = vertexTags[i];
diff --git a/Geo/GeoInterpolation.cpp b/Geo/GeoInterpolation.cpp
index 16f88b4893..598ce2e563 100644
--- a/Geo/GeoInterpolation.cpp
+++ b/Geo/GeoInterpolation.cpp
@@ -12,8 +12,6 @@
 
 #define SQU(a)      ((a)*(a))
 
-// Cubic spline :
-
 /*
 static void InterpolateBezier(Vertex *v[4], double t, Vertex &V)
 {
@@ -181,6 +179,11 @@ SPoint2 InterpolateCubicSpline(Vertex *v[4], double t, double mat[4][4],
 static Vertex InterpolateBezier(Curve *Curve, double u, int derivee)
 {
   int NbCurves = (List_Nbr(Curve->Control_Points) - 1) / 3;
+  if(NbCurves <= 0){
+    Msg::Error("Bezier curve requires at least 4 control points");
+    Vertex V;
+    return V;
+  }
   int iCurve = (int)floor(u * (double)NbCurves);
   if(iCurve >= NbCurves) iCurve = NbCurves - 1; // u = 1
   if(iCurve <= 0) iCurve = 0;
@@ -240,7 +243,7 @@ static Vertex InterpolateUBS(Curve *Curve, double u, int derivee)
     return V;
   }
   else
-    //    return InterpolateCubicSpline(v, t, Curve->mat, derivee, t1, t2);
+    // return InterpolateCubicSpline(v, t, Curve->mat, derivee, t1, t2);
     return InterpolateCubicSpline(v, t);
 }
 
@@ -557,7 +560,7 @@ static Vertex TransfiniteQua(Vertex c1, Vertex c2, Vertex c3, Vertex c4,
                      s1.Pos.Y, s2.Pos.Y, s3.Pos.Y, s4.Pos.Y, u, v);
   V.Pos.Z = TRAN_QUA(c1.Pos.Z, c2.Pos.Z, c3.Pos.Z, c4.Pos.Z,
                      s1.Pos.Z, s2.Pos.Z, s3.Pos.Z, s4.Pos.Z, u, v);
-  return (V);
+  return V;
 }
 
 // Transfinite interpolation on a triangle :
-- 
GitLab