From d701040d22947319d7fd6b636b1c88b8c216cf0e Mon Sep 17 00:00:00 2001 From: Scott Fennell Date: Fri, 28 Sep 2018 11:17:42 -0500 Subject: [PATCH] handle invalid swap magic and delete dangling pointers MatLab.cpp --- trick_source/data_products/Log/MatLab.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/trick_source/data_products/Log/MatLab.cpp b/trick_source/data_products/Log/MatLab.cpp index 075bd09f..2d79ca00 100644 --- a/trick_source/data_products/Log/MatLab.cpp +++ b/trick_source/data_products/Log/MatLab.cpp @@ -61,7 +61,7 @@ MatLab::MatLab(char * file_name, char * param_name, char * time_name) { int array_type, array_bytes, array_flags[2]; int array_complex, array_class ; int dim_type, dim_bytes, num_dims; - int * dims; + int * dims = nullptr; int name_bytes; char * name, * field_name, * variable_name; int real_type, real_bytes; @@ -111,7 +111,7 @@ MatLab::MatLab(char * file_name, char * param_name, char * time_name) { swap_ = 0; } else if (magic == 0x494d) { swap_ = 1; - } + } param_found = time_found = false; while ( (!param_found || !time_found) && !feof(fp_)) { @@ -166,7 +166,10 @@ MatLab::MatLab(char * file_name, char * param_name, char * time_name) { } num_dims = dim_bytes / 4; - + // delete dangling pointer + if(dims) { + delete [] dims; + } dims = new int[num_dims]; for (ii = 0; ii < num_dims; ii++) { fread(&dims[ii], 4, 1, fp_) ; @@ -300,7 +303,10 @@ MatLab::MatLab(char * file_name, char * param_name, char * time_name) { } num_dims = dim_bytes / 4; - + // delete dangling pointer + if(dims) { + delete [] dims; + } dims = new int[num_dims]; for (ii = 0; ii < num_dims; ii++) { fread(&dims[ii], 4, 1, fp_) ; @@ -697,6 +703,9 @@ int MatLabLocateParam(char * file_name, char * param_name, char * time_name) { swap = 0; } else if (magic == 0x494d) { swap = 1; + } else { + std::cerr << "ERROR: MatLab.cpp swap magic is invalid, default to byte swap == false" << std::endl; + swap = 0; } param_found = time_found = false; @@ -749,7 +758,10 @@ int MatLabLocateParam(char * file_name, char * param_name, char * time_name) { } num_dims = dim_bytes / 4; - + // delete dangling pointer + if(dims) { + delete [] dims; + } dims = new int[num_dims]; for (ii = 0; ii < num_dims; ii++) { fread(&dims[ii], 4, 1, fp) ;