diff --git a/libcommon/minc2_error.c b/libcommon/minc2_error.c index 49e2ad88..4e423d02 100644 --- a/libcommon/minc2_error.c +++ b/libcommon/minc2_error.c @@ -86,3 +86,4 @@ /*All code moved to minc_error*/ +typedef int minc2_error_empty_unit; diff --git a/libcommon/minc_error.c b/libcommon/minc_error.c index 1a933c9f..21babeaf 100644 --- a/libcommon/minc_error.c +++ b/libcommon/minc_error.c @@ -97,7 +97,7 @@ int v_mi2log_message(const char *file,int line, mimsgcode_t code, va_list ap); struct mierror_entry { int level; - char *msgfmt; + const char *msgfmt; }; @@ -115,7 +115,7 @@ static struct { /* MINC routine name variable, call depth counter (for keeping track of minc routines calling minc routines) and variable for keeping track of callers ncopts. All of these are for error logging. */ -static char *minc_routine_name = "MINC"; +static const char *minc_routine_name = "MINC"; static int minc_call_depth = 0; static int minc_trash_var = 0; @@ -232,7 +232,7 @@ static struct mierror_entry mierror_table[] = { }; -int MI_save_routine_name(char *name) +int MI_save_routine_name(const char *name) { /* no idea what peter was up to here */ /* minc_trash_var = (((minc_call_depth++)==0) ? MI_save_routine_name(name) : * MI_NOERROR)) */ @@ -264,23 +264,23 @@ int MI_return_error(void) } return( TRUE ); } -void MI_log_pkg_error2(int p1, char *p2) +void MI_log_pkg_error2(int p1, const char *p2) { (void) fprintf(stderr, "%s: ", minc_routine_name); (void) fprintf(stderr, "%s", p2); (void) fputc('\n', stderr); (void) fflush(stderr); } -void MI_log_pkg_error3(int p1, char *p2, char *p3) +void MI_log_pkg_error3(int p1, const char *p2, const char *p3) { (void) fprintf(stderr, "%s: ", minc_routine_name); (void) fprintf(stderr, p2, p3); (void) fputc('\n', stderr); (void) fflush(stderr); } -void MI_log_sys_error1(char *p1) +void MI_log_sys_error1(const char *p1) { - char *message; + const char *message; int errnum = errno; (void) fprintf(stderr, "%s", minc_routine_name); @@ -311,7 +311,13 @@ void milog_init(const char *name) const char *fname_str = miget_cfg_str(MICFG_LOGFILE); int level = miget_cfg_int(MICFG_LOGLEVEL); - if (!strlen(fname_str)) { + /* Close previously opened log file if re-initializing */ + if (_MI_log.fp != NULL && _MI_log.fp != stderr && _MI_log.fp != stdout) { + fclose(_MI_log.fp); + _MI_log.fp = NULL; + } + + if (fname_str == NULL || !strlen(fname_str)) { _MI_log.fp = stderr; } else if (!strcmp(fname_str, "stdout") || !strcmp(fname_str, "-")) { @@ -324,6 +330,10 @@ void milog_init(const char *name) else { _MI_log.fp = fopen(fname_str, "w"); } + /* Fall back to stderr if fopen failed */ + if (_MI_log.fp == NULL) { + _MI_log.fp = stderr; + } } if (level != 0) { @@ -381,7 +391,6 @@ int v_mi2log_message(const char *file, int line, mimsgcode_t code, va_list ap) } fprintf ( _MI2_log.fp, "%s:%d (from %s): ", file, line, minc_routine_name ); vfprintf ( _MI2_log.fp, fmt, ap ); - va_end ( ap ); fprintf ( _MI2_log.fp, "\n" ); fflush ( _MI2_log.fp ); } @@ -398,7 +407,7 @@ int v_mi2log_message(const char *file, int line, mimsgcode_t code, va_list ap) /*MINC2 error reporting*/ -int MI2_save_routine_name ( char *name ) +int MI2_save_routine_name ( const char *name ) { return MI_save_routine_name(name); } @@ -413,17 +422,17 @@ int MI2_return_error ( void ) return MI_return_error(); } -void MI2_log_pkg_error2 ( int p1, char *p2 ) +void MI2_log_pkg_error2 ( int p1, const char *p2 ) { MI_log_pkg_error2(p1,p2); } -void MI2_log_pkg_error3 ( int p1, char *p2, char *p3 ) +void MI2_log_pkg_error3 ( int p1, const char *p2, const char *p3 ) { MI_log_pkg_error3(p1,p2,p3); } -void MI2_log_sys_error1 ( char *p1 ) +void MI2_log_sys_error1 ( const char *p1 ) { MI_log_sys_error1(p1); } diff --git a/libcommon/minc_error.h b/libcommon/minc_error.h index e5232ab2..8ce6d6ca 100644 --- a/libcommon/minc_error.h +++ b/libcommon/minc_error.h @@ -177,21 +177,21 @@ int milog_set_verbosity(int); int milog_message(mimsgcode_t code, ...); int mi2log_message(const char *file,int line, mimsgcode_t code, ...); -int MI2_save_routine_name(char *name); +int MI2_save_routine_name(const char *name); int MI2_return(void); int MI2_return_error(void); -void MI2_log_pkg_error2(int p1, char *p2); -void MI2_log_pkg_error3(int p1, char *p2, char *p3); -void MI2_log_sys_error1(char *p1); +void MI2_log_pkg_error2(int p1, const char *p2); +void MI2_log_pkg_error3(int p1, const char *p2, const char *p3); +void MI2_log_sys_error1(const char *p1); void mi2log_init(const char *name); int mi2log_set_verbosity ( int lvl ); -int MI_save_routine_name(char *name); +int MI_save_routine_name(const char *name); int MI_return(void); int MI_return_error(void); -void MI_log_pkg_error2(int p1, char *p2); -void MI_log_pkg_error3(int p1, char *p2, char *p3); -void MI_log_sys_error1(char *p1); +void MI_log_pkg_error2(int p1, const char *p2); +void MI_log_pkg_error3(int p1, const char *p2, const char *p3); +void MI_log_sys_error1(const char *p1); #ifdef __cplusplus } diff --git a/libcommon/time_stamp.c b/libcommon/time_stamp.c index 22811fb6..5970d5e8 100644 --- a/libcommon/time_stamp.c +++ b/libcommon/time_stamp.c @@ -114,6 +114,9 @@ char *time_stamp(int argc, char *argv[]) length += 2; /* we will need quotes! */ } str = malloc(length); + if (str == NULL) { + return NULL; + } /* Copy the time and separator */ (void) strcpy(str, the_time); diff --git a/libsrc/hdf_convenience.c b/libsrc/hdf_convenience.c index 2d0141c1..28d4e351 100644 --- a/libsrc/hdf_convenience.c +++ b/libsrc/hdf_convenience.c @@ -279,7 +279,7 @@ hdf_dim_add(struct m2_file *file, const char *name, long length) static int hdf_is_dimension_name(struct m2_file *file, const char *varnm) { - static char *dimnms[MI2_STD_DIM_COUNT] = { + static const char *dimnms[MI2_STD_DIM_COUNT] = { MIxspace, MIyspace, MIzspace, @@ -356,6 +356,7 @@ hdf_get_diminfo(hid_t dst_id, int *ndims, hsize_t dims[]) spc_id = H5Dget_space(dst_id); if (spc_id < 0) { + *ndims = 0; MI_LOG_ERROR(MI_MSG_SNH); } else { @@ -2135,7 +2136,7 @@ herr_t hdf_copy_attr(hid_t in_id, const char *attr_name, void *op_data) } static int -hdf_open_dsets(struct m2_file *file, hid_t grp_id, char *cpath, int is_dim) +hdf_open_dsets(struct m2_file *file, hid_t grp_id, const char *cpath, int is_dim) { hsize_t nobjs; hsize_t idx; @@ -2273,7 +2274,8 @@ hdf_open(const char *path, int mode) /* OK, it's compound type. */ struct m2_dim *dim = hdf_dim_add(file, MIvector_dimension, H5Tget_nmembers(type_id)); - dim->is_fake = 1; + if (dim != NULL) + dim->is_fake = 1; dims[ndims++] = H5Tget_nmembers(type_id); is_compound = 1; } @@ -2283,7 +2285,8 @@ hdf_open(const char *path, int mode) var = hdf_var_add(file, MIimage, "/minc-2.0/image/0/image", ndims, dims); - var->is_cmpd = is_compound; + if (var != NULL) + var->is_cmpd = is_compound; H5Dclose(dset_id); } diff --git a/libsrc/image_conversion.c b/libsrc/image_conversion.c index e1642db0..8c80a3fe 100644 --- a/libsrc/image_conversion.c +++ b/libsrc/image_conversion.c @@ -176,7 +176,7 @@ /* Private functions */ PRIVATE int MI_icv_get_type(mi_icv_type *icvp, int cdfid, int varid); PRIVATE int MI_icv_get_vrange(mi_icv_type *icvp, int cdfid, int varid); -PRIVATE double MI_get_default_range(char *what, nc_type datatype, int sign); +PRIVATE double MI_get_default_range(const char *what, nc_type datatype, int sign); PRIVATE int MI_icv_get_norm(mi_icv_type *icvp, int cdfid, int varid); PRIVATE int MI_icv_access(int operation, mi_icv_type *icvp, long start[], long count[], void *values); @@ -1094,7 +1094,7 @@ PRIVATE int MI_icv_get_vrange(mi_icv_type *icvp, int cdfid, int varid) @CREATED : August 10, 1992 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ -PRIVATE double MI_get_default_range(char *what, nc_type datatype, int sign) +PRIVATE double MI_get_default_range(const char *what, nc_type datatype, int sign) { double range[2]; diff --git a/libsrc/minc.h b/libsrc/minc.h index 7eec6448..bb759e1d 100644 --- a/libsrc/minc.h +++ b/libsrc/minc.h @@ -505,7 +505,7 @@ MNCAPI int miopen(const char *path, int mode); MNCAPI int micreate(const char *path, int cmode); MNCAPI int miclose(int cdfid); MNCAPI int miattget_with_sign(int cdfid, int varid, const char *name, - char *insign, nc_type datatype, char *outsign, + const char *insign, nc_type datatype, char *outsign, int max_length, void *value, int *att_length); MNCAPI int miattget(int cdfid, int varid, const char *name, nc_type datatype, int max_length, void *value, int *att_length); diff --git a/libsrc/minc_convenience.c b/libsrc/minc_convenience.c index 271fb2b3..6a190f71 100644 --- a/libsrc/minc_convenience.c +++ b/libsrc/minc_convenience.c @@ -310,7 +310,7 @@ MNCAPI int miget_valid_range(int cdfid, int imgid, double valid_range[]) int length; nc_type datatype; int is_signed; - char *att_sign; + const char *att_sign; double temp; MI_SAVE_ROUTINE_NAME("miget_valid_range"); @@ -408,7 +408,7 @@ MNCAPI int miset_valid_range(int cdfid, int imgid, const double valid_range[]) nc_type datatype; int is_signed; int status; - char *attname; + const char *attname; float fval[2]; MI_SAVE_ROUTINE_NAME("miset_valid_range"); @@ -678,7 +678,7 @@ MNCAPI int miattget_pointer(int cdfid, int varid, const char *name) /* Character string to hold attribute */ char pointer_string[MAX_NC_NAME+sizeof(MI_VARATT_POINTER_PREFIX)]; int index; /* Index into string */ - char *prefix_string=MI_VARATT_POINTER_PREFIX; /* Prefix string */ + const char *prefix_string=MI_VARATT_POINTER_PREFIX; /* Prefix string */ int ptrvarid; /* Id of variable pointed to by name */ MI_SAVE_ROUTINE_NAME("miattget_pointer"); diff --git a/libsrc/minc_simple.c b/libsrc/minc_simple.c index 21817823..70f449bd 100644 --- a/libsrc/minc_simple.c +++ b/libsrc/minc_simple.c @@ -49,7 +49,7 @@ #define MI_S_X 3 #define MI_S_NDIMS 4 -static char *minc_dimnames[] = { +static const char *minc_dimnames[] = { MItime, MIzspace, MIyspace, @@ -83,7 +83,7 @@ struct file_info { }; static int -minc_simple_to_nc_type(int minctype, nc_type *nctype, char **signstr) +minc_simple_to_nc_type(int minctype, nc_type *nctype, const char **signstr) { switch (minctype) { case MINC_TYPE_CHAR: @@ -215,7 +215,7 @@ minc_load_data(char *path, void *dataptr, int datatype, { int fd; /* MINC file descriptor */ nc_type nctype; /* netCDF type */ - char *signstr; /* MI_SIGNED or MI_UNSIGNED */ + const char *signstr; /* MI_SIGNED or MI_UNSIGNED */ int length; int dim_id[MI_S_NDIMS]; long dim_len[MI_S_NDIMS]; @@ -534,7 +534,7 @@ minc_save_start(char *path, /* Path to the file */ struct var_info *p_var; struct att_info *p_att; int var_id; /* netCDF ID for variable */ - char *signstr; + const char *signstr; nc_type nctype; old_ncopts =get_ncopts(); @@ -788,7 +788,7 @@ minc_save_data(int fd, void *dataptr, int datatype, long ct, long cz, long cy, long cx) { nc_type nctype; - char *signstr; + const char *signstr; int i; int var_id; int var_ndims; @@ -951,7 +951,7 @@ minc_get_world_transform(int fd, double transform[4][4], int spatial_axes[3]) { int i, j; - char *dimensions[] = { MIxspace, MIyspace, MIzspace }; + const char *dimensions[] = { MIxspace, MIyspace, MIzspace }; int varid; int old_ncopts; int dims[MAX_VAR_DIMS]; diff --git a/libsrc/netcdf_convenience.c b/libsrc/netcdf_convenience.c index 0306b400..e25ad769 100644 --- a/libsrc/netcdf_convenience.c +++ b/libsrc/netcdf_convenience.c @@ -240,7 +240,7 @@ static int mi_h5_files = 0; @CREATED : January 20, 1995 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ -PRIVATE int execute_decompress_command(char *command, const char *infile, +PRIVATE int execute_decompress_command(const char *command, const char *infile, char *outfile, int header_only) { char whole_command[1024]; @@ -303,7 +303,7 @@ MNCAPI char *miexpand_file(const char *path, char *tempfile, int header_only, FILE *fp; Compress_type compress_type; static struct { - char *extension; + const char *extension; Compress_type type; } compression_code_list[] = { {".bz", BZIPPED}, @@ -378,6 +378,10 @@ MNCAPI char *miexpand_file(const char *path, char *tempfile, int header_only, compfile = NULL; if ((first_ncerr == NC_SYSERR) && (compress_type == UNKNOWN)) { compfile = MALLOC(strlen(path) + max_compression_code_length + 2, char); + if (compfile == NULL) { + newfile = strdup(path); + MI_RETURN(newfile); + } for (iext=0; iext < complist_length; iext++) { (void) strcat(strcpy(compfile, path), compression_code_list[iext].extension); @@ -755,7 +759,7 @@ MNCAPI int miattget(int cdfid, int varid, const char *name, nc_type datatype, @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int miattget_with_sign(int cdfid, int varid, const char *name, - char *insign, nc_type datatype, char *outsign, + const char *insign, nc_type datatype, char *outsign, int max_length, void *value, int *att_length) { nc_type att_type; /* Type of attribute */ @@ -1807,7 +1811,7 @@ micreate_tempfile(void) * So I more-or-less emulate that behavior here. */ const char pat_str[] = "/minc-XXXXXX"; - char *tmpdir_ptr; + const char *tmpdir_ptr; if ((tmpdir_ptr = getenv("TMPDIR")) == NULL) { tmpdir_ptr = P_tmpdir; diff --git a/libsrc/voxel_loop.c b/libsrc/voxel_loop.c index 074e3918..bf106ab5 100644 --- a/libsrc/voxel_loop.c +++ b/libsrc/voxel_loop.c @@ -1183,6 +1183,7 @@ PRIVATE void update_history(int mincid, char *arg_string) /* Allocate a string and get the old history */ string = MALLOC(att_length, char); + if (string == NULL) return; string[0] = '\0'; (void) miattgetstr(mincid, NC_GLOBAL, MIhistory, att_length, string); @@ -2007,6 +2008,7 @@ PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files, /* Allocate structure */ loopfile_info = MALLOC(1, Loopfile_Info); + if (loopfile_info == NULL) return NULL; /* Save clobber info */ if (loop_options->clobber) { @@ -2029,6 +2031,7 @@ PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files, /* Save input file names (just copy pointers, not strings) */ if (num_input_files > 0) { loopfile_info->input_files = MALLOC(num_input_files, char *); + if (loopfile_info->input_files == NULL) { FREE(loopfile_info); return NULL; } for (ifile=0; ifile < num_input_files; ifile++) loopfile_info->input_files[ifile] = input_files[ifile]; } @@ -2038,6 +2041,7 @@ PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files, /* Save output file names (just copy pointers, not strings) */ if (num_output_files > 0) { loopfile_info->output_files = MALLOC(num_output_files, char *); + if (loopfile_info->output_files == NULL) { FREE(loopfile_info->input_files); FREE(loopfile_info); return NULL; } for (ifile=0; ifile < num_output_files; ifile++) loopfile_info->output_files[ifile] = output_files[ifile]; } @@ -2061,6 +2065,14 @@ PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files, num_free_files -= num_files; loopfile_info->output_mincid = MALLOC(num_files, int); loopfile_info->output_icvid = MALLOC(num_files, int); + if (loopfile_info->output_mincid == NULL || loopfile_info->output_icvid == NULL) { + FREE(loopfile_info->output_mincid); + FREE(loopfile_info->output_icvid); + FREE(loopfile_info->input_files); + FREE(loopfile_info->output_files); + FREE(loopfile_info); + return NULL; + } for (ifile=0; ifile < num_files; ifile++) { loopfile_info->output_mincid[ifile] = MI_ERROR; loopfile_info->output_icvid[ifile] = MI_ERROR; @@ -2084,6 +2096,16 @@ PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files, num_free_files -= num_files; loopfile_info->input_mincid = MALLOC(num_files, int); loopfile_info->input_icvid = MALLOC(num_files, int); + if (loopfile_info->input_mincid == NULL || loopfile_info->input_icvid == NULL) { + FREE(loopfile_info->input_mincid); + FREE(loopfile_info->input_icvid); + FREE(loopfile_info->output_mincid); + FREE(loopfile_info->output_icvid); + FREE(loopfile_info->input_files); + FREE(loopfile_info->output_files); + FREE(loopfile_info); + return NULL; + } for (ifile=0; ifile < num_files; ifile++) { loopfile_info->input_mincid[ifile] = MI_ERROR; loopfile_info->input_icvid[ifile] = MI_ERROR; @@ -2732,6 +2754,7 @@ MNCAPI Loop_Options *create_loop_options(void) /* Allocate structure */ loop_options = MALLOC(1, Loop_Options); + if (loop_options == NULL) return NULL; /* Fill in the defaults */ loop_options->clobber = FALSE; @@ -3227,6 +3250,7 @@ PRIVATE Loop_Info *create_loop_info(void) /* Allocate structure */ loop_info = MALLOC(1, Loop_Info); + if (loop_info == NULL) return NULL; /* Fill in the defaults */ initialize_loop_info(loop_info); diff --git a/libsrc2/datatype.c b/libsrc2/datatype.c index d6bf234d..7d3f0469 100644 --- a/libsrc2/datatype.c +++ b/libsrc2/datatype.c @@ -94,9 +94,11 @@ int miget_space_name ( mihandle_t volume, char **name ) */ length = strlen ( MI_NATIVE ); *name = malloc ( length + 1 ); + if ( *name == NULL ) return ( MI_ERROR ); strcpy ( *name, MI_NATIVE ); } else { *name = malloc ( length + 1 ); + if ( *name == NULL ) return ( MI_ERROR ); result = miget_attr_values ( volume, MI_TYPE_STRING, path_list[i], "spacetype", length+1, *name ); } diff --git a/libsrc2/dimension.c b/libsrc2/dimension.c index ffe6c60a..03372368 100644 --- a/libsrc2/dimension.c +++ b/libsrc2/dimension.c @@ -97,6 +97,7 @@ int micopy_dimension ( midimhandle_t dim_ptr, midimhandle_t *new_dim_ptr ) handle->offsets = ( double * ) malloc ( dim_ptr->length * sizeof ( double ) ); if ( handle->offsets == NULL ) { + free(handle->name); free(handle); return ( MI_ERROR ); } @@ -133,6 +134,10 @@ int micopy_dimension ( midimhandle_t dim_ptr, midimhandle_t *new_dim_ptr ) handle->widths = ( double * ) malloc ( dim_ptr->length * sizeof ( double ) ); if ( handle->widths == NULL ) { + free(handle->offsets); + free(handle->units); + free(handle->name); + free(handle); return ( MI_ERROR ); } @@ -275,6 +280,8 @@ int micreate_dimension(const char *name, midimclass_t dimclass, midimattr_t attr break; case MI_DIMCLASS_ANY: default: + free(handle->comments); + free(handle->name); free(handle); return MI_ERROR; } @@ -284,6 +291,12 @@ int micreate_dimension(const char *name, midimclass_t dimclass, midimattr_t attr if ( attr & MI_DIMATTR_NOT_REGULARLY_SAMPLED ) { handle->widths = ( double * ) malloc ( length * sizeof ( double ) ); + if ( handle->widths == NULL ) { + free(handle->comments); + free(handle->name); + free ( handle ); + return MI_ERROR; + } for ( i = 0; i < length; i++ ) { @@ -482,6 +495,7 @@ int miset_apparent_dimension_order ( mihandle_t volume, int array_length, */ if ( volume->dim_indices == NULL ) { volume->dim_indices = ( int * ) malloc ( volume->number_of_dims * sizeof ( int ) ); + if ( volume->dim_indices == NULL ) return ( MI_ERROR ); memset ( volume->dim_indices, -1, sizeof ( volume->number_of_dims ) ); } @@ -534,7 +548,7 @@ int miset_apparent_dimension_order ( mihandle_t volume, int array_length, * \ingroup mi2Dim */ int miset_apparent_dimension_order_by_name ( mihandle_t volume, int array_length, - char **names ) + const char **names ) { int diff; int i = 0, j = 0, k = 0; @@ -576,6 +590,7 @@ int miset_apparent_dimension_order_by_name ( mihandle_t volume, int array_length */ if ( volume->dim_indices == NULL ) { volume->dim_indices = ( int * ) malloc ( volume->number_of_dims * sizeof ( int ) ); + if ( volume->dim_indices == NULL ) return ( MI_ERROR ); memset ( volume->dim_indices, -1, sizeof ( volume->number_of_dims ) ); } diff --git a/libsrc2/grpattr.c b/libsrc2/grpattr.c index 732e5cad..80e4733f 100644 --- a/libsrc2/grpattr.c +++ b/libsrc2/grpattr.c @@ -95,6 +95,10 @@ int milist_start ( mihandle_t vol, const char *path, int flags, } frame = ( struct milistframe * ) malloc ( sizeof ( struct milistframe ) ); + if ( frame == NULL ) { + free ( data ); + return ( MI_ERROR ); + } frame->next = NULL; frame->grp_id = grp_id; frame->att_idx = 0; @@ -905,6 +909,9 @@ int miset_attr_values ( mihandle_t vol, mitype_t data_type, const char *path, if ( pch != NULL ) { slength = strlen ( path ) - ( pch - path ); std_name = malloc ( slength + 1 ); + if ( std_name == NULL ) { + goto cleanup; + } for ( i = 0; i < slength; i++ ) std_name[i] = path[pch - path + 1 + i]; @@ -912,6 +919,9 @@ int miset_attr_values ( mihandle_t vol, mitype_t data_type, const char *path, std_name[slength] = '\0'; } else { std_name = malloc ( strlen ( path ) + 1 ); + if ( std_name == NULL ) { + goto cleanup; + } strcpy ( std_name, path ); } diff --git a/libsrc2/m2util.c b/libsrc2/m2util.c index 356224bf..ee530c48 100644 --- a/libsrc2/m2util.c +++ b/libsrc2/m2util.c @@ -1930,7 +1930,8 @@ alloc2d ( int n, int m ) mat[i] = ( double * ) malloc ( m * sizeof ( double ) ); if ( mat[i] == NULL ) { - free(mat); + while ( --i >= 0 ) free ( mat[i] ); + free ( mat ); return NULL; } } @@ -1943,6 +1944,9 @@ free2d ( int n, double **mat ) { int i; + if ( mat == NULL ) + return; + for ( i = 0; i < n; i++ ) { free ( mat[i] ); } @@ -2078,6 +2082,8 @@ scaled_maximal_pivoting_gaussian_elimination ( int n, int success; s = alloc1d ( n ); + if ( s == NULL ) + return ( 0 ); for ( i = 0; i < n; i++ ) row[i] = i; @@ -2180,6 +2186,13 @@ scaled_maximal_pivoting_gaussian_elimination_real ( int n, a = alloc2d ( n, n ); solution = alloc2d ( n, n_values ); + if ( row == NULL || a == NULL || solution == NULL ) { + free ( row ); + if ( a != NULL ) free2d ( n, a ); + if ( solution != NULL ) free2d ( n, solution ); + return ( 0 ); + } + for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) a[i][j] = coefs[i][j]; @@ -2221,6 +2234,12 @@ invert_4x4_matrix ( double matrix[4][4], /**< Input matrix */ mtmp = alloc2d ( 4, 4 ); itmp = alloc2d ( 4, 4 ); + if ( mtmp == NULL || itmp == NULL ) { + if ( mtmp != NULL ) free2d ( 4, mtmp ); + if ( itmp != NULL ) free2d ( 4, itmp ); + return ( 0 ); + } + /* Start off with the identity matrix. */ for ( i = 0; i < 4; i++ ) { for ( j = 0; j < 4; j++ ) { diff --git a/libsrc2/minc2_api.h b/libsrc2/minc2_api.h index bbdb053a..347ec3f9 100644 --- a/libsrc2/minc2_api.h +++ b/libsrc2/minc2_api.h @@ -257,7 +257,7 @@ int miset_apparent_dimension_order(mihandle_t volume, int array_length, midimhan * all dimension names must be different or an error occurs. * \ingroup mi2Dim */ -int miset_apparent_dimension_order_by_name(mihandle_t volume, int array_length, char **names); +int miset_apparent_dimension_order_by_name(mihandle_t volume, int array_length, const char **names); /** diff --git a/libsrc2/volume.c b/libsrc2/volume.c index ecb8cb56..a0c97287 100644 --- a/libsrc2/volume.c +++ b/libsrc2/volume.c @@ -470,6 +470,7 @@ int micreate_volume(const char *filename, int number_of_dimensions, hid_t dataset_width = -1; hid_t dataspace_id = -1; char *name; + const char *attr_str; size_t size; hsize_t hdf_size[MI2_MAX_VAR_DIMS]; mihandle_t handle; @@ -610,8 +611,8 @@ int micreate_volume(const char *filename, int number_of_dimensions, */ if (volume_class != MI_CLASS_LABEL && volume_class != MI_CLASS_UNIFORM_RECORD) { - size_t size = H5Tget_size(handle->ftype_id); - char *tmp = calloc(1, size); + size_t fill_size = H5Tget_size(handle->ftype_id); + char *tmp = calloc(1, fill_size); H5Pset_fill_value(hdf_plist, handle->ftype_id, tmp); free(tmp); } @@ -790,9 +791,9 @@ int micreate_volume(const char *filename, int number_of_dimensions, } if (dimensions[i]->attr & MI_DIMATTR_NOT_REGULARLY_SAMPLED) { - name = "irregular"; + attr_str = "irregular"; } else { - name = "regular__"; + attr_str = "regular__"; } /* Create attribute "spacing" and set its value to "regular__" or "irregular" @@ -800,26 +801,26 @@ int micreate_volume(const char *filename, int number_of_dimensions, if(!dimension_is_vector) miset_attr_at_loc(dataset_id, "spacing", MI_TYPE_STRING, - strlen(name), name); + strlen(attr_str), attr_str); switch (dimensions[i]->dim_class) { case MI_DIMCLASS_SPATIAL: - name = "spatial"; + attr_str = "spatial"; break; case MI_DIMCLASS_TIME: - name = "time___"; + attr_str = "time___"; break; case MI_DIMCLASS_SFREQUENCY: - name = "sfreq__"; + attr_str = "sfreq__"; break; case MI_DIMCLASS_TFREQUENCY: - name = "tfreq__"; + attr_str = "tfreq__"; break; case MI_DIMCLASS_USER: - name = "user___"; + attr_str = "user___"; break; case MI_DIMCLASS_RECORD: - name = "record_"; + attr_str = "record_"; break; case MI_DIMCLASS_ANY: default: @@ -841,8 +842,8 @@ int micreate_volume(const char *filename, int number_of_dimensions, { const char *align_str; - miset_attr_at_loc(dataset_id, "class", MI_TYPE_STRING, strlen(name), - name); + miset_attr_at_loc(dataset_id, "class", MI_TYPE_STRING, strlen(attr_str), + attr_str); /* Save step value. */ @@ -1219,6 +1220,7 @@ static int _miget_file_dimension(mihandle_t volume, const char *dimname, snprintf(path, sizeof(path), MI_ROOT_PATH "/dimensions/%s", dimname); /* Allocate space for the dimension handle */ hdim = (midimhandle_t) malloc(sizeof (*hdim)); + if (hdim == NULL) return (MI_ERROR); /* Initialize everything to zero */ memset(hdim, 0, sizeof (*hdim)); diff --git a/testdir/icv_range.c b/testdir/icv_range.c index 0b60d527..6dbbf5a2 100644 --- a/testdir/icv_range.c +++ b/testdir/icv_range.c @@ -28,15 +28,15 @@ int main(int argc, char **argv) { int icv, cdfid, img, max, min; - static char *typenm[]={"short", "double"}; - static char *boolnm[] = {"true", "false"}; + static const char *typenm[]={"short", "double"}; + static const char *boolnm[] = {"true", "false"}; static nc_type intypes[] = {NC_SHORT, NC_DOUBLE}; static int norms[] = {TRUE, FALSE}; static nc_type outtypes[] = {NC_SHORT, NC_DOUBLE}; static int maxpresent[] = {TRUE, FALSE}; static int valpresent[] = {TRUE, FALSE}; static int dim[MAX_VAR_DIMS]; - static struct { long len; char *name;} diminfo[] = { + static struct { long len; const char *name;} diminfo[] = { { 3, MIzspace }, { 1, MIyspace }, { 1, MIxspace } diff --git a/testdir/icv_vec.c b/testdir/icv_vec.c index 52e45298..ee548143 100644 --- a/testdir/icv_vec.c +++ b/testdir/icv_vec.c @@ -69,6 +69,8 @@ test_icv_vector(int cflag, nc_type voxel_type) if (ivalue == NULL || dvalue == NULL) { fprintf(stdout, "ERROR bad return code at line %d!\n", __LINE__); + free(ivalue); + free(dvalue); return 1; } diff --git a/testdir/minc2-create-test-images-2.c b/testdir/minc2-create-test-images-2.c index 5ffda42a..81ca5e87 100644 --- a/testdir/minc2-create-test-images-2.c +++ b/testdir/minc2-create-test-images-2.c @@ -32,6 +32,9 @@ static int create_real_as_int_image(const char* fname) double min = -1.0; double max = 1.0; + + if ( buf == NULL ) return -1; + /*TODO: add error checks in this functions*/ r = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[0]); @@ -110,6 +113,9 @@ static int create_real_as_float_image(const char* fname) double min = -1.0; double max = 1.0; + + if ( buf == NULL ) return -1; + r = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[0]); if( r!= MI_NOERROR ) TESTRPT("micreate_dimension",r); diff --git a/testdir/minc2-create-test-images.c b/testdir/minc2-create-test-images.c index a4bec9a2..ba39f38d 100644 --- a/testdir/minc2-create-test-images.c +++ b/testdir/minc2-create-test-images.c @@ -30,34 +30,40 @@ static int create_2D_image ( const char *fname ) misize_t count[NDIMS - 1]; misize_t start[NDIMS - 1]; + if ( buf == NULL || offsets == NULL ) { + free ( buf ); + free ( offsets ); + return -1; + } + r = micreate_dimension ( "xspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_NOT_REGULARLY_SAMPLED, CX, &hdim[0] ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_dimension ( "yspace", MI_DIMCLASS_USER, MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[1] ); - if(r<0) return r; + if(r<0) goto cleanup; for ( i = 0; i < CX; i++ ) { offsets[i] = ( i * i ) + 0.1; } r = miset_dimension_offsets ( hdim[0], CX, 0, offsets ); - if(r<0) return r; + if(r<0) goto cleanup; r = miset_dimension_separation ( hdim[1], 0.06 ); - if(r<0) return r; + if(r<0) goto cleanup; r = miset_dimension_starts ( hdim, NDIMS - 1, start_values ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_volume ( fname, NDIMS - 1 , hdim, MI_TYPE_SHORT, MI_CLASS_REAL, NULL, &hvol ); - if(r<0) return r; + if(r<0) goto cleanup; /* set slice scaling flag to true */ r = miset_slice_scaling_flag ( hvol, flag ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_volume_image ( hvol ); - if(r<0) return r; + if(r<0) goto cleanup; for ( i = 0; i < CX * CY; i++ ) { buf[i] = ( short ) (i * 0.1); @@ -66,10 +72,11 @@ static int create_2D_image ( const char *fname ) count[0] = CX; count[1] = CY; r = miset_voxel_value_hyperslab ( hvol, MI_TYPE_SHORT, start, count, buf ); - if(r<0) return r; + if(r<0) goto cleanup; r = miclose_volume ( hvol ); - if(r<0) return r; + +cleanup: free(buf); free(offsets); return r; @@ -90,36 +97,37 @@ static int create_3D_image ( const char *fname ) double min = -1.0; double max = 1.0; + + if ( buf == NULL ) return -1; + r = micreate_dimension ( "yspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[0] ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_dimension ( "xspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdim[1] ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_dimension ( "zspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[2] ); - if(r<0) return r; + if(r<0) goto cleanup; r = miset_dimension_starts ( hdim, NDIMS, start_values ); - if(r<0) return r; + if(r<0) goto cleanup; r = miset_dimension_separations ( hdim, NDIMS, separations ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_volume ( fname, NDIMS, hdim, MI_TYPE_USHORT, MI_CLASS_REAL, NULL, &hvol ); - if(r<0) return r; + if(r<0) goto cleanup; /* set slice scaling flag to true */ r = miset_slice_scaling_flag ( hvol, flag ); - if(r<0) return r; - + if(r<0) goto cleanup; r = micreate_volume_image ( hvol ); - if(r<0) return r; - + if(r<0) goto cleanup; for ( i = 0; i < CY * CX * CZ; i++ ) { buf[i] = ( unsigned short ) (i * 0.001); @@ -129,7 +137,7 @@ static int create_3D_image ( const char *fname ) count[0] = CY; count[1] = CX; count[2] = CZ; r = miset_voxel_value_hyperslab ( hvol, MI_TYPE_USHORT, start, count, buf ); - if(r<0) return r; + if(r<0) goto cleanup; /* Set random values to slice min and max for slice scaling*/ start[0] = start[1] = start[2] = 0; @@ -138,12 +146,13 @@ static int create_3D_image ( const char *fname ) min += 0.1; max += 0.1; r = miset_slice_range ( hvol, start, NDIMS , max, min ); - if(r<0) return r; - + if(r<0) goto cleanup; } - free(buf); r = miclose_volume ( hvol ); + +cleanup: + free(buf); return r; } @@ -162,37 +171,40 @@ static int create_4D_image ( const char *fname ) double min = -1.0; double max = 1.0; + + if ( buf == NULL ) return -1; + r = micreate_dimension ( "xspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdim[0] ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_dimension ( "time", MI_DIMCLASS_USER, MI_DIMATTR_REGULARLY_SAMPLED, CU, &hdim[1] ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_dimension ( "zspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[2] ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_dimension ( "yspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[3] ); - if(r<0) return r; + if(r<0) goto cleanup; r = miset_dimension_starts ( hdim, NDIMS + 1, start_values ); - if(r<0) return r; + if(r<0) goto cleanup; r = miset_dimension_separations ( hdim, NDIMS + 1, separations ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_volume ( fname, NDIMS + 1, hdim, MI_TYPE_UBYTE, MI_CLASS_REAL, NULL, &hvol ); - if(r<0) return r; + if(r<0) goto cleanup; /* set slice scaling flag to true */ r = miset_slice_scaling_flag ( hvol, flag ); - if(r<0) return r; + if(r<0) goto cleanup; r = micreate_volume_image ( hvol ); - if(r<0) return r; + if(r<0) goto cleanup; for ( i = 0; i < CX * CU * CZ * CY; i++ ) { buf[i] = ( unsigned char ) i; @@ -202,7 +214,7 @@ static int create_4D_image ( const char *fname ) count[0] = CX; count[1] = CU; count[2] = CZ; count[3] = CY; r = miset_voxel_value_hyperslab ( hvol, MI_TYPE_UBYTE, start, count, buf ); - if(r<0) return r; + if(r<0) goto cleanup; /* Set random values to slice min and max for slice scaling*/ start[0] = start[1] = start[2] = start[3] = 0; for ( i = 0; i < CX; i++ ) { @@ -213,12 +225,14 @@ static int create_4D_image ( const char *fname ) min += -0.1; max += 0.1; r = miset_slice_range ( hvol, start, NDIMS + 1 , max, min ); - if(r<0) return r; + if(r<0) goto cleanup; } } - free(buf); r = miclose_volume ( hvol ); + +cleanup: + free(buf); return r; } diff --git a/testdir/minc2-grpattr-test.c b/testdir/minc2-grpattr-test.c index 6590392b..9b16a857 100644 --- a/testdir/minc2-grpattr-test.c +++ b/testdir/minc2-grpattr-test.c @@ -30,7 +30,6 @@ int main(void) char pathbuf[256]=""; char namebuf[256]=""; char pathbuf1[1024]=""; - int count=0; r = micreate_volume("tst-grpa.mnc", 0, NULL, MI_TYPE_UINT, MI_CLASS_REAL, NULL, &hvol); @@ -280,7 +279,6 @@ int main(void) r = milist_start(hvol, "/", 0, &hlist); if (r == MI_NOERROR) { - count++; while (milist_attr_next(hvol, hlist, pathbuf, sizeof(pathbuf), namebuf, sizeof(namebuf)) == MI_NOERROR) { printf(" %s %s\n", pathbuf, namebuf); diff --git a/testdir/minc2-hyper-test-2.c b/testdir/minc2-hyper-test-2.c index d3921e4c..115e969a 100644 --- a/testdir/minc2-hyper-test-2.c +++ b/testdir/minc2-hyper-test-2.c @@ -37,6 +37,9 @@ static void create_test_file ( void ) double min = -1.0; double max = 1.0; + + if ( buf == NULL ) return; + micreate_dimension ( "zspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[0] ); diff --git a/testdir/minc2-hyper-test.c b/testdir/minc2-hyper-test.c index 2328afac..cd8c8983 100644 --- a/testdir/minc2-hyper-test.c +++ b/testdir/minc2-hyper-test.c @@ -41,7 +41,7 @@ create_and_test_image(const char *name, misize_t count[NDIMS]; double dtemp[CX][CY][CZ]; int i,j,k; - char *dimnames[] = {"zspace", "xspace", "yspace"}; + const char *dimnames[] = {"zspace", "xspace", "yspace"}; int error_cnt = 0; result = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, @@ -613,7 +613,7 @@ test3(void) int itemp[CZ][CX][CY]; int i,j,k; midimhandle_t hdims[NDIMS]; - char *dimnames[] = {"zspace", "xspace", "yspace"}; + const char *dimnames[] = {"zspace", "xspace", "yspace"}; int error_cnt = 0; printf("Testing hyperslab operations with multi-resolution image.\n"); diff --git a/testdir/minc2-label-test.c b/testdir/minc2-label-test.c index 8b792256..78f057a6 100644 --- a/testdir/minc2-label-test.c +++ b/testdir/minc2-label-test.c @@ -156,6 +156,12 @@ main ( void ) double *dbuf = ( double * ) malloc ( CX * CY * CZ * sizeof ( double ) ); int result; + if ( buf == NULL || dbuf == NULL ) { + free ( buf ); + free ( dbuf ); + return 1; + } + printf ( "Creating label image !! \n" ); error_cnt += create_label_image(); diff --git a/testdir/minc2-large-attribute.c b/testdir/minc2-large-attribute.c index 7f719513..de1997fa 100644 --- a/testdir/minc2-large-attribute.c +++ b/testdir/minc2-large-attribute.c @@ -19,7 +19,7 @@ static int error_cnt=0; #define NDIMS 3 -static int create_3D_image ( size_t attribute_size,char *test_file ) +static int create_3D_image ( size_t attribute_size, const char *test_file ) { int r; double start_values[NDIMS] = { -6.96, -12.453, -9.48}; @@ -67,6 +67,7 @@ static int create_3D_image ( size_t attribute_size,char *test_file ) buf = ( unsigned short * ) malloc ( CX * CY * CZ * sizeof ( unsigned short ) ); + if ( buf == NULL ) return -1; for ( i = 0; i < CY * CX * CZ; i++ ) { buf[i] = ( unsigned short ) (i * 0.001); } @@ -100,7 +101,7 @@ static int create_3D_image ( size_t attribute_size,char *test_file ) } -static int test_3D_image ( size_t attribute_size,char *test_file ) +static int test_3D_image ( size_t attribute_size, const char *test_file ) { int r; mihandle_t hvol; @@ -159,7 +160,7 @@ static int test_3D_image ( size_t attribute_size,char *test_file ) int main ( int argc, char **argv ) { int attribute_size=100000; - char *test_file="3D_image_a.mnc"; + const char *test_file="3D_image_a.mnc"; if(argc>1) attribute_size=atoi(argv[1]); if(argc>2) diff --git a/testdir/minc2-leak-test.c b/testdir/minc2-leak-test.c index 30e4b0fb..a07fc265 100644 --- a/testdir/minc2-leak-test.c +++ b/testdir/minc2-leak-test.c @@ -23,9 +23,10 @@ #define CHUNK_LENGTH 10 /** - * checks for maximum memory usage. units seem to be different between - * os x and linux despite the documentation, but the code seems to work - * either way. + * checks for maximum memory usage. + * ru_maxrss is in kilobytes on Linux but bytes on macOS/BSD. + * Normalize to kilobytes so the leak-detection threshold works + * on both platforms. */ static int check_high_water_mark(void) @@ -33,7 +34,11 @@ check_high_water_mark(void) struct rusage usage; if (getrusage(RUSAGE_SELF, &usage) < 0) return 0; - return usage.ru_maxrss; +#ifdef __APPLE__ + return usage.ru_maxrss / 1024; /* bytes -> KB */ +#else + return usage.ru_maxrss; /* already KB on Linux */ +#endif } static int diff --git a/testdir/minc2-read-rgb.c b/testdir/minc2-read-rgb.c index 943454d1..65d18782 100644 --- a/testdir/minc2-read-rgb.c +++ b/testdir/minc2-read-rgb.c @@ -118,6 +118,11 @@ int main ( int argc, char **argv ) origin=malloc(sizeof(double)*ndim); step=malloc(sizeof(double)*ndim); + if ( dim == NULL || sizes == NULL || origin == NULL || step == NULL ) { + free(dim); free(sizes); free(origin); free(step); + return 1; + } + /* get the apparent dimensions and their sizes */ r = miget_volume_dimensions ( vol, MI_DIMCLASS_ANY , MI_DIMATTR_ALL, MI_DIMORDER_FILE, @@ -162,7 +167,7 @@ int main ( int argc, char **argv ) { /*Now we are going to work with the volume using apparent dimension order*/ midimhandle_t my_dim[4]; - static char *my_dimorder[] = {MIvector_dimension,MIxspace,MIyspace,MIzspace}; + static const char *my_dimorder[] = {MIvector_dimension,MIxspace,MIyspace,MIzspace}; misize_t my_sizes[4]; misize_t my_start[4]; misize_t my_count[4]; @@ -330,7 +335,7 @@ int main ( int argc, char **argv ) } else if(ndim==3) { /*Now we are going to work with the volume using apparent dimension order*/ midimhandle_t my_dim[3]; - static char *my_dimorder[] = {MIxspace,MIyspace,MIzspace}; + static const char *my_dimorder[] = {MIxspace,MIyspace,MIzspace}; misize_t my_sizes[3]; misize_t my_start[3]; misize_t my_count[3]; diff --git a/testdir/minc2-vector_dimension-test.c b/testdir/minc2-vector_dimension-test.c index f36bc0ca..a0db6a4f 100644 --- a/testdir/minc2-vector_dimension-test.c +++ b/testdir/minc2-vector_dimension-test.c @@ -22,6 +22,8 @@ static void create_test_file ( void ) misize_t count[NDIMS]; misize_t start[NDIMS]; + if ( buf == NULL ) return; + micreate_dimension ( "zspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[0] ); diff --git a/testdir/minc_conversion.c b/testdir/minc_conversion.c index dd3f924a..ab03367f 100644 --- a/testdir/minc_conversion.c +++ b/testdir/minc_conversion.c @@ -101,7 +101,7 @@ static int test1(struct testinfo *ip, struct dimdef *dims, int ndims) /* Create the image-max variable. */ printf("%s:%d\n",__FILE__,__LINE__); - ip->maxid = micreate_std_variable(ip->fd, (char*)MIimagemax, NC_DOUBLE, 0, NULL); + ip->maxid = micreate_std_variable(ip->fd, MIimagemax, NC_DOUBLE, 0, NULL); if (ip->maxid < 0) { FUNC_ERROR("micreate_std_variable"); } @@ -109,13 +109,13 @@ static int test1(struct testinfo *ip, struct dimdef *dims, int ndims) /* Create the image-min variable. */ printf("%s:%d\n",__FILE__,__LINE__); - ip->minid = micreate_std_variable(ip->fd, (char*)MIimagemin, NC_DOUBLE, 0, NULL); + ip->minid = micreate_std_variable(ip->fd, MIimagemin, NC_DOUBLE, 0, NULL); if (ip->minid < 0) { FUNC_ERROR("micreate_std_variable"); } printf("%s:%d\n",__FILE__,__LINE__); - ip->imgid = micreate_std_variable(ip->fd, (char*)MIimage, NC_FLOAT, ndims, ip->dim); + ip->imgid = micreate_std_variable(ip->fd, MIimage, NC_FLOAT, ndims, ip->dim); if (ip->imgid < 0) { FUNC_ERROR("micreate_std_variable"); } @@ -304,7 +304,7 @@ static void test4(struct testinfo *ip, struct dimdef *dims, int ndims) int r; /*Now we are going to work with the volume using apparent dimension order*/ midimhandle_t my_dim[3]; - static char *my_dimorder[] = {MIxspace,MIyspace,MIzspace}; + static const char *my_dimorder[] = {MIxspace,MIyspace,MIzspace}; misize_t my_sizes[3]; misize_t my_start[3]; misize_t my_count[3]; diff --git a/testdir/minc_long_attr.c b/testdir/minc_long_attr.c index fe2c4015..568bd719 100644 --- a/testdir/minc_long_attr.c +++ b/testdir/minc_long_attr.c @@ -116,7 +116,7 @@ static int test1(struct testinfo *ip, struct dimdef *dims, int ndims) FUNC_ERROR("micreate_std_variable"); } - ip->test_group = ncvardef(ip->fd,(char*)"test",NC_INT,0,0);/* micreate_group_variable(ip->fd,(char*)"test");*/ + ip->test_group = ncvardef(ip->fd,"test",NC_INT,0,0);/* micreate_group_variable(ip->fd,(char*)"test");*/ if(ip->test_group<0) { FUNC_ERROR("micreate_group_variable"); diff --git a/testdir/minc_types.c b/testdir/minc_types.c index 384fc01e..b1232990 100644 --- a/testdir/minc_types.c +++ b/testdir/minc_types.c @@ -33,8 +33,8 @@ static struct { nc_type type; - char *sign; - char *ctype; + const char *sign; + const char *ctype; } types[]= { { NC_BYTE, MI_UNSIGNED, "byte" }, { NC_BYTE, MI_SIGNED, "byte" }, diff --git a/volume_io/Volumes/input_mnc2.c b/volume_io/Volumes/input_mnc2.c index b3addc4c..4e6e91b1 100644 --- a/volume_io/Volumes/input_mnc2.c +++ b/volume_io/Volumes/input_mnc2.c @@ -52,7 +52,7 @@ static Minc_file initialize_minc_input_from_minc2_id( { minc_file_struct *file; int n_vol_dims; - int i, slab_size, prev_sizes[MAX_VAR_DIMS]; + int i, prev_sizes[MAX_VAR_DIMS]; mitype_t prev_minc_type; VIO_BOOL different; VIO_BOOL range_specified; @@ -157,7 +157,6 @@ static Minc_file initialize_minc_input_from_minc2_id( if(slice_scaling_flag) { int n_slice_dimensions=file->n_file_dimensions; - int slices_count=1; misize_t *slice_start; if(miget_slice_dimension_count(file->minc2id, @@ -172,13 +171,12 @@ static Minc_file initialize_minc_input_from_minc2_id( } n_slice_dimensions=file->n_file_dimensions-n_slice_dimensions; - - /*now iterate through all slices to find out global image intensity range*/ - for_less(d,0,n_slice_dimensions) + slice_start=(misize_t *)calloc(n_slice_dimensions,sizeof(misize_t)); + if(slice_start==NULL) { - slices_count*=dimension_size[d]; + print_error("Memory allocation failed for slice_start!\n"); + return( (Minc_file) NULL ); } - slice_start=(misize_t *)calloc(n_slice_dimensions,sizeof(misize_t)); miget_slice_range(file->minc2id,slice_start,n_slice_dimensions,&volume_max,&volume_min); do @@ -492,11 +490,9 @@ static Minc_file initialize_minc_input_from_minc2_id( chunking dimensions for compression */ file->n_slab_dims = 0; - slab_size = 1; for( d = file->n_file_dimensions-1; d >= 0; d-- ) { if( file->to_volume_index[d] != INVALID_AXIS ) { - slab_size *= file->sizes_in_file[d]; file->n_slab_dims++; /* integral number of complete dimensions */ } } diff --git a/volume_io/Volumes/input_nifti.c b/volume_io/Volumes/input_nifti.c index ad9507d0..6cca2e9d 100644 --- a/volume_io/Volumes/input_nifti.c +++ b/volume_io/Volumes/input_nifti.c @@ -615,6 +615,11 @@ input_more_nifti_format_file( VIO_Real *temp_buffer = malloc(in_ptr->sizes_in_file[0] * sizeof(VIO_Real)); + if (temp_buffer == NULL) + { + print_error("Failed to allocate temp_buffer.\n"); + return FALSE; + } /* If the memory for the volume has not been allocated yet, * initialize that memory now. @@ -625,6 +630,7 @@ input_more_nifti_format_file( if (!volume_is_alloced(volume)) { print_error("Failed to allocate volume.\n"); + free(temp_buffer); return FALSE; } } @@ -632,6 +638,7 @@ input_more_nifti_format_file( n_bytes_read = nifti_read_buffer(zfp, data_ptr, n_bytes_per_slice, nii_ptr); if (n_bytes_read < n_bytes_per_slice) { + free(temp_buffer); return FALSE; } diff --git a/volume_io/Volumes/output_mnc2.c b/volume_io/Volumes/output_mnc2.c index b0252eb8..78f3c03b 100644 --- a/volume_io/Volumes/output_mnc2.c +++ b/volume_io/Volumes/output_mnc2.c @@ -1120,7 +1120,7 @@ static VIO_Status output_the_volume2( { VIO_Status status; int d, n_volume_dims, sizes[VIO_MAX_DIMENSIONS]; - int slab_size, n_slab, this_count; + int n_slab, this_count; int vol_index, step, n_steps; int to_volume_index[MAX_VAR_DIMS]; int to_file_index[VIO_MAX_DIMENSIONS]; @@ -1202,7 +1202,6 @@ static VIO_Status output_the_volume2( chunking dimensions for compression (for efficiency) */ file->n_slab_dims = 0; - slab_size = 1; n_steps = 1; for( d = file->n_file_dimensions-1; d >= 0; d-- ) { @@ -1211,7 +1210,6 @@ static VIO_Status output_the_volume2( if( to_volume_index[d] != INVALID_AXIS ) { count[d] = volume_count[to_volume_index[d]]; file->n_slab_dims++; /* integral number of complete dimensions */ - slab_size *= count[d]; } } diff --git a/volume_io/Volumes/volume_cache.c b/volume_io/Volumes/volume_cache.c index fcf99183..8f46d616 100644 --- a/volume_io/Volumes/volume_cache.c +++ b/volume_io/Volumes/volume_cache.c @@ -459,7 +459,7 @@ static void write_cache_block( int file_start[VIO_MAX_DIMENSIONS]; int file_count[VIO_MAX_DIMENSIONS]; int volume_sizes[VIO_MAX_DIMENSIONS]; - int block_start[VIO_MAX_DIMENSIONS]; + int block_start[VIO_MAX_DIMENSIONS] = {0}; void *array_data_ptr; minc_file = (Minc_file) cache->minc_file; @@ -1239,7 +1239,7 @@ static VIO_cache_block_struct *get_cache_block_for_voxel( { VIO_cache_block_struct *block; VIO_cache_lookup_struct *lookup0, *lookup1, *lookup2, *lookup3, *lookup4; - int block_index; + int block_index = 0; int block_start[VIO_MAX_DIMENSIONS]; int n_dims, hash_index; VIO_volume_cache_struct *cache;