]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Protection against invalid size in expression evaluation.
authorageay <ageay>
Wed, 11 Jul 2012 12:38:21 +0000 (12:38 +0000)
committerageay <ageay>
Wed, 11 Jul 2012 12:38:21 +0000 (12:38 +0000)
src/INTERP_KERNEL/ExprEval/InterpKernelExprParser.cxx
src/INTERP_KERNEL/ExprEval/InterpKernelExprParser.hxx
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/Test/MEDCouplingBasicsTest3.cxx
src/MEDCoupling/Test/MEDCouplingBasicsTest4.cxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py

index a8867850ccfec61ab465639621334efd1cd03992..69dabca7a6b747c4409751f5b1571303feb84358 100644 (file)
@@ -92,7 +92,7 @@ void LeafExprVar::fillValue(Value *val) const throw(INTERP_KERNEL::Exception)
   val->setVarname(_fast_pos,_var_name);
 }
 
-void LeafExprVar::prepareExprEvaluation(const std::vector<std::string>& vars) const throw(INTERP_KERNEL::Exception)
+void LeafExprVar::prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception)
 {
   std::vector<std::string>::const_iterator iter=std::find(vars.begin(),vars.end(),_var_name);
   if(iter==vars.end())
@@ -103,9 +103,25 @@ void LeafExprVar::prepareExprEvaluation(const std::vector<std::string>& vars) co
           std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss,", "));
           throw INTERP_KERNEL::Exception(oss.str().c_str());
         }
-      return;
+      else
+        {
+          int relPos=-7-_fast_pos;
+          if(relPos>=targetNbOfCompo)
+            {
+              std::ostringstream oss; oss << "LeafExprVar::prepareExprEvaluation : Found recognized unitary vector \"" << _var_name << "\" which implies that component #" << relPos;
+              oss << " exists, but it is not the case component id should be in [0," << targetNbOfCompo << ") !";
+              throw INTERP_KERNEL::Exception(oss.str().c_str());
+            }
+          else
+            return;
+        }
     }
   _fast_pos=(int)std::distance(vars.begin(),iter);
+  if(_fast_pos>=nbOfCompo)
+    {
+      std::ostringstream oss; oss << "LeafExprVar::prepareExprEvaluation : Found var \"" << _var_name << "\" on place " << _fast_pos << " whereas only must be in [0," << nbOfCompo << ") !";
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
 }
 
 void LeafExprVar::prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception)
@@ -275,17 +291,17 @@ void ExprParser::evaluateExpr(int szOfOutParam, const double *inParam, double *o
   delete res;
 }
 
-void ExprParser::prepareExprEvaluation(const std::vector<std::string>& vars) const throw(INTERP_KERNEL::Exception)
+void ExprParser::prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception)
 {
   if(_leaf)
     {
       LeafExprVar *leafC=dynamic_cast<LeafExprVar *>(_leaf);
       if(leafC)
-        leafC->prepareExprEvaluation(vars);
+        leafC->prepareExprEvaluation(vars,nbOfCompo,targetNbOfCompo);
     }
   else
     for(std::list<ExprParser>::const_iterator iter=_sub_expr.begin();iter!=_sub_expr.end();iter++)
-      (*iter).prepareExprEvaluation(vars);
+      (*iter).prepareExprEvaluation(vars,nbOfCompo,targetNbOfCompo);
 }
 
 void ExprParser::prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception)
index 0d01bad10eb7b5a5d8acf95b66ce8f1301d45fbd..a87f335a7337ae403685ff2643a6ba9c5df60d20 100644 (file)
@@ -67,7 +67,7 @@ namespace INTERP_KERNEL
     void compileX86_64(std::vector<std::string>& ass) const;
     void fillValue(Value *val) const throw(INTERP_KERNEL::Exception);
     std::string getVar() const { return _var_name; }
-    void prepareExprEvaluation(const std::vector<std::string>& vars) const throw(INTERP_KERNEL::Exception);
+    void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception);
     void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
     void replaceValues(const std::vector<double>& valuesInExpr) throw(INTERP_KERNEL::Exception);
     static bool isRecognizedKeyVar(const std::string& var, int& pos);
@@ -88,7 +88,7 @@ namespace INTERP_KERNEL
     bool isParsingSuccessfull() const { return _is_parsing_ok; }
     double evaluate() const throw(INTERP_KERNEL::Exception);
     DecompositionInUnitBase evaluateUnit() const throw(INTERP_KERNEL::Exception);
-    void prepareExprEvaluation(const std::vector<std::string>& vars) const throw(INTERP_KERNEL::Exception);
+    void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const throw(INTERP_KERNEL::Exception);
     void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const throw(INTERP_KERNEL::Exception);
     void prepareExprEvaluationVec() const throw(INTERP_KERNEL::Exception);
     void getSetOfVars(std::set<std::string>& vars) const;
index 0eae4e12b710f34dde464d76a65b72b1fa8996e0..8dae06d62622f7caf2339faec19867e3e1ae4d78 100644 (file)
@@ -2135,7 +2135,7 @@ DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, const char *func) cons
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
   std::vector<std::string> varsV(vars.begin(),vars.end());
-  expr.prepareExprEvaluation(varsV);
+  expr.prepareExprEvaluation(varsV,oldNbOfComp,nbOfComp);
   //
   DataArrayDouble *newArr=DataArrayDouble::New();
   int nbOfTuples=getNumberOfTuples();
@@ -2210,7 +2210,7 @@ DataArrayDouble *DataArrayDouble::applyFunc2(int nbOfComp, const char *func) con
       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
-  expr.prepareExprEvaluation(getVarsOnComponent());
+  expr.prepareExprEvaluation(getVarsOnComponent(),oldNbOfComp,nbOfComp);
   //
   DataArrayDouble *newArr=DataArrayDouble::New();
   int nbOfTuples=getNumberOfTuples();
@@ -2254,7 +2254,7 @@ DataArrayDouble *DataArrayDouble::applyFunc3(int nbOfComp, const std::vector<std
       std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
-  expr.prepareExprEvaluation(varsOrder);
+  expr.prepareExprEvaluation(varsOrder,oldNbOfComp,nbOfComp);
   //
   DataArrayDouble *newArr=DataArrayDouble::New();
   int nbOfTuples=getNumberOfTuples();
index a9d8b8759d75243642e7773e121f0adc143b90d7..a02421060a758c9aa7c3ddf0b805f2fe19c1ab64 100644 (file)
@@ -1391,6 +1391,7 @@ void MEDCouplingBasicsTest3::testExtrudedMesh5()
   e->setCoordsAt(0,d);
   MEDCouplingUMesh *f=e->buildUnstructured();
   DataArrayDouble *g=f->getCoords()->applyFunc(2,"3.5*IVec+x/6*3.14159265359*JVec");
+  CPPUNIT_ASSERT_THROW(f->getCoords()->applyFunc(2,"3.5*IVec+x/6*3.14159265359*KVec"),INTERP_KERNEL::Exception); // KVec refers to component #2 and there is only 2 components !
   DataArrayDouble *h=g->fromPolarToCart();
   f->setCoords(h);
   MEDCouplingUMesh *i=c->buildExtrudedMesh(f,1);
index 4c5256e548cd1e3a9e81b1ebdce761492fc9eff3..c867a699adb7653206cf8228af2e63fd593f0980 100644 (file)
@@ -378,6 +378,8 @@ void MEDCouplingBasicsTest4::testApplyFuncThree1()
   for(int i=0;i<5;i++)
     CPPUNIT_ASSERT_DOUBLES_EQUAL(expected1[i],da2->getIJ(0,i),1e-12);
   da2->decrRef();
+  std::vector<std::string> vs2(4); vs2[0]="x"; vs2[1]="y"; vs2[2]="z"; vs2[3]="a";
+  CPPUNIT_ASSERT_THROW(da->applyFunc3(1,vs2,"x+a"),INTERP_KERNEL::Exception);
   f1->setArray(da);
   CPPUNIT_ASSERT_EQUAL(3,f1->getNumberOfComponents());
   CPPUNIT_ASSERT_EQUAL(5,f1->getNumberOfTuples());
index 5fa0833a8b7bd4dca6e938c6c3cec1fcdf6a218f..79f5adb37ee2b13c33aa0ff5507af62132872ca5 100644 (file)
@@ -5027,6 +5027,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         e.setCoordsAt(0,d);
         f=e.buildUnstructured();
         g=f.getCoords().applyFunc(2,"3.5*IVec+x/6*3.14159265359*JVec");
+        self.assertRaises(InterpKernelException,f.getCoords().applyFunc,2,"3.5*IVec+x/6*3.14159265359*KVec"); # KVec refers to component #2 and there is only 2 components !
         h=g.fromPolarToCart();
         f.setCoords(h);
         i=c.buildExtrudedMesh(f,1);
@@ -6892,6 +6893,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         for i in xrange(5):
             self.assertAlmostEqual(expected1[i],da2.getIJ(0,i),12);
             pass
+        self.assertRaises(InterpKernelException, da.applyFunc3, 1, ["x","y","z","a"],"x+a")
         f1.setArray(da);
         self.assertEqual(3,f1.getNumberOfComponents());
         self.assertEqual(5,f1.getNumberOfTuples());