diff --git a/src/tinymatwriter.cpp b/src/tinymatwriter.cpp index 4bfd0ee..6dc576d 100644 --- a/src/tinymatwriter.cpp +++ b/src/tinymatwriter.cpp @@ -328,8 +328,11 @@ const char *TinyMATWriter_getVersion() else if (newsize < 1000 * 1024 * 1024) newsize = newsize * 3 / 2; else newsize = newsize * 6 / 5; } - file->filedata = (uint8_t*)realloc(file->filedata, newsize); - file->filedata_size = newsize; + auto newMem = (uint8_t*)realloc(file->filedata, newsize); + if (newMem) { + file->filedata=newMem; + file->filedata_size = newsize; + } } #endif } @@ -615,8 +618,10 @@ TINYMAT_inlineattrib static void TinyMAT_writeDatElement_string(TinyMATWriterFil std::unique_ptr tmp; if (slen>0 && data) { tmp=std::unique_ptr(new int16_t[slen]); - for (uint32_t i=0; i inline void TinyMATWriter_writeContainerAsRow_internalCopy(TinyMATWriterFile* mat, const char* name, const T& data_vec) { int32_t siz[2]={1, (int32_t)data_vec.size()}; auto tmp=static_cast(malloc(data_vec.size()*sizeof(typename T::value_type))); - int i=0; - for (auto it=data_vec.begin(); it!=data_vec.end(); ++it) { - tmp[i]=*it; - i++; + if (tmp) { + 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, siz, 2); - free(tmp); } /*! \brief write a 1-dimensional vector/list/... of values as a column-vector into a MAT-file