Fix memory corruption introduced into ef9db08c81

This commit is contained in:
Anthony Geay 2021-06-23 08:38:56 +02:00
parent ef9db08c81
commit e34095b65b
3 changed files with 12 additions and 18 deletions

View File

@ -369,24 +369,19 @@ Driver_Mesh::Status DriverMED_W_Field::Perform()
*/ */
Driver_Mesh::Status DriverMED_W_Field_Mem::Perform() Driver_Mesh::Status DriverMED_W_Field_Mem::Perform()
{ {
void *ptr(nullptr);
std::size_t sz(0);
Driver_Mesh::Status status = Driver_Mesh::DRS_OK; Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
bool isClosed(false); bool isClosed(false);
MED::TMemFile *tfileInst = nullptr; void *ptr(_data->getPointer());
char *initPtr(_data->getPointer()); std::size_t sz(_data->getNumberOfTuples());
mcIdType initSz(_data->getNumberOfTuples());
_data->accessToMemArray().setSpecificDeallocator(nullptr); _data->accessToMemArray().setSpecificDeallocator(nullptr);
_data->useArray(nullptr,false,MEDCoupling::DeallocType::C_DEALLOC,0,1); _data->useArray(nullptr,false,MEDCoupling::DeallocType::C_DEALLOC,0,1);
{// let braces to flush (call of MED::PWrapper myMed destructor) {// let braces to flush (call of MED::PWrapper myMed destructor)
tfileInst = new MED::TMemFile(initPtr,initSz,&isClosed); MED::TMemFile *tfileInst = new MED::TMemFile(&ptr,&sz,&isClosed);
MED::PWrapper myMed = MED::CrWrapperW(myFile, -1, tfileInst); MED::PWrapper myMed = MED::CrWrapperW(myFile, -1, tfileInst);
status = this->PerformInternal(myMed); status = this->PerformInternal(myMed);
} }
if(tfileInst) if(!isClosed)
{ EXCEPTION(std::runtime_error, "TFTMemFile destructor : on destruction file has not been closed properly -> chunk of memory data may be invalid !");
ptr = tfileInst->getData(); sz = tfileInst->getSize();
}
_data = MEDCoupling::DataArrayByte::New(); _data = MEDCoupling::DataArrayByte::New();
_data->useArray(reinterpret_cast<char *>(ptr),true,MEDCoupling::DeallocType::C_DEALLOC,sz,1); _data->useArray(reinterpret_cast<char *>(ptr),true,MEDCoupling::DeallocType::C_DEALLOC,sz,1);
return status; return status;

View File

@ -356,16 +356,13 @@ Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh_Mem::Perform()
std::size_t sz(0); std::size_t sz(0);
Driver_Mesh::Status status = Driver_Mesh::DRS_OK; Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
bool isClosed(false); bool isClosed(false);
TMemFile *tfileInst = nullptr;
{// let braces to flush (call of MED::PWrapper myMed destructor) {// let braces to flush (call of MED::PWrapper myMed destructor)
tfileInst = new TMemFile(&isClosed); TMemFile *tfileInst = new TMemFile(&ptr,&sz,&isClosed);// this new will be destroyed by destructor of myMed
MED::PWrapper myMed = CrWrapperW(myFile, -1, tfileInst); MED::PWrapper myMed = CrWrapperW(myFile, -1, tfileInst);
status = this->PerformInternal<MED::PWrapper>(myMed); status = this->PerformInternal<MED::PWrapper>(myMed);
} }
if(tfileInst) if(!isClosed)
{ EXCEPTION(std::runtime_error, "TFTMemFile destructor : on destruction file has not been closed properly -> chunk of memory data may be invalid !");
ptr = tfileInst->getData(); sz = tfileInst->getSize();
}
_data = MEDCoupling::DataArrayByte::New(); _data = MEDCoupling::DataArrayByte::New();
_data->useArray(reinterpret_cast<char *>(ptr),true,MEDCoupling::DeallocType::C_DEALLOC,sz,1); _data->useArray(reinterpret_cast<char *>(ptr),true,MEDCoupling::DeallocType::C_DEALLOC,sz,1);
if(!isClosed) if(!isClosed)

View File

@ -73,12 +73,14 @@ namespace MED
class MEDWRAPPER_EXPORT TMemFile : public MEDIDTHoder class MEDWRAPPER_EXPORT TMemFile : public MEDIDTHoder
{ {
public: public:
TMemFile(bool* isClosedStatus = nullptr):MEDIDTHoder(isClosedStatus) { memfile.app_image_ptr=nullptr; memfile.app_image_size=0; } TMemFile(void **data, std::size_t *sz, bool* isClosedStatus):MEDIDTHoder(isClosedStatus),_data(data),_sz(sz) { memfile.app_image_ptr=*data; memfile.app_image_size=*sz; }
TMemFile(void *data, std::size_t sz, bool* isClosedStatus):MEDIDTHoder(isClosedStatus) { memfile.app_image_ptr=data; memfile.app_image_size=sz; } ~TMemFile() { UnRefFid(); if(myIsClosed) { *_data = memfile.app_image_ptr; *_sz = memfile.app_image_size; } }
void Open(EModeAcces theMode, TErr* theErr = nullptr) override; void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
void *getData() const { return memfile.app_image_ptr; } void *getData() const { return memfile.app_image_ptr; }
std::size_t getSize() const { return memfile.app_image_size; } std::size_t getSize() const { return memfile.app_image_size; }
private: private:
void **_data = nullptr;
std::size_t * _sz = nullptr;
med_memfile memfile = MED_MEMFILE_INIT; med_memfile memfile = MED_MEMFILE_INIT;
}; };