Skip to content

Commit

Permalink
refs #2192: MSVC: Avoid heap corruption
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
beutlich committed Jan 24, 2018
1 parent b19f734 commit 075b560
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Modelica/Resources/C-Sources/ModelicaIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Modelica/Resources/C-Sources/ModelicaIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 11 additions & 11 deletions Modelica/Resources/C-Sources/ModelicaStandardTables.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 075b560

Please sign in to comment.