From 2a552bc3dcbcff68ae9e8effd4400d735a9c26e2 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Fri, 21 Sep 2012 11:48:09 +0000
Subject: [PATCH] crash proof

---
 Geo/GModelIO_MSH.cpp |  5 +++--
 Geo/MElement.cpp     | 18 ++++++++++++++----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/Geo/GModelIO_MSH.cpp b/Geo/GModelIO_MSH.cpp
index e7913105cf..348bbb6235 100644
--- a/Geo/GModelIO_MSH.cpp
+++ b/Geo/GModelIO_MSH.cpp
@@ -248,8 +248,9 @@ int GModel::readMSH(const std::string &name)
             if(swap) SwapBytes((char*)&data[0], sizeof(int), numData);
           }
         }
-        MElementFactory factory;
-        MElement *element = factory.create(num, type, data, this);
+        MElementFactory f;
+        MElement *element = f.create(num, type, data, this);
+        if(!element) return 0;
         switch(element->getType()){
         case TYPE_PNT: elements[0][entity].push_back(element); break;
         case TYPE_LIN: elements[1][entity].push_back(element); break;
diff --git a/Geo/MElement.cpp b/Geo/MElement.cpp
index 18e6dc3b0d..e3b1972e08 100644
--- a/Geo/MElement.cpp
+++ b/Geo/MElement.cpp
@@ -1290,8 +1290,8 @@ MElement *MElement::copy(std::map<int, MVertex*> &vertexMap,
     parent = newParent;
   }
 
-  MElementFactory factory;
-  MElement *newEl = factory.create(eType, vmv, getNum(), _partition, ownsParent(), parent);
+  MElementFactory f;
+  MElement *newEl = f.create(eType, vmv, getNum(), _partition, ownsParent(), parent);
 
   for(int i = 0; i < 2; i++) {
     MElement *dom = getDomain(i);
@@ -1408,10 +1408,20 @@ MElement *MElementFactory::create(int num, int type, const std::vector<int> &dat
 
   std::vector<MVertex*> vertices(numVertices);
   if(data.size() > startVertices + numVertices - 1){
-    for(int i = 0; i < numVertices; i++)
-      vertices[i] = model->getMeshVertexByTag(data[startVertices + i]);
+    for(int i = 0; i < numVertices; i++){
+      int numVertex = data[startVertices + i];
+      MVertex *v = model->getMeshVertexByTag(numVertex);
+      if(v){
+        vertices[i] = v;
+      }
+      else{
+        Msg::Error("Unknown vertex %d in element %d", numVertex, num);
+        return 0;
+      }
+    }
   }
   else{
+    Msg::Error("Missing data in element %d", num);
     return 0;
   }
 
-- 
GitLab