From 075b5609941d2a64eb634b6c52bfb048d49e1890 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Wed, 24 Jan 2018 14:52:23 +0100 Subject: [PATCH] refs #2192: MSVC: Avoid heap corruption Avoid heap corruption if ModelicaIO.dll and ModelicaStandardTables.dll are built with different VC run-time options. Free the table in the same address space where it was allocated earlier. --- Modelica/Resources/C-Sources/ModelicaIO.c | 4 ++++ Modelica/Resources/C-Sources/ModelicaIO.h | 6 +++++ .../C-Sources/ModelicaStandardTables.c | 22 +++++++++---------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Modelica/Resources/C-Sources/ModelicaIO.c b/Modelica/Resources/C-Sources/ModelicaIO.c index b25a9fbdba..9b97f86a6f 100644 --- a/Modelica/Resources/C-Sources/ModelicaIO.c +++ b/Modelica/Resources/C-Sources/ModelicaIO.c @@ -372,6 +372,10 @@ double* ModelicaIO_readRealTable(_In_z_ const char* fileName, return table; } +void ModelicaIO_freeRealTable(double* table) { + free(table); +} + static double* readMatTable(_In_z_ const char* fileName, _In_z_ const char* tableName, _Out_ size_t* m, _Out_ size_t* n) { double* table = NULL; diff --git a/Modelica/Resources/C-Sources/ModelicaIO.h b/Modelica/Resources/C-Sources/ModelicaIO.h index 6dda5e97dc..fdb6553ff5 100644 --- a/Modelica/Resources/C-Sources/ModelicaIO.h +++ b/Modelica/Resources/C-Sources/ModelicaIO.h @@ -140,4 +140,10 @@ MODELICA_EXPORT double* ModelicaIO_readRealTable(_In_z_ const char* fileName, <- RETURN: Array of dimensions m by n */ +MODELICA_EXPORT void ModelicaIO_freeRealTable(double* table); + /* Free table + Note: Only called from ModelicaStandardTables to free the allocated memory by + ModelicaIO_readRealTable + */ + #endif diff --git a/Modelica/Resources/C-Sources/ModelicaStandardTables.c b/Modelica/Resources/C-Sources/ModelicaStandardTables.c index 0fc486e3c7..efbe924d96 100644 --- a/Modelica/Resources/C-Sources/ModelicaStandardTables.c +++ b/Modelica/Resources/C-Sources/ModelicaStandardTables.c @@ -594,7 +594,7 @@ void* ModelicaStandardTables_CombiTimeTable_init2(_In_z_ const char* fileName, if (NULL != file) { MUTEX_LOCK(); if (--file->refCount == 0) { - free(file->table); + ModelicaIO_freeRealTable(file->table); free(file->key); HASH_DEL(tableShare, file); free(file); @@ -773,7 +773,7 @@ void ModelicaStandardTables_CombiTimeTable_close(void* _tableID) { if (NULL != file) { /* Share hit */ if (--file->refCount == 0) { - free(file->table); + ModelicaIO_freeRealTable(file->table); free(file->key); HASH_DEL(tableShare, file); free(file); @@ -1766,7 +1766,7 @@ void* ModelicaStandardTables_CombiTable1D_init2(_In_z_ const char* fileName, if (NULL != file) { MUTEX_LOCK(); if (--file->refCount == 0) { - free(file->table); + ModelicaIO_freeRealTable(file->table); free(file->key); HASH_DEL(tableShare, file); free(file); @@ -1940,7 +1940,7 @@ void ModelicaStandardTables_CombiTable1D_close(void* _tableID) { if (NULL != file) { /* Share hit */ if (--file->refCount == 0) { - free(file->table); + ModelicaIO_freeRealTable(file->table); free(file->key); HASH_DEL(tableShare, file); free(file); @@ -2405,7 +2405,7 @@ void* ModelicaStandardTables_CombiTable2D_init2(_In_z_ const char* fileName, if (NULL != file) { MUTEX_LOCK(); if (--file->refCount == 0) { - free(file->table); + ModelicaIO_freeRealTable(file->table); free(file->key); HASH_DEL(tableShare, file); free(file); @@ -2548,7 +2548,7 @@ void ModelicaStandardTables_CombiTable2D_close(void* _tableID) { if (NULL != file) { /* Share hit */ if (--file->refCount == 0) { - free(file->table); + ModelicaIO_freeRealTable(file->table); free(file->key); HASH_DEL(tableShare, file); free(file); @@ -5632,7 +5632,7 @@ static READ_RESULT readTable(_In_z_ const char* fileName, _In_z_ const char* tab /* Again allocate and set key */ key = (char*)malloc((lenFileName + strlen(tableName) + 2) * sizeof(char)); if (NULL == key) { - free(table); + ModelicaIO_freeRealTable(table); return file; } strcpy(key, fileName); @@ -5655,14 +5655,14 @@ static READ_RESULT readTable(_In_z_ const char* fileName, _In_z_ const char* tab if (NULL == file->hh.tbl) { free(key); free(file); - free(table); + ModelicaIO_freeRealTable(table); MUTEX_UNLOCK(); return NULL; } } else { free(key); - free(table); + ModelicaIO_freeRealTable(table); MUTEX_UNLOCK(); return file; } @@ -5673,7 +5673,7 @@ static READ_RESULT readTable(_In_z_ const char* fileName, _In_z_ const char* tab */ free(key); if (file->refCount == 1) { - free(file->table); + ModelicaIO_freeRealTable(file->table); file->nRow = *nRow; file->nCol = *nCol; file->table = table; @@ -5688,7 +5688,7 @@ static READ_RESULT readTable(_In_z_ const char* fileName, _In_z_ const char* tab */ free(key); if (NULL != table) { - free(table); + ModelicaIO_freeRealTable(table); } file->refCount++; *nRow = file->nRow;