Skip to content

Commit

Permalink
Fix issues reported by cppcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed Feb 25, 2024
1 parent 34b4ddf commit 9c2078a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
9 changes: 5 additions & 4 deletions src/mat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2465,7 +2465,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;

Expand Down Expand Up @@ -2842,7 +2843,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. "
Expand All @@ -2867,8 +2868,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. "
Expand Down
26 changes: 11 additions & 15 deletions src/mat4.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@
mat_t *
Mat_Create4(const char *matname)
{
FILE *fp = NULL;
FILE *fp;
mat_t *mat = NULL;

#if defined(_WIN32)
wchar_t *wname = utf82u(matname);
if ( NULL != wname ) {
fp = _wfopen(wname, L"w+b");
free(wname);
} else {
fp = NULL;
}
#else
fp = fopen(matname, "w+b");
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 {
Expand All @@ -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 )
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 18 additions & 11 deletions src/mat5.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1233,6 +1236,7 @@ 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;
Expand Down Expand Up @@ -1375,9 +1379,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;

Expand All @@ -1388,6 +1391,7 @@ ReadNextStructField(mat_t *mat, matvar_t *matvar)
}
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
#if HAVE_ZLIB
size_t nfields, i mat_uint32_t fieldname_size;
mat_uint32_t uncomp_buf[16];
mat_uint32_t array_flags, len;

Expand Down Expand Up @@ -1662,6 +1666,8 @@ ReadNextStructField(mat_t *mat, matvar_t *matvar)
Mat_Critical("Not compiled with zlib support");
#endif
} else {
size_t nfields, i;
mat_uint32_t fieldname_size;
mat_uint32_t buf[6] = {0, 0, 0, 0, 0, 0};
mat_uint32_t array_flags, len;

Expand Down Expand Up @@ -4888,7 +4894,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 )
Expand All @@ -4905,6 +4911,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);
Expand Down Expand Up @@ -5146,7 +5153,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;

Expand Down Expand Up @@ -5397,7 +5404,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);
Expand Down
8 changes: 4 additions & 4 deletions src/matio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down
12 changes: 6 additions & 6 deletions test/test_snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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;
Expand Down

0 comments on commit 9c2078a

Please sign in to comment.