From fa007ead3ed05b03b0bf03a802e8082ee6515bd8 Mon Sep 17 00:00:00 2001 From: tbeu Date: Sun, 25 Feb 2024 12:14:21 +0100 Subject: [PATCH] update --- .pre-commit-config.yaml | 2 +- cmake/staticAnalyzers.cmake | 5 +---- src/mat5.c | 5 +++++ test/test_mat.c | 32 +++++++++++++++++--------------- tools/matdump.c | 2 +- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0d9b19be..d2ff9ad7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ repos: rev: v17.0.6 hooks: - id: clang-format - files: ^(src\/.+\.[ch]|tools\/matdump.c)$ + files: ^(src\/.+\.[ch]|tools\/matdump.c|test\/test_mat.c)$ exclude: ^(src\/fortran\/matio_internal.c|src\/snprintf.c|src\/safe-math.h)$ - repo: https://github.com/pre-commit/pre-commit-hooks diff --git a/cmake/staticAnalyzers.cmake b/cmake/staticAnalyzers.cmake index 08b47c65..7998e522 100644 --- a/cmake/staticAnalyzers.cmake +++ b/cmake/staticAnalyzers.cmake @@ -3,6 +3,7 @@ if(MATIO_ENABLE_CPPCHECK) if(CPPCHECK) set(CMAKE_C_CPPCHECK ${CPPCHECK} --language=c + --quiet --check-level=exhaustive --suppress=checkersReport --suppress=unusedFunction @@ -11,11 +12,7 @@ if(MATIO_ENABLE_CPPCHECK) --enable=all --inline-suppr --inconclusive - -i ${CMAKE_SOURCE_DIR}/test - -i ${CMAKE_SOURCE_DIR}/snprintf - -i ${CMAKE_SOURCE_DIR}/getopt ) - message(VERBOSE "Cppcheck finished setting up.") else() message(SEND_ERROR "Cppcheck requested but executable not found.") endif() diff --git a/src/mat5.c b/src/mat5.c index fd4dae3c..f6f833a2 100644 --- a/src/mat5.c +++ b/src/mat5.c @@ -945,6 +945,7 @@ WriteCompressedData(mat_t *mat, z_streamp z, void *data, int N, enum matio_types z->next_out = buf; z->avail_out = buf_size; deflate(z, Z_NO_FLUSH); + /* // cppcheck-suppress uninitvar */ byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); @@ -2299,6 +2300,7 @@ WriteCompressedTypeArrayFlags(mat_t *mat, matvar_t *matvar, z_streamp z) z->next_out = ZLIB_BYTE_PTR(comp_buf); z->avail_out = buf_size * sizeof(*comp_buf); deflate(z, Z_NO_FLUSH); + /* // cppcheck-suppress uninitvar */ byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); @@ -2540,6 +2542,7 @@ WriteCompressedCellArrayField(mat_t *mat, matvar_t *matvar, z_streamp z) z->next_out = ZLIB_BYTE_PTR(comp_buf); z->avail_out = buf_size * sizeof(*comp_buf); deflate(z, Z_NO_FLUSH); + /* // cppcheck-suppress uninitvar */ byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); @@ -2673,6 +2676,7 @@ WriteCompressedStructField(mat_t *mat, matvar_t *matvar, z_streamp z) z->next_out = ZLIB_BYTE_PTR(comp_buf); z->avail_out = buf_size * sizeof(*comp_buf); deflate(z, Z_NO_FLUSH); + /* // cppcheck-suppress uninitvar */ byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); @@ -5025,6 +5029,7 @@ Mat_VarWrite5(mat_t *mat, matvar_t *matvar, int compress) z->next_out = ZLIB_BYTE_PTR(comp_buf); z->avail_out = buf_size * sizeof(*comp_buf); deflate(z, Z_NO_FLUSH); + /* // cppcheck-suppress uninitvar */ fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); uncomp_buf[0] = array_flags_type; diff --git a/test/test_mat.c b/test/test_mat.c index 7799ab5d..dc99ddaa 100644 --- a/test/test_mat.c +++ b/test/test_mat.c @@ -1885,7 +1885,6 @@ test_write_empty_cell(const char *output_name) mat = Mat_CreateVer(output_name, NULL, mat_file_ver); if ( mat ) { /* Write an empty cell */ - matvar[0] = NULL; dims[0] = 0; dims[1] = 1; cell_matvar = Mat_VarCreate("var1", MAT_C_CELL, MAT_T_CELL, 2, dims, NULL, 0); @@ -2502,7 +2501,7 @@ test_struct_api_getfieldnames(void) printf(" None\n"); } else { for ( i = 0; i < nfields; i++ ) - printf(" %3d. %s\n", i, fieldnames2[i]); + printf(" %3u. %s\n", i, fieldnames2[i]); } Mat_VarFree(matvar); @@ -2514,7 +2513,7 @@ test_struct_api_getfieldnames(void) printf(" None\n"); } else { for ( i = 0; i < nfields; i++ ) - printf(" %3d. %s\n", i, fieldnames2[i]); + printf(" %3u. %s\n", i, fieldnames2[i]); } Mat_VarFree(matvar); @@ -2525,7 +2524,7 @@ test_struct_api_getfieldnames(void) printf(" None\n"); } else { for ( i = 0; i < nfields; i++ ) - printf(" %3d. %s\n", i, fieldnames2[i]); + printf(" %3u. %s\n", i, fieldnames2[i]); } return err; @@ -2535,7 +2534,7 @@ static int test_struct_api_addfield(void) { size_t dims[2]; - double data1[2] = {0, 1}, data2[3] = {2, 3, 4}, data3[3] = {5, 6, 7}, data4[2] = {8, 9}; + const double data1[2] = {0, 1}, data2[3] = {2, 3, 4}, data3[3] = {5, 6, 7}, data4[2] = {8, 9}; matvar_t *fields[5], *matvar; dims[0] = 2; @@ -2863,7 +2862,8 @@ static int test_get_struct_field(const char *file, const char *structname, const char *fieldname) { mat_t *mat; - matvar_t *matvar, *field; + matvar_t *matvar; + const matvar_t *field; int index = 1, err = 0; mat = Mat_Open(file, MAT_ACC_RDONLY); @@ -2908,7 +2908,8 @@ test_get_struct_field(const char *file, const char *structname, const char *fiel static int test_readslab(const char *file, const char *var, enum matio_classes matvar_class) { - int start[3] = {0, 0, 0}, stride[3] = {1, 1, 1}, edge[3] = {2, 2, 1}, err = 0; + const int start[3] = {0, 0, 0}, edge[3] = {2, 2, 1}; + int stride[3] = {1, 1, 1}, err = 0; mat_t *mat; matvar_t *matvar; @@ -3374,7 +3375,7 @@ static int test_writenan(void) { int err = 0, i; - size_t dims[2] = {5, 5}; + const size_t dims[2] = {5, 5}; double data[25] = { 0.0, }; @@ -3405,7 +3406,7 @@ static int test_writeinf(const char *output_name) { int err = 0, i; - size_t dims[2] = {5, 5}; + const size_t dims[2] = {5, 5}; double data[25] = { 0.0, }; @@ -3436,7 +3437,7 @@ static int test_write_sparse(enum matio_classes matvar_class, const char *output_name) { int err = 0; - size_t dims[2] = {5, 10}; + const size_t dims[2] = {5, 10}; mat_uint32_t ir[25] = {0, 4, 1, 2, 3, 0, 4, 1, 2, 3, 0, 4, 1, 2, 3, 0, 4, 1, 2, 3, 0, 4, 1, 2, 3}; mat_uint32_t jc[11] = {0, 2, 5, 7, 10, 12, 15, 17, 20, 22, 25}; @@ -3566,7 +3567,7 @@ static int test_write_complex_sparse(enum matio_classes matvar_class, const char *output_name) { int err = 0; - size_t dims[2] = {5, 10}; + const size_t dims[2] = {5, 10}; mat_uint32_t ir[25] = {0, 4, 1, 2, 3, 0, 4, 1, 2, 3, 0, 4, 1, 2, 3, 0, 4, 1, 2, 3, 0, 4, 1, 2, 3}; mat_uint32_t jc[11] = {0, 2, 5, 7, 10, 12, 15, 17, 20, 22, 25}; @@ -3738,7 +3739,7 @@ test_write_complex_sparse(enum matio_classes matvar_class, const char *output_na } static int -test_delete(char *file, char *name) +test_delete(char *file, const char *name) { int err = 0; mat_t *mat; @@ -4148,7 +4149,8 @@ main(int argc, char *argv[]) err += test_write_complex_sparse(matvar_class, output_name); ntests++; } else if ( !strcasecmp(argv[k], "ind2sub") ) { - size_t *subs, dims[3] = {256, 256, 124}; + size_t *subs; + const size_t dims[3] = {256, 256, 124}; redirect_output(output_name); subs = Mat_CalcSubscripts2(3, dims, 18921 - 1); Mat_Message("(%zu,%zu,%zu)", subs[0], subs[1], subs[2]); @@ -4156,7 +4158,8 @@ main(int argc, char *argv[]) k++; ntests++; } else if ( !strcasecmp(argv[k], "sub2ind") ) { - size_t dims[3] = {256, 256, 124}, index[3] = {233, 74, 1}; + const size_t dims[3] = {256, 256, 124}; + const size_t index[3] = {233, 74, 1}; size_t linear_index = 0; redirect_output(output_name); err += Mat_CalcSingleSubscript2(3, dims, index, &linear_index); @@ -4191,7 +4194,6 @@ main(int argc, char *argv[]) ntests++; } else { Mat_Critical("Unrecognized test %s", argv[k]); - k++; break; } } diff --git a/tools/matdump.c b/tools/matdump.c index b8a8c3d7..2f44530d 100644 --- a/tools/matdump.c +++ b/tools/matdump.c @@ -660,7 +660,7 @@ print_default_numeric_2d(const matvar_t *matvar) printf("\n"); } } else { - char *data = (char *)matvar->data; + const char *data = (char *)matvar->data; for ( i = 0; i < matvar->dims[0]; i++ ) { for ( j = 0; j < matvar->dims[1]; j++ ) { size_t idx = matvar->dims[0] * j + i;