Salome HOME
Protect applyLin against errors in input (bug PAL1164, CEA22584)
authorgeay <anthony.geay@cea.fr>
Wed, 14 May 2014 08:29:17 +0000 (10:29 +0200)
committergeay <anthony.geay@cea.fr>
Wed, 14 May 2014 08:29:17 +0000 (10:29 +0200)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py

index 86b8c8727683571b6d8e0b529e625f29e57ad0a6..c54ef2cec1678423d63a669b42e60acc88106a30 100644 (file)
@@ -4113,14 +4113,18 @@ DataArrayDouble *DataArrayDouble::computeAbs() const
  *  \param [in] a - the first coefficient of the function.
  *  \param [in] b - the second coefficient of the function.
  *  \param [in] compoId - the index of component to modify.
- *  \throw If \a this is not allocated.
+ *  \throw If \a this is not allocated, or \a compoId is not in [0,\c this->getNumberOfComponents() ).
  */
 void DataArrayDouble::applyLin(double a, double b, int compoId)
 {
   checkAllocated();
-  double *ptr=getPointer()+compoId;
-  int nbOfComp=getNumberOfComponents();
-  int nbOfTuple=getNumberOfTuples();
+  double *ptr(getPointer()+compoId);
+  int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
+  if(compoId<0 || compoId>=nbOfComp)
+    {
+      std::ostringstream oss; oss << "DataArrayDouble::applyLin : The compoId requested (" << compoId << ") is not valid ! Must be in [0," << nbOfComp << ") !";
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
   for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
     *ptr=a*(*ptr)+b;
   declareAsNew();
index 9d834779bbfcf9b0f0ab933487922b0624728348..20b8b5848003d9941aa8d1c73af9063a14f75537 100644 (file)
@@ -14977,6 +14977,24 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(3,amr.getMaxNumberOfLevelsRelativeToThis())
         self.assertEqual(2,amr.getSpaceDimension())
         pass
+
+    def testSwig2NonRegressionTestPAL1164(self):
+        """ Test PAL1164 Protection of applyLin against error in compoId ( #CEA22584 ) """
+        xarr=DataArrayDouble(3,1)
+        xarr.iota(0.)
+        cmesh=MEDCouplingCMesh()
+        cmesh.setCoords(xarr,xarr,xarr)
+        mesh=cmesh.buildUnstructured()
+        f=mesh.fillFromAnalytic(ON_CELLS,1,"(x-5.)*(x-5.)+(y-5.)*(y-5.)+(z-5.)*(z-5.)")
+        f.setName("MyField")
+        self.assertTrue(f.getArray().isEqual(DataArrayDouble([60.75,52.75,52.75,44.75,52.75,44.75,44.75,36.75]),1e-12))
+        self.assertRaises(InterpKernelException,f.applyLin,2.,0.,1)# compoId 1 whereas f has only one component !
+        self.assertTrue(f.getArray().isEqual(DataArrayDouble([60.75,52.75,52.75,44.75,52.75,44.75,44.75,36.75]),1e-12))
+        f.applyLin(2.,0.,0)# here it is OK !
+        self.assertTrue(f.getArray().isEqual(DataArrayDouble([121.5,105.5,105.5,89.5,105.5,89.5,89.5,73.5]),1e-12))
+        #f.applyLin(2.,0.)
+        #self.assertTrue(f.getArray().isEqual(DataArrayDouble([243.,211.,211.,179.,211.,179.,179.,127.]),1e-12))
+        pass
     
     def setUp(self):
         pass