From 6105a1887015e6433a8f2c701fc764384f20b0ff Mon Sep 17 00:00:00 2001 From: ageay Date: Fri, 5 Apr 2013 13:12:31 +0000 Subject: [PATCH] Protection against segfault when pushing invalid GAUSS field in MEDFileField* instance --- src/MEDLoader/MEDFileField.cxx | 4 ++++ src/MEDLoader/Swig/MEDLoaderTest3.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/MEDLoader/MEDFileField.cxx b/src/MEDLoader/MEDFileField.cxx index 91d0b6275..fce2b6b6c 100644 --- a/src/MEDLoader/MEDFileField.cxx +++ b/src/MEDLoader/MEDFileField.cxx @@ -1063,6 +1063,8 @@ std::vector MEDFileFieldPerMeshPerType::addNewEntryIfNecessaryGauss(const M if(!disc2) throw INTERP_KERNEL::Exception("addNewEntryIfNecessaryGauss : invalid call to this method ! Internal Error !"); const DataArrayInt *da=disc2->getArrayOfDiscIds(); + if(!da) + throw INTERP_KERNEL::Exception("addNewEntryIfNecessaryGauss (no profile) : no localization ids per cell array available ! The input Gauss node field is maybe invalid !"); MEDCouplingAutoRefCountObjectPtr da2=da->selectByTupleId2(offset,offset+nbOfCells,1); MEDCouplingAutoRefCountObjectPtr retTmp=da2->getDifferentValues(); if(retTmp->presenceOfValue(-1)) @@ -1133,6 +1135,8 @@ std::vector MEDFileFieldPerMeshPerType::addNewEntryIfNecessaryGauss(const M if(!disc2) throw INTERP_KERNEL::Exception("addNewEntryIfNecessaryGauss : invalid call to this method ! Internal Error !"); const DataArrayInt *da=disc2->getArrayOfDiscIds(); + if(!da) + throw INTERP_KERNEL::Exception("addNewEntryIfNecessaryGauss : no localization ids per cell array available ! The input Gauss node field is maybe invalid !"); MEDCouplingAutoRefCountObjectPtr da2=da->selectByTupleIdSafe(subCells->getConstPointer(),subCells->getConstPointer()+subCells->getNumberOfTuples()); MEDCouplingAutoRefCountObjectPtr retTmp=da2->getDifferentValues(); if(retTmp->presenceOfValue(-1)) diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 891437501..1f7d5d68e 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -555,6 +555,10 @@ class MEDLoaderTest(unittest.TestCase): # fname="Pyfile28.med" f1=MEDLoaderDataForTest.buildVecFieldOnGauss_2_Simpler(); + f1InvalidCpy=f1.deepCpy() + f1InvalidCpy.setDiscretization(MEDCouplingFieldDiscretizationGauss()) + f1InvalidCpy2=f1.deepCpy() + f1InvalidCpy2.setDiscretization(MEDCouplingFieldDiscretizationGauss()) m1=f1.getMesh() mm1=MEDFileUMesh.New() mm1.setCoords(m1.getCoords()) @@ -562,6 +566,9 @@ class MEDLoaderTest(unittest.TestCase): mm1.setName(m1.getName()) mm1.write(fname,2) ff1=MEDFileField1TS.New() + self.assertRaises(InterpKernelException,ff1.setFieldNoProfileSBT,f1InvalidCpy) # fails because no Gauss localization per cell set !* + f1InvalidCpy2.getDiscretization().setArrayOfDiscIds(f1.getDiscretization().getArrayOfDiscIds()) # fails because no Gauss localization set whereas gauss locid per cell given ! + self.assertRaises(InterpKernelException,ff1.setFieldNoProfileSBT,f1InvalidCpy2) ff1.setFieldNoProfileSBT(f1) ff1.write(fname,0) ff2=MEDFileField1TS.New(fname,f1.getName(),f1.getTime()[1],f1.getTime()[2]) @@ -1483,10 +1490,13 @@ class MEDLoaderTest(unittest.TestCase): da=DataArrayDouble(34) ; da.iota(3.) f.setArray(da) f.setName("fieldCellOnPflWithoutPfl") + fInvalid=f.deepCpy() f.setGaussLocalizationOnCells([0,1,2,3,4,5,6,7,8],[0.,0.,1.,0.,1.,1.],[0.3,0.3,0.7,0.7],[0.8,0.2]) f.setGaussLocalizationOnCells([9,10],[0.,0.,1.,0.,1.,1.],[0.3,0.3,0.7,0.7,0.8,0.8],[0.8,0.07,0.13]) f.setGaussLocalizationOnCells([11,12],[0.,0.,1.,0.,1.,1.,0.,1.],[0.3,0.3,0.7,0.7,0.8,0.8,0.8,0.8,0.8,0.8],[0.8,0.07,0.1,0.01,0.02]) f.checkCoherency() + fInvalid2=fInvalid.deepCpy() + fInvalid2.getDiscretization().setArrayOfDiscIds(f.getDiscretization().getArrayOfDiscIds()) # mm=MEDFileUMesh() mm.setMeshAtLevel(0,m) @@ -1494,6 +1504,8 @@ class MEDLoaderTest(unittest.TestCase): # f1ts=MEDFileField1TS.New() pfl=DataArrayInt(range(13)) ; pfl.setName("pfl") + self.assertRaises(InterpKernelException,f1ts.setFieldProfile,fInvalid,mm,0,pfl) # fails because no Gauss localization per cell set ! + self.assertRaises(InterpKernelException,f1ts.setFieldProfile,fInvalid2,mm,0,pfl) # fails because no Gauss localization set whereas gauss locid per cell given ! f1ts.setFieldProfile(f,mm,0,pfl) f1ts.write(fname,0) # -- 2.39.2