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 9d4d669
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
29 changes: 18 additions & 11 deletions src/mat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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. "
Expand All @@ -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. "
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
Loading

0 comments on commit 9d4d669

Please sign in to comment.