From 23b66911d0fe32310840432fdb50fe8f9fad021a Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:21:04 -0400 Subject: [PATCH] BUG: Stop casting away const in fslio and cifti Nine -Wcast-qual warnings. Casting away const is how a read-only contract turns into a write to memory the caller thought was safe, so each was looked at rather than silenced. fslio.c, FslGetFileType2() mutablefslio = (FSLIO *)fslio; /* dodgy and will generate warnings */ mutablefslio->niftiptr->nifti_type = ...; The comment is right that it looks dodgy, but the cast was never needed. `const FSLIO * fslio` makes the *member* niftiptr const -- its type is `nifti_image * const` -- while what it points at stays fully mutable. The assignment is legal as written, so the cast and the local both go. fslio.c FslWriteVolumes(), cifti axio_num_tokens(), text_to_i64(), text_to_f64() Pointers cast to char * and then only read: walked, indexed, or handed to strtoll()/strtod(), which take const char *. Declared const, casts removed. cifti strip_whitespace() Returned `(char *)str` on its early-exit paths, handing the caller a writable pointer to the const string it passed in. The function is static, so its return type is nobody else's business; it now returns const char *. --- cifti/afni_xml.c | 10 +++++----- cifti/afni_xml_io.c | 14 ++++++++------ fsliolib/fslio.c | 9 ++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 1c9a5956..bfe691ca 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -141,7 +141,7 @@ static int show_attrs (afni_xml_control *, const char **, int); static int64_t loc_strnlen (const char * str, int64_t maxlen); static afni_xml_t * make_afni_xml (const char * ename, const char ** attr); -static char * strip_whitespace(const char * str, int slen); +static const char * strip_whitespace(const char * str, int slen); /*----------------------- main I/O functions ---------------------------*/ @@ -888,7 +888,7 @@ static int show_attrs(afni_xml_control * xd, const char ** attr, int showd) static void free_whitespace(void) { strip_whitespace(NULL,-2); } /* if slen == 0, use entire length */ -static char * strip_whitespace(const char * str, int slen) +static const char * strip_whitespace(const char * str, int slen) { static char * buf = NULL; static int blen = 0; @@ -898,18 +898,18 @@ static char * strip_whitespace(const char * str, int slen) if(!str && slen == -2){ free(buf); buf=NULL; blen=0; return 0; } /* if string is long, forget it */ - if( !str || slen > 1024 ) return (char *)str; + if( !str || slen > 1024 ) return str; len = strlen(str); if( slen > 0 && slen < len ) len = slen; - if( len <= 0 ) return (char *)str; + if( len <= 0 ) return str; /* make sure we have local space */ if( len > blen ) { /* allocate a bigger buffer */ buf = (char *)safe_realloc(buf, (len+1) * sizeof(char)); if( !buf ) { fprintf(stderr,"** failed to alloc wspace buf of len %d\n", len+1); - return (char *)str; + return str; } blen = len; } diff --git a/cifti/afni_xml_io.c b/cifti/afni_xml_io.c index a06229e4..6a1430e3 100644 --- a/cifti/afni_xml_io.c +++ b/cifti/afni_xml_io.c @@ -111,7 +111,7 @@ int axio_text_to_binary(afni_xml_t * ax) int axio_num_tokens(const char * str, int64_t maxlen) { - char * sp = (char *)str; + const char * sp = str; int64_t ind, len, ntok; int intok; /* flag: are we inside a token? */ @@ -123,7 +123,7 @@ int axio_num_tokens(const char * str, int64_t maxlen) ntok = 0; intok = 0; - for( ind = 0, sp = (char *)str; ind < len; ind++, sp++ ) { + for( ind = 0, sp = str; ind < len; ind++, sp++ ) { /* just look for state switches */ if( intok ) { if( isspace(*sp) || (*sp == ',') ) @@ -519,7 +519,8 @@ static int can_process_dtype(int dtype) * varies, unfortunately */ static int64_t text_to_i64(int64_t * result, const char * text, int64_t nvals) { - char * eptr, * sptr; + char * eptr; + const char * sptr; int64_t * rptr, val; int64_t nread; @@ -527,7 +528,7 @@ static int64_t text_to_i64(int64_t * result, const char * text, int64_t nvals) *result = 0; /* Initialize to zero in case of failure */ if( nvals <= 0 ) return 0; - sptr = (char *)text; + sptr = text; nread = 0; rptr = result; @@ -548,14 +549,15 @@ static int64_t text_to_i64(int64_t * result, const char * text, int64_t nvals) static int64_t text_to_f64(double * result, const char * text, int64_t nvals) { - char * eptr, * sptr; + char * eptr; + const char * sptr; double * rptr, val; int64_t nread; if( ! text || ! result) return 1; if( nvals <= 0 ) return 0; - sptr = (char *)text; + sptr = text; nread = 0; rptr = result; diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index c80e62a0..a8ef9450 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -99,7 +99,6 @@ int FslBaseFileType(int filetype) static int FslGetFileType2(const FSLIO *fslio, int quiet) { - FSLIO *mutablefslio; if (fslio==NULL) FSLIOERR("FslGetFileType: Null pointer passed for FSLIO"); if ( (fslio->file_mode==FSL_TYPE_MINC) || (fslio->file_mode==FSL_TYPE_MINC_GZ) ) { return fslio->file_mode; @@ -112,8 +111,7 @@ static int FslGetFileType2(const FSLIO *fslio, int quiet) fprintf(stderr,"Warning: nifti structure and fsl structure disagree on file type\n"); fprintf(stderr,"nifti = %d and fslio = %d\n",fslio->niftiptr->nifti_type,fslio->file_mode); } - mutablefslio = (FSLIO *) fslio; /* dodgy and will generate warnings */ - mutablefslio->niftiptr->nifti_type = FslBaseFileType(fslio->file_mode); + fslio->niftiptr->nifti_type = FslBaseFileType(fslio->file_mode); return fslio->file_mode; } } @@ -926,10 +924,11 @@ size_t FslWriteVolumes(FSLIO *fslio, const void *buffer, size_t nvols) && (FslGetLeftRightOrder(fslio)==FSL_NEUROLOGICAL) ) { /* If it is Analyze and Neurological order then SWAP DATA into Radiological order */ /* This is nasty - but what else can be done?!? */ - char *tmpbuf, *inbuf; + char *tmpbuf; + const char *inbuf; long int x, b, n, nrows; short nx, ny, nz, nv; - inbuf = (char *) buffer; + inbuf = buffer; tmpbuf = (char *)calloc(nbytes,1); FslGetDim(fslio,&nx,&ny,&nz,&nv); nrows = nbytes / (nx * bpv);