From 324f59bb0bd6e9818294d220374db3795fd7268f Mon Sep 17 00:00:00 2001 From: tbeu Date: Sun, 25 Feb 2024 12:04:32 +0100 Subject: [PATCH] Fix issues reported by cppcheck --- src/inflate.c | 2 +- src/mat.c | 29 ++++++++----- src/mat4.c | 26 +++++------- src/mat4.h | 2 +- src/mat5.c | 84 +++++++++++++++++++------------------- src/matio.h | 8 ++-- test/test_snprintf.c | 12 +++--- tools/matdump.c | 96 ++++++++++++++++++++++---------------------- 8 files changed, 131 insertions(+), 128 deletions(-) diff --git a/src/inflate.c b/src/inflate.c index 22509d73..03bee228 100644 --- a/src/inflate.c +++ b/src/inflate.c @@ -404,7 +404,7 @@ InflateData(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes) if ( z->avail_in ) { const mat_off_t offset = -(mat_off_t)z->avail_in; (void)fseeko((FILE *)mat->fp, offset, SEEK_CUR); - bytesread -= z->avail_in; + /* bytesread -= z->avail_in; */ z->avail_in = 0; } diff --git a/src/mat.c b/src/mat.c index 601abfc2..bf69ee39 100644 --- a/src/mat.c +++ b/src/mat.c @@ -639,6 +639,9 @@ Mat_Open(const char *matname, int mode) mat = NULL; } + if ( NULL == mat ) + return mat; + if ( -1 < *(hid_t *)mat->fp ) { H5G_info_t group_info; herr_t herr; @@ -1204,9 +1207,9 @@ Mat_VarCreate(const char *name, enum matio_classes class_type, enum matio_types if ( matvar->isComplex ) { sparse_data->data = malloc(sizeof(mat_complex_split_t)); if ( NULL != sparse_data->data ) { - mat_complex_split_t *complex_data, *complex_data_in; - complex_data = (mat_complex_split_t *)sparse_data->data; - complex_data_in = (mat_complex_split_t *)sparse_data_in->data; + mat_complex_split_t *complex_data = (mat_complex_split_t *)sparse_data->data; + const mat_complex_split_t *complex_data_in = + (mat_complex_split_t *)sparse_data_in->data; complex_data->Re = malloc(sparse_data->ndata * data_size); complex_data->Im = malloc(sparse_data->ndata * data_size); if ( NULL != complex_data->Re ) @@ -1259,7 +1262,7 @@ Mat_CopyFile(const char *src, const char *dst) { size_t len; char buf[BUFSIZ] = {'\0'}; - FILE *in = NULL; + FILE *in; FILE *out = NULL; #if defined(_WIN32) @@ -1268,6 +1271,8 @@ Mat_CopyFile(const char *src, const char *dst) if ( NULL != wname ) { in = _wfopen(wname, L"rb"); free(wname); + } else { + in = NULL; } } #else @@ -1370,7 +1375,7 @@ Mat_VarDelete(mat_t *mat, const char *name) char *new_name = strdup(mat->filename); #if defined(MAT73) && MAT73 if ( mat_file_ver == MAT_FT_MAT73 ) { - err = Mat_Close73(mat); + /* err = */ Mat_Close73(mat); } #endif if ( mat->fp != NULL ) { @@ -1559,7 +1564,8 @@ Mat_VarDuplicate(const matvar_t *in, int opt) out_sparse->data = malloc(sizeof(mat_complex_split_t)); if ( out_sparse->data != NULL ) { mat_complex_split_t *out_data = (mat_complex_split_t *)out_sparse->data; - mat_complex_split_t *in_data = (mat_complex_split_t *)in_sparse->data; + const mat_complex_split_t *in_data = + (mat_complex_split_t *)in_sparse->data; out_data->Re = malloc(in_sparse->ndata * Mat_SizeOf(in->data_type)); if ( NULL != out_data->Re ) memcpy(out_data->Re, in_data->Re, @@ -1580,7 +1586,7 @@ Mat_VarDuplicate(const matvar_t *in, int opt) out->internal->data = malloc(sizeof(mat_complex_split_t)); if ( out->internal->data != NULL ) { mat_complex_split_t *out_data = (mat_complex_split_t *)out->internal->data; - mat_complex_split_t *in_data = (mat_complex_split_t *)in->internal->data; + const mat_complex_split_t *in_data = (mat_complex_split_t *)in->internal->data; out_data->Re = malloc(out->nbytes); if ( NULL != out_data->Re ) memcpy(out_data->Re, in_data->Re, out->nbytes); @@ -2465,7 +2471,8 @@ Mat_VarPrint(const matvar_t *matvar, int printdata) * @retval 0 on success */ int -Mat_VarReadData(mat_t *mat, matvar_t *matvar, void *data, int *start, int *stride, int *edge) +Mat_VarReadData(mat_t *mat, matvar_t *matvar, void *data, const int *start, const int *stride, + const int *edge) { int err = MATIO_E_NO_ERROR; @@ -2842,7 +2849,7 @@ Mat_VarReadNextPredicate(mat_t *mat, mat_iter_pred_t pred, const void *user_data * @see Mat_VarWrite/Mat_VarWriteAppend */ int -Mat_VarWriteInfo(mat_t *mat, matvar_t *matvar) +Mat_VarWriteInfo(const mat_t *mat, matvar_t *matvar) { Mat_Critical( "Mat_VarWriteInfo/Mat_VarWriteData is not supported. " @@ -2867,8 +2874,8 @@ Mat_VarWriteInfo(mat_t *mat, matvar_t *matvar) * @see Mat_VarWrite/Mat_VarWriteAppend */ int -Mat_VarWriteData(mat_t *mat, matvar_t *matvar, void *data, const int *start, const int *stride, - const int *edge) +Mat_VarWriteData(const mat_t *mat, matvar_t *matvar, void *data, const int *start, + const int *stride, const int *edge) { Mat_Critical( "Mat_VarWriteInfo/Mat_VarWriteData is not supported. " diff --git a/src/mat4.c b/src/mat4.c index 583294a8..9aee2a33 100644 --- a/src/mat4.c +++ b/src/mat4.c @@ -56,7 +56,7 @@ mat_t * Mat_Create4(const char *matname) { - FILE *fp = NULL; + FILE *fp; mat_t *mat = NULL; #if defined(_WIN32) @@ -64,6 +64,8 @@ Mat_Create4(const char *matname) if ( NULL != wname ) { fp = _wfopen(wname, L"w+b"); free(wname); + } else { + fp = NULL; } #else fp = fopen(matname, "w+b"); @@ -119,7 +121,6 @@ Mat_VarWrite4(mat_t *mat, const matvar_t *matvar) mat_int32_t namelen; } Fmatrix; - mat_uint32_t i; Fmatrix x; if ( NULL == mat || NULL == matvar ) @@ -200,9 +201,7 @@ Mat_VarWrite4(mat_t *mat, const matvar_t *matvar) fwrite(&x, sizeof(Fmatrix), 1, (FILE *)mat->fp); fwrite(matvar->name, sizeof(char), x.namelen, (FILE *)mat->fp); if ( matvar->isComplex ) { - mat_complex_split_t *complex_data; - - complex_data = (mat_complex_split_t *)matvar->data; + const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; fwrite(complex_data->Re, matvar->data_size, nelems, (FILE *)mat->fp); fwrite(complex_data->Im, matvar->data_size, nelems, (FILE *)mat->fp); } else { @@ -213,7 +212,7 @@ Mat_VarWrite4(mat_t *mat, const matvar_t *matvar) case MAT_C_SPARSE: { mat_sparse_t *sparse; double tmp; - mat_uint32_t j; + mat_uint32_t i, j; size_t stride = Mat_SizeOf(matvar->data_type); #if !defined(EXTENDED_SPARSE) if ( MAT_T_DOUBLE != matvar->data_type ) @@ -247,12 +246,9 @@ Mat_VarWrite4(mat_t *mat, const matvar_t *matvar) fwrite(&tmp, sizeof(double), 1, (FILE *)mat->fp); tmp = 0.; if ( matvar->isComplex ) { - mat_complex_split_t *complex_data; - char *re, *im; - - complex_data = (mat_complex_split_t *)sparse->data; - re = (char *)complex_data->Re; - im = (char *)complex_data->Im; + mat_complex_split_t *complex_data = (mat_complex_split_t *)sparse->data; + const char *re = (char *)complex_data->Re; + const char *im = (char *)complex_data->Im; for ( i = 0; i < sparse->njc - 1; i++ ) { for ( j = sparse->jc[i]; j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) { fwrite(re + j * stride, stride, 1, (FILE *)mat->fp); @@ -265,7 +261,7 @@ Mat_VarWrite4(mat_t *mat, const matvar_t *matvar) } } } else { - char *data = (char *)sparse->data; + const char *data = (char *)sparse->data; for ( i = 0; i < sparse->njc - 1; i++ ) { for ( j = sparse->jc[i]; j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) { fwrite(data + j * stride, stride, 1, (FILE *)mat->fp); @@ -768,8 +764,8 @@ Mat_VarRead4(mat_t *mat, matvar_t *matvar) * @endif */ int -Mat_VarReadData4(mat_t *mat, matvar_t *matvar, void *data, const int *start, const int *stride, - const int *edge) +Mat_VarReadData4(mat_t *mat, const matvar_t *matvar, void *data, const int *start, + const int *stride, const int *edge) { int err = MATIO_E_NO_ERROR; diff --git a/src/mat4.h b/src/mat4.h index 7455bcf5..1e81b41a 100644 --- a/src/mat4.h +++ b/src/mat4.h @@ -38,7 +38,7 @@ EXTERN mat_t *Mat_Create4(const char *matname); EXTERN int Mat_VarWrite4(mat_t *mat, const matvar_t *matvar); EXTERN int Mat_VarRead4(mat_t *mat, matvar_t *matvar); -EXTERN int Mat_VarReadData4(mat_t *mat, matvar_t *matvar, void *data, const int *start, +EXTERN int Mat_VarReadData4(mat_t *mat, const matvar_t *matvar, void *data, const int *start, const int *stride, const int *edge); EXTERN int Mat_VarReadDataLinear4(mat_t *mat, matvar_t *matvar, void *data, int start, int stride, int edge); diff --git a/src/mat5.c b/src/mat5.c index 801e8012..fd4dae3c 100644 --- a/src/mat5.c +++ b/src/mat5.c @@ -65,7 +65,7 @@ static mat_complex_split_t null_complex_data = {NULL, NULL}; static int GetTypeBufSize(matvar_t *matvar, size_t *size); static int GetStructFieldBufSize(matvar_t *matvar, size_t *size); static int GetCellArrayFieldBufSize(matvar_t *matvar, size_t *size); -static void SetFieldNames(matvar_t *matvar, char *buf, size_t nfields, +static void SetFieldNames(matvar_t *matvar, const char *buf, size_t nfields, mat_uint32_t fieldname_length); static size_t ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v); #if HAVE_ZLIB @@ -457,7 +457,7 @@ GetEmptyMatrixMaxBufSize(const char *name, int rank, size_t *size) } static void -SetFieldNames(matvar_t *matvar, char *buf, size_t nfields, mat_uint32_t fieldname_length) +SetFieldNames(matvar_t *matvar, const char *buf, size_t nfields, mat_uint32_t fieldname_length) { matvar->internal->num_fields = nfields; matvar->internal->fieldnames = (char **)calloc(nfields, sizeof(*matvar->internal->fieldnames)); @@ -479,12 +479,12 @@ ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v { int data_in_tag = 0; enum matio_types packed_type; - mat_uint32_t tag[2] = {0, 0}; size_t bytesread = 0; mat_uint32_t N = 0; if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { #if HAVE_ZLIB + mat_uint32_t tag[2] = {0, 0}; matvar->internal->z->avail_in = 0; if ( 0 != Inflate(mat, matvar->internal->z, tag, 4, &bytesread) ) { return bytesread; @@ -501,6 +501,7 @@ ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v } #endif } else { + mat_uint32_t tag[2] = {0, 0}; if ( 0 != Read(tag, 4, 1, (FILE *)mat->fp, &bytesread) ) { return bytesread; } @@ -524,9 +525,8 @@ ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v *n = N / 4; *v = (mat_uint32_t *)calloc(N, 1); if ( NULL != *v ) { - int nBytes; if ( matvar->compression == MAT_COMPRESSION_NONE ) { - nBytes = ReadUInt32Data(mat, *v, packed_type, *n); + int nBytes = ReadUInt32Data(mat, *v, packed_type, *n); /* * If the data was in the tag we started on a 4-byte * boundary so add 4 to make it an 8-byte @@ -538,7 +538,7 @@ ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v (void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); #if HAVE_ZLIB } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { - nBytes = ReadCompressedUInt32Data(mat, matvar->internal->z, *v, packed_type, *n); + int nBytes = ReadCompressedUInt32Data(mat, matvar->internal->z, *v, packed_type, *n); /* * If the data was in the tag we started on a 4-byte * boundary so add 4 to make it an 8-byte @@ -629,7 +629,7 @@ GetMatrixMaxBufSize(matvar_t *matvar, size_t *size) mat_t * Mat_Create5(const char *matname, const char *hdr_str) { - FILE *fp = NULL; + FILE *fp; mat_int16_t endian = 0, version; mat_t *mat = NULL; size_t err; @@ -640,6 +640,8 @@ Mat_Create5(const char *matname, const char *hdr_str) if ( NULL != wname ) { fp = _wfopen(wname, L"w+b"); free(wname); + } else { + fp = NULL; } #else fp = fopen(matname, "w+b"); @@ -984,7 +986,7 @@ WriteCompressedData(mat_t *mat, z_streamp z, void *data, int N, enum matio_types static size_t ReadNextCell(mat_t *mat, matvar_t *matvar) { - size_t bytesread = 0, i; + size_t bytesread = 0; int err; matvar_t **cells = NULL; size_t nelems = 1; @@ -1011,6 +1013,7 @@ ReadNextCell(mat_t *mat, matvar_t *matvar) if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { #if HAVE_ZLIB + size_t i; mat_uint32_t uncomp_buf[16]; mat_uint32_t nBytes; mat_uint32_t array_flags; @@ -1233,13 +1236,13 @@ ReadNextCell(mat_t *mat, matvar_t *matvar) #endif } else { + size_t i; mat_uint32_t buf[6] = {0, 0, 0, 0, 0, 0}; mat_uint32_t nBytes; mat_uint32_t array_flags; for ( i = 0; i < nelems; i++ ) { size_t nbytes = 0; - mat_uint32_t name_len; cells[i] = Mat_VarCalloc(); if ( NULL == cells[i] ) { Mat_Critical("Couldn't allocate memory for cell %zu", i); @@ -1329,11 +1332,10 @@ ReadNextCell(mat_t *mat, matvar_t *matvar) (void)Mat_uint32Swap(buf); (void)Mat_uint32Swap(buf + 1); } - name_len = 0; if ( buf[1] > 0 ) { /* Name of variable */ if ( buf[0] == MAT_T_INT8 ) { /* Name not in tag */ - name_len = buf[1]; + mat_uint32_t name_len = buf[1]; if ( name_len % 8 > 0 ) { if ( name_len < UINT32_MAX - 8 + (name_len % 8) ) { name_len = name_len + 8 - (name_len % 8); @@ -1375,9 +1377,8 @@ ReadNextCell(mat_t *mat, matvar_t *matvar) static size_t ReadNextStructField(mat_t *mat, matvar_t *matvar) { - mat_uint32_t fieldname_size; int err; - size_t bytesread = 0, nfields, i; + size_t bytesread = 0; matvar_t **fields = NULL; size_t nelems = 1, nelems_x_nfields; @@ -1388,8 +1389,9 @@ ReadNextStructField(mat_t *mat, matvar_t *matvar) } if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { #if HAVE_ZLIB + size_t nfields, i; mat_uint32_t uncomp_buf[16]; - mat_uint32_t array_flags, len; + mat_uint32_t array_flags, len, fieldname_size; memset(&uncomp_buf, 0, sizeof(uncomp_buf)); /* Field name length */ @@ -1633,7 +1635,7 @@ ReadNextStructField(mat_t *mat, matvar_t *matvar) else if ( nBytes <= (1 << MAX_WBITS) ) { /* Memory optimization: Read data if less in size than the zlib inflate state (approximately) */ - err = Mat_VarRead5(mat, fields[i]); + /* err = */ Mat_VarRead5(mat, fields[i]); fields[i]->internal->data = fields[i]->data; fields[i]->data = NULL; } @@ -1662,8 +1664,9 @@ ReadNextStructField(mat_t *mat, matvar_t *matvar) Mat_Critical("Not compiled with zlib support"); #endif } else { + size_t nfields, i; mat_uint32_t buf[6] = {0, 0, 0, 0, 0, 0}; - mat_uint32_t array_flags, len; + mat_uint32_t array_flags, len, fieldname_size; err = Read(buf, 4, 2, (FILE *)mat->fp, &bytesread); if ( err ) { @@ -2000,7 +2003,7 @@ WriteType(mat_t *mat, matvar_t *matvar) case MAT_C_INT8: case MAT_C_UINT8: { if ( matvar->isComplex ) { - mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; + const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; if ( NULL == matvar->data ) complex_data = &null_complex_data; @@ -2025,7 +2028,7 @@ WriteType(mat_t *mat, matvar_t *matvar) if ( matvar->data_type == MAT_T_UTF8 ) { nelems = matvar->nbytes; } - nBytes = WriteCharData(mat, matvar->data, nelems, matvar->data_type); + /* nBytes = */ WriteCharData(mat, matvar->data, nelems, matvar->data_type); break; case MAT_C_CELL: { size_t i; @@ -2844,8 +2847,8 @@ Mat_WriteCompressedEmptyVariable5(mat_t *mat, const char *name, int rank, const memset(uncomp_buf, 0, 8); uncomp_buf[0] = (array_name_len << 16) | array_name_type; memcpy(uncomp_buf + 1, name, array_name_len); - if ( array_name_len % 4 ) - array_name_len += 4 - (array_name_len % 4); + /* if ( array_name_len % 4 ) */ + /* array_name_len += 4 - (array_name_len % 4); */ z->next_in = ZLIB_BYTE_PTR(uncomp_buf); z->avail_in = 8; @@ -4888,7 +4891,7 @@ Mat_VarWrite5(mat_t *mat, matvar_t *matvar, int compress) int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; int array_flags_size = 8, matrix_type = MAT_T_MATRIX; const mat_uint32_t pad4 = 0; - int nBytes, i, nzmax = 0; + int nBytes, nzmax = 0; mat_off_t start = 0, end = 0; if ( NULL == mat ) @@ -4905,6 +4908,7 @@ Mat_VarWrite5(mat_t *mat, matvar_t *matvar, int compress) #else { #endif + int i; fwrite(&matrix_type, 4, 1, (FILE *)mat->fp); fwrite(&pad4, 4, 1, (FILE *)mat->fp); start = ftello((FILE *)mat->fp); @@ -4973,8 +4977,8 @@ Mat_VarWrite5(mat_t *mat, matvar_t *matvar, int compress) } else if ( compress == MAT_COMPRESSION_ZLIB ) { mat_uint32_t comp_buf[512]; mat_uint32_t uncomp_buf[512]; - int buf_size = 512, err; - size_t byteswritten = 0, matrix_max_buf_size; + int buf_size = 512, err, i; + size_t matrix_max_buf_size; z_streamp z; z = (z_streamp)calloc(1, sizeof(*z)); @@ -5021,8 +5025,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); - byteswritten += - fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); + 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; uncomp_buf[1] = array_flags_size; @@ -5048,8 +5051,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); - byteswritten += - fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); + fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); /* Name of variable */ if ( strlen(matvar->name) <= 4 ) { @@ -5059,8 +5061,8 @@ Mat_VarWrite5(mat_t *mat, matvar_t *matvar, int compress) memset(uncomp_buf, 0, 8); uncomp_buf[0] = (array_name_len << 16) | array_name_type; memcpy(uncomp_buf + 1, matvar->name, array_name_len); - if ( array_name_len % 4 ) - array_name_len += 4 - (array_name_len % 4); + /* if ( array_name_len % 4 ) */ + /* array_name_len += 4 - (array_name_len % 4); */ z->next_in = ZLIB_BYTE_PTR(uncomp_buf); z->avail_in = 8; @@ -5068,8 +5070,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); - byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, - (FILE *)mat->fp); + fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); } else { mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); @@ -5087,8 +5088,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); - byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, - (FILE *)mat->fp); + fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( z->avail_out == 0 ); } if ( NULL != matvar->internal ) { @@ -5109,8 +5109,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); err = deflate(z, Z_FINISH); - byteswritten += - fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); + fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); } while ( err != Z_STREAM_END && z->avail_out == 0 ); #if 0 if ( byteswritten % 8 ) @@ -5146,7 +5145,7 @@ matvar_t * Mat_VarReadNextInfo5(mat_t *mat) { int err; - mat_uint32_t data_type, array_flags, nBytes; + mat_uint32_t data_type, nBytes; mat_off_t fpos; matvar_t *matvar = NULL; @@ -5178,7 +5177,6 @@ Mat_VarReadNextInfo5(mat_t *mat) case MAT_T_COMPRESSED: { #if HAVE_ZLIB mat_uint32_t uncomp_buf[16]; - int nbytes; size_t bytesread = 0; memset(&uncomp_buf, 0, sizeof(uncomp_buf)); @@ -5200,11 +5198,15 @@ Mat_VarReadNextInfo5(mat_t *mat) /* Read variable tag */ err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); + if ( err ) { + Mat_VarFree(matvar); + matvar = NULL; + break; + } if ( mat->byteswap ) { (void)Mat_uint32Swap(uncomp_buf); (void)Mat_uint32Swap(uncomp_buf + 1); } - nbytes = uncomp_buf[1]; if ( uncomp_buf[0] != MAT_T_MATRIX ) { (void)fseeko((FILE *)mat->fp, (mat_off_t)(nBytes - bytesread), SEEK_CUR); Mat_VarFree(matvar); @@ -5226,7 +5228,7 @@ Mat_VarReadNextInfo5(mat_t *mat) } /* Array flags */ if ( uncomp_buf[0] == MAT_T_UINT32 ) { - array_flags = uncomp_buf[2]; + mat_uint32_t array_flags = uncomp_buf[2]; matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); matvar->isComplex = (array_flags & MAT_F_COMPLEX); matvar->isGlobal = (array_flags & MAT_F_GLOBAL); @@ -5262,7 +5264,7 @@ Mat_VarReadNextInfo5(mat_t *mat) if ( uncomp_buf[0] == MAT_T_INT32 ) { int j; size_t size; - nbytes = uncomp_buf[1]; + int nbytes = uncomp_buf[1]; matvar->rank = nbytes / 4; if ( 0 == do_clean && matvar->rank > 13 ) { int rank = matvar->rank; @@ -5397,7 +5399,7 @@ Mat_VarReadNextInfo5(mat_t *mat) /* Array flags */ if ( buf[0] == MAT_T_UINT32 || buf[0] == MAT_T_INT32 ) { /* Also allow INT32 for SWAN */ - array_flags = buf[2]; + mat_uint32_t array_flags = buf[2]; matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); matvar->isComplex = (array_flags & MAT_F_COMPLEX); matvar->isGlobal = (array_flags & MAT_F_GLOBAL); diff --git a/src/matio.h b/src/matio.h index 06d877eb..8a805258 100644 --- a/src/matio.h +++ b/src/matio.h @@ -331,8 +331,8 @@ EXTERN matvar_t *Mat_VarGetStructsLinear(const matvar_t *matvar, int start, int int copy_fields); EXTERN void Mat_VarPrint(const matvar_t *matvar, int printdata); EXTERN matvar_t *Mat_VarRead(mat_t *mat, const char *name); -EXTERN int Mat_VarReadData(mat_t *mat, matvar_t *matvar, void *data, int *start, int *stride, - int *edge); +EXTERN int Mat_VarReadData(mat_t *mat, matvar_t *matvar, void *data, const int *start, + const int *stride, const int *edge); EXTERN int Mat_VarReadDataAll(mat_t *mat, matvar_t *matvar); EXTERN int Mat_VarReadDataLinear(mat_t *mat, matvar_t *matvar, void *data, int start, int stride, int edge); @@ -350,8 +350,8 @@ EXTERN matvar_t *Mat_VarSetStructFieldByName(matvar_t *matvar, const char *field EXTERN int Mat_VarWrite(mat_t *mat, matvar_t *matvar, enum matio_compression compress); EXTERN int Mat_VarWriteAppend(mat_t *mat, matvar_t *matvar, enum matio_compression compress, int dim); -EXTERN int Mat_VarWriteInfo(mat_t *mat, matvar_t *matvar); -EXTERN int Mat_VarWriteData(mat_t *mat, matvar_t *matvar, void *data, const int *start, +EXTERN int Mat_VarWriteInfo(const mat_t *mat, matvar_t *matvar); +EXTERN int Mat_VarWriteData(const mat_t *mat, matvar_t *matvar, void *data, const int *start, const int *stride, const int *edge); /* Other functions */ diff --git a/test/test_snprintf.c b/test/test_snprintf.c index 704bd9d9..818da896 100644 --- a/test/test_snprintf.c +++ b/test/test_snprintf.c @@ -15,7 +15,7 @@ int main(int argc, char **argv) { char buf1[1024]; char buf2[1024]; - char *fp_fmt[] = { + const char *fp_fmt[] = { "%1.1f", "%-1.5f", "%1.5f", @@ -33,10 +33,10 @@ int main(int argc, char **argv) "-16.16f", NULL }; - double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996, + const double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996, 0.9996, 1.996, 4.136, 5.030201, 0.00205, /* END LIST */ 0}; - char *int_fmt[] = { + const char *int_fmt[] = { "%-1.5d", "%1.5d", "%123.9d", @@ -49,8 +49,8 @@ int main(int argc, char **argv) "%d", NULL }; - long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; - char *str_fmt[] = { + const long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; + const char *str_fmt[] = { "10.5s", "5.10s", "10.1s", @@ -63,7 +63,7 @@ int main(int argc, char **argv) "%10s", NULL }; - char *str_vals[] = {"hello", "a", "", "a longer string", NULL}; + const char *str_vals[] = {"hello", "a", "", "a longer string", NULL}; int x, y; int fail = 0; int num = 0; diff --git a/tools/matdump.c b/tools/matdump.c index 042ed5e3..b8a8c3d7 100644 --- a/tools/matdump.c +++ b/tools/matdump.c @@ -85,10 +85,10 @@ static int human_readable = 0; static int print_whos_first = 1; /* Print Functions */ -static void print_whos(matvar_t *matvar); -static void print_default(matvar_t *matvar); +static void print_whos(const matvar_t *matvar); +static void print_default(const matvar_t *matvar); -static void (*printfunc)(matvar_t *matvar) = NULL; +static void (*printfunc)(const matvar_t *matvar) = NULL; static char * get_next_token(char *str) @@ -113,7 +113,7 @@ get_next_token(char *str) } static int -slab_get_rank(char *open, char *close) +slab_get_rank(char *open, const char *close) { int rank = 0; char *ptr = open + 1; @@ -126,15 +126,12 @@ slab_get_rank(char *open, char *close) } static void -slab_get_select(char *open, char *close, int rank, int *start, int *stride, int *edge) +slab_get_select(char *open, const char *close, int rank, int *start, int *stride, int *edge) { - char *ptr, *valptr; - int nvals, dim, i; - - ptr = open; - valptr = open + 1; - dim = 0; - nvals = 0; + char *ptr = open; + const char *valptr = open + 1; + int dim = 0; + int nvals = 0; do { ptr++; if ( *ptr == ',' ) { @@ -143,16 +140,14 @@ slab_get_select(char *open, char *close, int rank, int *start, int *stride, int if ( !strcmp(valptr, "end") ) { edge[dim] = -1; } else { - i = (int)strtol(valptr, NULL, 10); - edge[dim] = i; + edge[dim] = (int)strtol(valptr, NULL, 10); } } else if ( nvals == 1 ) { *ptr = '\0'; if ( !strcmp(valptr, "end") ) { edge[dim] = -1; } else { - i = (int)strtol(valptr, NULL, 10); - edge[dim] = i; + edge[dim] = (int)strtol(valptr, NULL, 10); } } else if ( nvals == 0 ) { *ptr = '\0'; @@ -160,7 +155,7 @@ slab_get_select(char *open, char *close, int rank, int *start, int *stride, int start[dim] = -1; edge[dim] = -1; } else { - i = (int)strtol(valptr, NULL, 10); + int i = (int)strtol(valptr, NULL, 10); start[dim] = i - 1; edge[dim] = i; } @@ -180,7 +175,7 @@ slab_get_select(char *open, char *close, int rank, int *start, int *stride, int else fprintf(stderr, "Too many inputs to dim %d", dim + 1); } else { - i = (int)strtol(valptr, NULL, 10); + int i = (int)strtol(valptr, NULL, 10); if ( nvals == 0 ) start[dim] = i - 1; else if ( nvals == 1 ) @@ -205,7 +200,7 @@ slab_get_select(char *open, char *close, int rank, int *start, int *stride, int else fprintf(stderr, "Too many inputs to dim %d", dim + 1); } else { - i = (int)strtol(valptr, NULL, 10); + int i = (int)strtol(valptr, NULL, 10); if ( nvals == 0 ) { start[dim] = i - 1; edge[dim] = i; @@ -317,7 +312,7 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) { char *next_tok_pos, next_tok = 0; char *open = NULL, *close = NULL; - int err = 1, i = 0, j, done = 0; + int err = 1, j, done = 0; matvar_t *matvar = *_matvar; next_tok_pos = get_next_token(index_str); @@ -385,6 +380,7 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) matvar->dims[1] = 1; } } else { + int i; err = Mat_VarReadData(mat, matvar, matvar->data, start, stride, edge); for ( i = 0; i < rank; i++ ) matvar->dims[i] = (size_t)edge[i]; @@ -395,9 +391,7 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) free(edge); } else if ( next_tok == '.' ) { matvar_t *field; - char *varname; - - varname = next_tok_pos + 1; + const char *varname = next_tok_pos + 1; if ( matvar->class_type == MAT_C_STRUCT ) { next_tok_pos = get_next_token(next_tok_pos + 1); if ( next_tok_pos != varname ) { @@ -421,7 +415,7 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) } } else if ( matvar->class_type == MAT_C_CELL ) { int ncells; - matvar_t *cell, **cells; + matvar_t **cells; ncells = matvar->nbytes / matvar->data_size; cells = (matvar_t **)matvar->data; @@ -433,7 +427,7 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) done = 1; } for ( j = 0; j < ncells; j++ ) { - cell = Mat_VarGetCell(matvar, j); + matvar_t *cell = Mat_VarGetCell(matvar, j); if ( cell == NULL || cell->class_type != MAT_C_STRUCT ) { fprintf(stderr, "cell index %d is not a structure", j); break; @@ -500,6 +494,7 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) matvar->dims[0] = *edge; } } else { + int i; cells = Mat_VarGetCells(matvar, start, stride, edge); for ( i = 0; i < rank; i++ ) matvar->dims[i] = (size_t)edge[i]; @@ -530,13 +525,9 @@ read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str) } static void -print_whos(matvar_t *matvar) +print_whos(const matvar_t *matvar) { - int i; size_t nbytes; - char size[32] = { - '\0', - }; if ( print_whos_first ) { printf("%-20s %-10s %-10s %-18s\n\n", "Name", "Size", "Bytes", "Class"); @@ -544,12 +535,16 @@ print_whos(matvar_t *matvar) } printf("%-20s", matvar->name); if ( matvar->rank > 0 ) { - int cnt = 0; + int cnt = 0, i; + char size[32] = { + '\0', + }; printf("%8" SIZE_T_FMTSTR, matvar->dims[0]); for ( i = 1; i < matvar->rank; i++ ) { if ( ceil(log10((double)matvar->dims[i])) + 1 < 32 ) { - cnt += mat_snprintf(size + cnt, sizeof(size) - cnt, "x%" SIZE_T_FMTSTR, matvar->dims[i]); - if (cnt >= sizeof(size)) { + cnt += mat_snprintf(size + cnt, sizeof(size) - cnt, "x%" SIZE_T_FMTSTR, + matvar->dims[i]); + if ( cnt >= sizeof(size) ) { break; } } @@ -587,7 +582,7 @@ default_printf_func(int log_level, const char *message) } static void -print_default_number(enum matio_types type, void *data) +print_default_number(enum matio_types type, const void *data) { switch ( type ) { case MAT_T_DOUBLE: @@ -642,7 +637,7 @@ print_default_number(enum matio_types type, void *data) } static void -print_default_numeric_2d(matvar_t *matvar) +print_default_numeric_2d(const matvar_t *matvar) { size_t i, j, stride; @@ -651,9 +646,9 @@ print_default_numeric_2d(matvar_t *matvar) stride = Mat_SizeOf(matvar->data_type); if ( matvar->isComplex ) { - mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; - char *rp = (char *)complex_data->Re; - char *ip = (char *)complex_data->Im; + const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; + const char *rp = (char *)complex_data->Re; + const char *ip = (char *)complex_data->Im; for ( i = 0; i < matvar->dims[0]; i++ ) { for ( j = 0; j < matvar->dims[1]; j++ ) { size_t idx = matvar->dims[0] * j + i; @@ -678,7 +673,7 @@ print_default_numeric_2d(matvar_t *matvar) } static void -print_default_numeric_3d(matvar_t *matvar) +print_default_numeric_3d(const matvar_t *matvar) { size_t i, j, k, l, stride; @@ -687,9 +682,9 @@ print_default_numeric_3d(matvar_t *matvar) stride = Mat_SizeOf(matvar->data_type); if ( matvar->isComplex ) { - mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; - char *rp = (char *)complex_data->Re; - char *ip = (char *)complex_data->Im; + const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; + const char *rp = (char *)complex_data->Re; + const char *ip = (char *)complex_data->Im; for ( k = 0; k < matvar->dims[2]; k++ ) { Mat_Message("%s(:,:,%zu) = ", matvar->name, k); indent++; @@ -709,7 +704,7 @@ print_default_numeric_3d(matvar_t *matvar) printf("\n"); } } else { - char *data = (char *)matvar->data; + const char *data = (char *)matvar->data; for ( k = 0; k < matvar->dims[2]; k++ ) { Mat_Message("%s(:,:,%zu) = ", matvar->name, k); indent++; @@ -730,7 +725,7 @@ print_default_numeric_3d(matvar_t *matvar) } static void -print_default(matvar_t *matvar) +print_default(const matvar_t *matvar) { if ( NULL == matvar ) return; @@ -785,11 +780,12 @@ print_default(matvar_t *matvar) Mat_Message("Fields[%d] {", nfields); indent++; { - size_t j; matvar_t **fields = (matvar_t **)matvar->data; - if ( NULL != fields ) + if ( NULL != fields ) { + size_t j; for ( j = 0; j < nfields * nmemb; j++ ) print_default(fields[j]); + } } indent--; Mat_Message("}"); @@ -812,11 +808,12 @@ print_default(matvar_t *matvar) Mat_Message("{"); indent++; { - size_t j; matvar_t **cells = (matvar_t **)matvar->data; - if ( NULL != cells ) + if ( NULL != cells ) { + size_t j; for ( j = 0; j < ncells; j++ ) print_default(cells[j]); + } } indent--; Mat_Message("}"); @@ -831,7 +828,7 @@ int main(int argc, char *argv[]) { const char *prog_name = "matdump"; - int i, c, err = EXIT_SUCCESS; + int c, err = EXIT_SUCCESS; mat_t *mat; matvar_t *matvar; int version[3]; @@ -901,6 +898,7 @@ main(int argc, char *argv[]) optind++; if ( optind < argc ) { + int i; /* variables specified on the command line */ for ( i = optind; i < argc; i++ ) { char *next_tok_pos, next_tok = 0;