From 3a8d9cc4fce1c61b50150891a59ccd4cc56d5587 Mon Sep 17 00:00:00 2001 From: jkriege2 Date: Tue, 21 May 2024 12:09:00 +0200 Subject: [PATCH] fixed some security issues --- src/tinymatwriter.cpp | 64 +++++++++++++++++++++---------------------- src/tinymatwriter.h | 5 ++-- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/tinymatwriter.cpp b/src/tinymatwriter.cpp index e177f79..4bfd0ee 100644 --- a/src/tinymatwriter.cpp +++ b/src/tinymatwriter.cpp @@ -16,8 +16,6 @@ */ -#include -#include #include #include #include @@ -614,9 +612,9 @@ TINYMAT_inlineattrib static void TinyMAT_writeDatElement_stringas8bit(TinyMATWri TINYMAT_inlineattrib static void TinyMAT_writeDatElement_string(TinyMATWriterFile* mat, const char* data, uint32_t slen) { size_t pad=(2*slen)%8; - int16_t* tmp=NULL; + std::unique_ptr tmp; if (slen>0 && data) { - tmp=(int16_t*)malloc(slen*2); + tmp=std::unique_ptr(new int16_t[slen]); for (uint32_t i=0; i0 && tmp) { - TinyMAT_fwrite(tmp, 2, slen, mat); + TinyMAT_fwrite(tmp.get(), 2, slen, mat); // write padding if (pad>0) { static const uint8_t paddata[8] = { 0,0,0,0,0,0,0,0 }; @@ -634,8 +632,6 @@ TINYMAT_inlineattrib static void TinyMAT_writeDatElement_string(TinyMATWriterFil } else { } - if (tmp) free(tmp); - } TINYMAT_inlineattrib static void TinyMAT_writeDatElement_stringas8bit(TinyMATWriterFile* mat, const char* data) { @@ -1158,21 +1154,22 @@ void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *na TinyMAT_writeDatElement_stringas8bit(mat, name); // write data type - int8_t* dat=NULL; + std::unique_ptr tmp; if (nentries>0) { - dat=(int8_t*)malloc(nentries*sizeof(int8_t*)); - for (uint32_t i=0; i(new int8_t[nentries]); + if (tmp) { + for (uint32_t i=0; i d; if (data.size()>0) { - d=(double*)malloc(data.size()*sizeof(double)); - int i=0; - for (std::list::const_iterator it=data.begin(); it!=data.end(); it++) { - d[i]=*it; - i++; + d=std::unique_ptr(new double[data.size()]); + if (d) { + int i=0; + for (std::list::const_iterator it=data.begin(); it!=data.end(); it++) { + d[i]=*it; + i++; + } } } // write data type - TinyMAT_writeDatElement_dbla(mat, d, (uint32_t)data.size()); - if (d) free(d); + TinyMAT_writeDatElement_dbla(mat, d.get(), (uint32_t)data.size()); } @@ -1331,19 +1329,20 @@ void TinyMATWriter_writeDoubleVector(TinyMATWriterFile *mat, const char *name, c // write field name TinyMAT_writeDatElement_stringas8bit(mat, name); - double * d=NULL; + std::unique_ptr d; if (data.size()>0) { - d=(double*)malloc(data.size()*sizeof(double)); - int i=0; - for (std::vector::const_iterator it=data.begin(); it!=data.end(); it++) { - d[i]=*it; - i++; + d=std::unique_ptr(new double[data.size()]); + if (d) { + int i=0; + for (std::vector::const_iterator it=data.begin(); it!=data.end(); it++) { + d[i]=*it; + i++; + } } } // write data type - TinyMAT_writeDatElement_dbla(mat, d, (uint32_t)data.size()); - if (d) free(d); + TinyMAT_writeDatElement_dbla(mat, d.get(), (uint32_t)data.size()); } @@ -1512,9 +1511,9 @@ void TinyMATWriter_endStruct(TinyMATWriterFile* mat) { long start=TinyMAT_ftell(mat); long ssize=start-struc.data_start; - uint8_t* tmpdata=(uint8_t*)malloc(ssize*sizeof(uint8_t)); + auto tmpdata=std::unique_ptr(new uint8_t[ssize]); TinyMAT_fseek(mat, struc.data_start); - TinyMAT_fread(tmpdata, ssize, 1, mat); + TinyMAT_fread(tmpdata.get(), ssize, 1, mat); int32_t maxlen=0; @@ -1528,9 +1527,8 @@ void TinyMATWriter_endStruct(TinyMATWriterFile* mat) { // write field names TinyMAT_writeDatElement_stringas8bit(mat, joinednames.c_str(), (uint32_t)joinednames.size()); - TinyMAT_fwrite(tmpdata, ssize, 1, mat); + TinyMAT_fwrite(tmpdata.get(), ssize, 1, mat); - free(tmpdata); long endpos=TinyMAT_ftell(mat); diff --git a/src/tinymatwriter.h b/src/tinymatwriter.h index d4d94c3..e508d9f 100644 --- a/src/tinymatwriter.h +++ b/src/tinymatwriter.h @@ -758,14 +758,13 @@ inline void TinyMATWriter_writeContainerAsRow_internalCopy(TinyMATWriterFile* m template inline void TinyMATWriter_writeContainerAsColumn_internalCopy(TinyMATWriterFile* mat, const char* name, const T& data_vec) { int32_t siz[2]={(int32_t)data_vec.size(), 1}; - auto tmp=static_cast(malloc(data_vec.size()*sizeof(typename T::value_type))); + auto tmp=std::unique_ptr(new typename T::value_type[data_vec.size()]); int i=0; for (auto it=data_vec.begin(); it!=data_vec.end(); ++it) { tmp[i]=*it; i++; } - TinyMATWriter_writeMatrixND_rowmajor(mat, name, tmp, siz, 2); - free(tmp); + TinyMATWriter_writeMatrixND_rowmajor(mat, name, tmp.get(), siz, 2); }