From bf8ec61c91115a8535fa45e1575eb6464bb5dc14 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Sun, 19 Apr 2015 12:18:38 +0000
Subject: [PATCH] coverity fixes

---
 Common/onelab.h       |  8 ++++++--
 Geo/Geo.cpp           | 12 ++++++++----
 Geo/gmshEdge.cpp      |  2 +-
 Post/shapeFunctions.h |  2 +-
 4 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/Common/onelab.h b/Common/onelab.h
index 8a9dc5cfbe..1b9c24a57d 100644
--- a/Common/onelab.h
+++ b/Common/onelab.h
@@ -979,13 +979,17 @@ namespace onelab{
                                    std::set<T*, parameterLessThan> &ps)
     {
       if(name.empty() && client.size()){
+        std::vector<T*> toDelete;
         for(typename std::set<T*, parameterLessThan>::iterator it = ps.begin();
-            it != ps.end(); it++){
+            it != ps.end(); ){
           T *p = *it;
           if(p->hasClient(client)){
-            ps.erase(it);
+            ps.erase(it++); // to avoid invalid iterator
             delete p;
           }
+          else{
+            it++;
+          }
         }
       }
       else{
diff --git a/Geo/Geo.cpp b/Geo/Geo.cpp
index 75be98c0d2..e8e35786c3 100644
--- a/Geo/Geo.cpp
+++ b/Geo/Geo.cpp
@@ -2534,7 +2534,8 @@ static void ReplaceDuplicatePoints(std::map<int, int> * v_report = 0)
         std::map<int, int>::iterator m_it = v_report->find(v->Num);
         if(m_it != v_report->end()){
 	  Vertex **v_rep = (Vertex **)Tree_PQuery(allNonDuplicatedPoints, &v);
-	  m_it->second = (*v_rep)->Num;
+          if(v_rep)
+            m_it->second = (*v_rep)->Num;
         }
       }
     }
@@ -2711,12 +2712,14 @@ static void ReplaceDuplicateCurves(std::map<int, int> * c_report = 0)
 	  std::map<int, int>::iterator m_it = c_report->find(c->Num);
 	  if(m_it != c_report->end()){
 	    Curve **c_rep = (Curve **)Tree_PQuery(allNonDuplicatedCurves, &c);
-	    m_it->second = (*c_rep)->Num;
+            if(c_rep)
+              m_it->second = (*c_rep)->Num;
 	  }
 	  m_it = c_report->find(c2->Num);
 	  if(m_it != c_report->end()){
 	    Curve **c_rep_neg = (Curve **)Tree_PQuery(allNonDuplicatedCurves, &c2);
-	    m_it->second = (*c_rep_neg)->Num;
+            if(c_rep_neg)
+              m_it->second = (*c_rep_neg)->Num;
 	  }
 	}
       }
@@ -3005,7 +3008,8 @@ static void ReplaceDuplicateSurfaces(std::map<int, int> *s_report = 0)
 	  std::map<int, int>::iterator m_it = (*s_report).find(s->Num);
 	  if(m_it != s_report->end()){
 	    Surface **s_rep = (Surface **)Tree_PQuery(allNonDuplicatedSurfaces, &s);
-	    m_it->second = (*s_rep)->Num;
+            if(s_rep)
+              m_it->second = (*s_rep)->Num;
 	  }
 	}
       }
diff --git a/Geo/gmshEdge.cpp b/Geo/gmshEdge.cpp
index 0881787645..40aadcadc2 100644
--- a/Geo/gmshEdge.cpp
+++ b/Geo/gmshEdge.cpp
@@ -407,7 +407,7 @@ void gmshEdge::writeGEO(FILE *fp)
       fprintf(fp, ", %d", v->Num);
     else
       fprintf(fp, "{%d", v->Num);
-    if(i % 6 == 7)
+    if(i % 8 == 7)
       fprintf(fp, "\n");
   }
   fprintf(fp, "};\n");
diff --git a/Post/shapeFunctions.h b/Post/shapeFunctions.h
index 6a2a94aeac..d17286c8cd 100644
--- a/Post/shapeFunctions.h
+++ b/Post/shapeFunctions.h
@@ -912,7 +912,7 @@ public:
   }
   void getGradShapeFunction(int num, double u, double v, double w, double s[3])
   {
-    if(w == 1. && num != 4) {
+    if(w == 1.) {
       switch(num) {
       case 0 : s[0] = -0.25 ;
                s[1] = -0.25 ;
-- 
GitLab