From feb7ea9c2912a6e09bd8caebe790e664d1b28722 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:33:01 -0400 Subject: [PATCH 1/3] ENH: Make the integer conversions in allocations, copies and reads explicit 152 of the -Wsign-conversion findings are an int or int64_t count reaching a size_t parameter of malloc(), calloc(), realloc(), memcpy(), memset(), strncpy(), znzread(), znzwrite() or fread(). Where a count is multiplied by a sizeof, the cast goes on the count rather than around the product, so the multiply happens at 64-bit width: (int)count * sizeof(T) can overflow before it is widened. The counts cannot be negative. nifti_update_dims_from_array() clamps every dim[i] to at least 1, nifti_datatype_sizes() leaves nbyper at 0 for an unknown datatype, and nifti_image_load() requires nbyper > 0 and nvox > 0. --- cifti/afni_xml.c | 20 +++++----- cifti/afni_xml_io.c | 2 +- nifti2/nifti2_io.c | 90 +++++++++++++++++++++--------------------- nifti2/nifti_tool.c | 34 ++++++++-------- niftilib/nifti1_io.c | 46 ++++++++++----------- niftilib/nifti1_tool.c | 24 +++++------ 6 files changed, 108 insertions(+), 108 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index bfe691ca..388f99ae 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -196,7 +196,7 @@ afni_xml_list axml_read_file(const char * fname, int read_data) { if( reset_xml_buf(xd, &buf, &bsize) ) break; - blen = fread(buf, 1, bsize, fp); + blen = fread(buf, 1, (size_t)bsize, fp); /* check for early termination */ bshort = loc_strnlen(buf, blen); @@ -447,8 +447,8 @@ int axml_add_attrs(afni_xml_t * ax, const char ** attr) return 0; } - ax->attrs.name = (char **)malloc(natr*sizeof(char *)); - ax->attrs.value = (char **)malloc(natr*sizeof(char *)); + ax->attrs.name = (char **)malloc((size_t)natr * sizeof(char *)); + ax->attrs.value = (char **)malloc((size_t)natr * sizeof(char *)); /* failure? */ if( ! ax->attrs.name || ! ax->attrs.value ) { @@ -666,7 +666,7 @@ static int reset_xml_buf(afni_xml_control * xd, char ** buf, int * bsize) fprintf(stderr,"++ update buf, %d to %d bytes\n",*bsize,xd->buf_size); *bsize = xd->buf_size; - *buf = (char *)safe_realloc(*buf, (*bsize+1) * sizeof(char)); + *buf = (char *)safe_realloc(*buf, (size_t)((*bsize+1) * sizeof(char))); if( ! *buf ) { fprintf(stderr,"** failed to alloc %d bytes of xml buf!\n", *bsize); *bsize = 0; @@ -826,7 +826,7 @@ static int add_to_xroot_list(afni_xml_control * xd, afni_xml_t * newp) xd->xroot->len++; xd->xroot->xlist = (afni_xml_t **)safe_realloc(xd->xroot->xlist, - xd->xroot->len * sizeof(afni_xml_t *)); + (size_t)xd->xroot->len * sizeof(afni_xml_t *)); if( ! xd->xroot->xlist ) { fprintf(stderr,"** failed to alloc %d AXMLT pointers\n", xd->xroot->len); return 1; @@ -844,7 +844,7 @@ static int add_to_xchild_list(afni_xml_t * parent, afni_xml_t * child) parent->nchild++; parent->xchild = (afni_xml_t **)safe_realloc(parent->xchild, - parent->nchild * sizeof(afni_xml_t *)); + (size_t)parent->nchild * sizeof(afni_xml_t *)); if( ! parent->xchild ) { fprintf(stderr,"** failed to alloc %d AXML pointers\n", parent->nchild); return 1; @@ -906,7 +906,7 @@ static const char * strip_whitespace(const char * str, int slen) /* make sure we have local space */ if( len > blen ) { /* allocate a bigger buffer */ - buf = (char *)safe_realloc(buf, (len+1) * sizeof(char)); + buf = (char *)safe_realloc(buf, (size_t)((len+1) * sizeof(char))); if( !buf ) { fprintf(stderr,"** failed to alloc wspace buf of len %d\n", len+1); return str; @@ -919,7 +919,7 @@ static const char * strip_whitespace(const char * str, int slen) if( ifirst == len ) *buf = '\0'; else { - strncpy(buf, str+ifirst, len-ifirst-ilast); + strncpy(buf, str+ifirst, (size_t)(len-ifirst-ilast)); buf[len-ifirst-ilast] = '\0'; } @@ -971,14 +971,14 @@ static int append_to_string(char ** ostr, int * olen, newlen = *olen + ilen; - *ostr = (char *)safe_realloc(*ostr, newlen * sizeof(char)); + *ostr = (char *)safe_realloc(*ostr, (size_t)newlen * sizeof(char)); if( !*ostr ) { fprintf(stderr,"** AX.A2S: failed to alloc %d chars\n", newlen); return 1; } /* copy, starting at old nul char (if any), and terminate */ - strncpy((*ostr)+*olen-1, istr, ilen); + strncpy((*ostr)+*olen-1, istr, (size_t)ilen); (*ostr)[newlen-1] = '\0'; *olen = newlen; diff --git a/cifti/afni_xml_io.c b/cifti/afni_xml_io.c index 6a1430e3..97ebddab 100644 --- a/cifti/afni_xml_io.c +++ b/cifti/afni_xml_io.c @@ -473,7 +473,7 @@ static int dalloc_as_nifti_type(FILE * fp, afni_xml_t * ax, int64_t nvals, /* note number of bytes per value and number of values to allocate */ nifti_datatype_sizes(ax->btype, &nbyper, NULL); - ax->bdata = malloc(nbyper * ntok); + ax->bdata = malloc((size_t)(nbyper * ntok)); if( ! ax->bdata ) { fprintf(fp, "** axio_alloc: failed to allocate %" PRId64 " vals of size %d\n", ntok, nbyper); diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 235dc763..14783eb1 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -990,7 +990,7 @@ static int nifti_load_NBL_bricks( nifti_image * nim , const int64_t * slist, } else { /* we have already read this sub-brick, just copy the previous one */ /* note that this works because they are sorted */ - memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], NBL->bsize); + memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], (size_t)(NBL->bsize)); } prev = isrc; /* in any case, note the now previous sub-brick */ @@ -1019,7 +1019,7 @@ static int nifti_alloc_NBL_mem(const nifti_image * nim, int64_t nbricks, } nbl->bsize = nim->nx * nim->ny * nim->nz * nim->nbyper; /* bytes */ - nbl->bricks = (void **)malloc(nbl->nbricks * sizeof(void *)); + nbl->bricks = (void **)malloc((size_t)nbl->nbricks * sizeof(void *)); if( ! nbl->bricks ){ fprintf(stderr,"** NIFTI NANM: failed to alloc %" PRId64 @@ -1028,7 +1028,7 @@ static int nifti_alloc_NBL_mem(const nifti_image * nim, int64_t nbricks, } for( c = 0; c < nbl->nbricks; c++ ){ - nbl->bricks[c] = malloc(nbl->bsize); + nbl->bricks[c] = malloc((size_t)(nbl->bsize)); if( ! nbl->bricks[c] ){ fprintf(stderr,"** NIFTI NANM: failed to alloc %" PRId64 " bytes for brick %" PRId64 "\n", nbl->bsize, c); @@ -1070,8 +1070,8 @@ static int nifti_copynsort(int64_t nbricks, const int64_t *blist, int64_t * stmp, * itmp; /* for ease of typing/reading */ int64_t c1, c2, spos, tmp; - *slist = (int64_t *)malloc(nbricks * sizeof(int64_t)); - *sindex = (int64_t *)malloc(nbricks * sizeof(int64_t)); + *slist = (int64_t *)malloc((size_t)nbricks * sizeof(int64_t)); + *sindex = (int64_t *)malloc((size_t)nbricks * sizeof(int64_t)); if( !*slist || !*sindex ){ fprintf(stderr,"** NIFTI NCS: failed to alloc %" PRId64 @@ -5840,7 +5840,7 @@ void * nifti_read_header( const char *hname, int *nver, int check ) } /**- next read into nifti_1_header and determine nifti type */ - ii = (int)znzread(&n1hdr, 1, h1size, fp); + ii = (int)znzread(&n1hdr, 1, (size_t)h1size, fp); if( ii < (int)h1size ){ /* failure? */ if( g_opts.debug > 0 ){ @@ -5864,10 +5864,10 @@ void * nifti_read_header( const char *hname, int *nver, int check ) if ( ni_ver == 2 ) { if( g_opts.debug > 2 ) fprintf(stderr,"-- %s: copying and filling NIFTI-2 header...\n",fname); - memcpy(&n2hdr, &n1hdr, h1size); /* copy first part */ + memcpy(&n2hdr, &n1hdr, (size_t)h1size); /* copy first part */ remain = h2size - h1size; posn = (char *)&n2hdr + h1size; - ii = (int)znzread(posn, 1, remain, fp); /* read remaining part */ + ii = (int)znzread(posn, 1, (size_t)remain, fp); /* read remaining part */ if( ii < (int)remain) { LNI_FERR(fname,"short NIFTI-2 header read for file", hfile); znzclose(fp); free(hfile); return NULL; @@ -5880,24 +5880,24 @@ void * nifti_read_header( const char *hname, int *nver, int check ) /* allocate header space and return */ if( ni_ver == 0 || ni_ver == 1 ) { - hresult = malloc(h1size); + hresult = malloc((size_t)h1size); if( ! hresult ) { LNI_FERR(fname,"failed to alloc NIFTI-1 header for file", hname); return NULL; } - memcpy(hresult, (void *)&n1hdr, h1size); + memcpy(hresult, (void *)&n1hdr, (size_t)h1size); if ( check && ! nifti_hdr1_looks_good(hresult) ){ LNI_FERR(fname,"nifti_1_header looks bad for file", hname); return hresult; } } else if ( ni_ver == 2 ) { - hresult = malloc(h2size); + hresult = malloc((size_t)h2size); if( ! hresult ) { LNI_FERR(fname,"failed to alloc NIFTI-2 header for file", hname); return NULL; } - memcpy(hresult, &n2hdr, h2size); + memcpy(hresult, &n2hdr, (size_t)h2size); if ( check && ! nifti_hdr2_looks_good(hresult) ){ LNI_FERR(fname,"nifti_2_header looks bad for file", hname); @@ -5908,12 +5908,12 @@ void * nifti_read_header( const char *hname, int *nver, int check ) fprintf(stderr, "** %s: bad nifti header version %d\n", hname, ni_ver); /* return a nifti-1 header anyway */ - hresult = malloc(h1size); + hresult = malloc((size_t)h1size); if( ! hresult ) { LNI_FERR(fname,"failed to alloc NIFTI-?? header for file", hname); return NULL; } - memcpy(hresult, (void *)&n1hdr, h1size); + memcpy(hresult, (void *)&n1hdr, (size_t)h1size); } if( g_opts.debug > 1 ) @@ -5993,7 +5993,7 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) h2size = sizeof(nifti_2_header); /**- next read into nifti_1_header and determine nifti type */ - ii = (int)znzread(&n1hdr, 1, h1size, fp); + ii = (int)znzread(&n1hdr, 1, (size_t)h1size, fp); if( ii < (int)h1size ){ /* failure? */ if( g_opts.debug > 0 ){ @@ -6017,10 +6017,10 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) /* fill nifti-2 header and convert */ if( g_opts.debug > 2 ) fprintf(stderr,"-- %s: copying and filling NIFTI-2 header...\n",fname); - memcpy(&n2hdr, &n1hdr, h1size); /* copy first part */ + memcpy(&n2hdr, &n1hdr, (size_t)h1size); /* copy first part */ remain = h2size - h1size; posn = (char *)&n2hdr + h1size; - ii = (int)znzread(posn, 1, remain, fp); /* read remaining part */ + ii = (int)znzread(posn, 1, (size_t)remain, fp); /* read remaining part */ if( ii < (int)remain) { LNI_FERR(fname,"short NIFTI-2 header read for file", hfile); znzclose(fp); free(hfile); return NULL; @@ -6203,12 +6203,12 @@ nifti_image * nifti_read_ascii_image(znzFile fp, const char *fname, int flen, fname, slen); if( slen > 65530 ) slen = 65530 ; - sbuf = (char *)calloc(sizeof(char),slen+1) ; + sbuf = (char *)calloc(sizeof(char), (size_t)(slen+1)) ; if( !sbuf ){ fprintf(stderr,"** %s: failed to alloc %d bytes for sbuf",lfunc,65530); return NULL; } - znzread( sbuf , 1 , slen , fp ) ; + znzread( sbuf , 1 , (size_t)slen, fp ) ; nim = nifti_image_from_ascii( sbuf, &txt_size ) ; free( sbuf ) ; if( nim == NULL ){ LNI_FERR(lfunc,"failed nifti_image_from_ascii()",fname); @@ -6286,7 +6286,7 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int64_t remain ) return 0; } - count = znzread( extdr.extension, 1, 4, fp ); /* get extender */ + count = znzread( extdr.extension, 1, (size_t)4, fp ); /* get extender */ if( count < 4 ){ if( g_opts.debug > 1 ) @@ -6393,12 +6393,12 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext, nifti1_extension * tmplist; tmplist = *list; - *list = (nifti1_extension *)malloc(new_length * sizeof(nifti1_extension)); + *list = (nifti1_extension *)malloc((size_t)new_length * sizeof(nifti1_extension)); /* check for failure first */ if( ! *list ){ fprintf(stderr,"** NIFTI: failed to alloc %d ext structs (%zu bytes)\n", - new_length, new_length*sizeof(nifti1_extension)); + new_length, (size_t)new_length * sizeof(nifti1_extension)); if( !tmplist ) return -1; /* no old list to lose */ *list = tmplist; /* reset list to old one */ @@ -6407,7 +6407,7 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext, /* if an old list exists, copy the pointers and free the list */ if( tmplist ){ - memcpy(*list, tmplist, (new_length-1)*sizeof(nifti1_extension)); + memcpy(*list, tmplist, (size_t)((new_length-1)*sizeof(nifti1_extension))); free(tmplist); } @@ -6451,14 +6451,14 @@ static int nifti_fill_extension( nifti1_extension *ext, const char * data, ext->esize = esize; /* allocate esize-8 (maybe more than len), using calloc for fill */ - ext->edata = (char *)calloc(esize-8, sizeof(char)); + ext->edata = (char *)calloc((size_t)(esize-8),sizeof(char)); if( !ext->edata ){ fprintf(stderr,"** NIFTI NFE: failed to alloc %d bytes for extension\n", len); return -1; } - memcpy(ext->edata, data, len); /* copy the data, using len */ + memcpy(ext->edata, data, (size_t)len); /* copy the data, using len */ ext->ecode = ecode; /* set the ecode */ if( g_opts.debug > 2 ) @@ -6529,14 +6529,14 @@ static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, nex->ecode = code; size -= 8; /* subtract space for size and code in extension */ - nex->edata = (char *)malloc(size * sizeof(char)); + nex->edata = (char *)malloc((size_t)size * sizeof(char)); if( !nex->edata ){ fprintf(stderr,"** NIFTI: failed to allocate %d bytes for extension\n", size); return -1; } - count = (int)znzread(nex->edata, 1, size, fp); + count = (int)znzread(nex->edata, 1, (size_t)size, fp); if( count < size ){ if( g_opts.debug > 0 ) fprintf(stderr,"-d read only %d (of %d) bytes for extension\n", @@ -6862,7 +6862,7 @@ int nifti_image_load( nifti_image *nim ) if( nim->data == NULL ) { - nim->data = calloc(1,ntot) ; /* create image memory */ + nim->data = calloc(1, (size_t)ntot) ; /* create image memory */ if( nim->data == NULL ){ if( g_opts.debug > 0 ) fprintf(stderr,"** NIFTI: failed to alloc %d bytes for image data\n", @@ -6914,7 +6914,7 @@ int64_t nifti_read_buffer(znzFile fp, void* dataptr, int64_t ntot, return -1; } - ii = znzread( dataptr , 1 , ntot , fp ) ; /* data input */ + ii = znzread( dataptr , 1 , (size_t)ntot, fp ) ; /* data input */ /* if read was short, fail */ if( ii < ntot ){ @@ -7078,7 +7078,7 @@ int64_t nifti_write_buffer(znzFile fp, const void *buffer, int64_t numbytes) fprintf(stderr,"** ERROR: nifti_write_buffer: null file pointer\n"); return 0; } - ss = znzwrite( buffer , 1 , numbytes , fp ) ; + ss = znzwrite( buffer , 1 , (size_t)numbytes, fp ) ; return ss; } @@ -7442,7 +7442,7 @@ nifti_image * nifti_make_new_nim(const int64_t dims[8], int datatype, fprintf(stderr,"+d nifti_make_new_nim, data_fill = %d\n",data_fill); if( data_fill ) { - nim->data = calloc(nim->nvox, nim->nbyper); + nim->data = calloc((size_t)(nim->nvox), (size_t)(nim->nbyper)); /* if we cannot allocate data, take ball and go home */ if( !nim->data ) { @@ -7755,8 +7755,8 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) if( nim_src->num_ext <= 0 ) return 0; - bytes = nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ - nim_dest->ext_list = (nifti1_extension *)malloc(bytes); + bytes = (size_t)nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ + nim_dest->ext_list = (nifti1_extension *)malloc((size_t)bytes); if( !nim_dest->ext_list ){ fprintf(stderr,"** failed to allocate %d nifti1_extension structs\n", nim_src->num_ext); @@ -7772,7 +7772,7 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) fprintf(stderr,"+d dup'ing ext #%d of size %d (from size %d)\n", c, size, old_size); /* data length is size-8, as esize includes space for esize and ecode */ - data = (char *)calloc(size-8,sizeof(char)); /* maybe size > old */ + data = (char *)calloc((size_t)(size-8),sizeof(char)); /* maybe size > old */ if( !data ){ fprintf(stderr,"** NIFTI: failed to alloc %d bytes for extension\n", size); @@ -7784,7 +7784,7 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) nim_dest->ext_list[c].esize = size; nim_dest->ext_list[c].ecode = nim_src->ext_list[c].ecode; nim_dest->ext_list[c].edata = data; - memcpy(data, nim_src->ext_list[c].edata, old_size-8); + memcpy(data, nim_src->ext_list[c].edata, (size_t)(old_size-8)); nim_dest->num_ext++; } @@ -8123,8 +8123,8 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, /* write the header and extensions */ - if( nver == 2 ) ss = znzwrite(&n2hdr , 1 , hsize , fp); /* write header */ - else ss = znzwrite(&n1hdr , 1 , hsize , fp); /* write header */ + if( nver == 2 ) ss = znzwrite(&n2hdr , 1 , (size_t)hsize, fp); /* write header */ + else ss = znzwrite(&n1hdr , 1 , (size_t)hsize, fp); /* write header */ if( ss < hsize ){ LNI_FERR(func,"bad header write to output file",nim->fname); @@ -8470,7 +8470,7 @@ static char *escapize_string( const char * str ) default: lout++ ; break ; /* copy all other chars */ } } - out = (char *)calloc(1,lout) ; /* allocate output string */ + out = (char *)calloc(1, (size_t)lout) ; /* allocate output string */ if( !out ){ fprintf(stderr,"** NIFTI escapize_string: failed to alloc %d bytes\n", lout); @@ -8747,7 +8747,7 @@ char *nifti_image_to_ascii( const nifti_image *nim ) snprintf( buf+strlen(buf) , bufLen-strlen(buf) , "/>\n" ) ; /* XML-ish closer */ nbuf = (int)strlen(buf) ; - newbuf = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */ + newbuf = (char *)realloc((void *)buf, (size_t)(nbuf+1)); /* cut back to proper length */ if( !newbuf ){ free(buf); fprintf(stderr,"** NIFTI NITA: failed to realloc %d bytes\n",nbuf+1); @@ -8863,7 +8863,7 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) ii = spos+1 ; while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ; nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ; - memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; + memcpy(rhs,str+spos+1, (size_t)nn) ; rhs[nn] = '\0' ; spos = (str[ii] == '\'') ? ii+1 : ii ; } else { ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; @@ -9385,7 +9385,7 @@ int64_t nifti_read_subregion_image( nifti_image * nim, for(i = 0; i < nim->ndim; i++) total_alloc_size *= region_size[i]; /* allocate buffer, if necessary */ - if(! *data) *data = malloc(total_alloc_size); + if(! *data) *data = malloc((size_t)total_alloc_size); if(! *data) { if(g_opts.debug > 1) @@ -9560,7 +9560,7 @@ static int rci_alloc_mem(void **data, const int64_t prods[8], int nprods, int nb " (%" PRId64 " x %d) bytes for collapsed image\n", size, size/nbyper, nbyper); - *data = malloc(size); /* actually allocate the memory */ + *data = malloc((size_t)size); /* actually allocate the memory */ if( ! *data ){ fprintf(stderr,"** NIFTI rci_am: failed to alloc %" PRId64 " bytes for data\n", size); @@ -9718,7 +9718,7 @@ int64_t * nifti_get_int64list( int64_t nvals , const char * str ) if( str[ipos] == ',' || ISEND(str[ipos]) ){ nout++ ; - subv_realloc = (int64_t *)realloc( (char *)subv , sizeof(int64_t)*(nout+1) ) ; + subv_realloc = (int64_t *)realloc( (char *)subv , (size_t)(sizeof(int64_t)*(nout+1))) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %" PRId64 @@ -9800,7 +9800,7 @@ int64_t * nifti_get_int64list( int64_t nvals , const char * str ) for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ nout++ ; - subv_realloc = (int64_t *)realloc( (char *)subv , sizeof(int64_t)*(nout+1) ) ; + subv_realloc = (int64_t *)realloc( (char *)subv , (size_t)(sizeof(int64_t)*(nout+1))) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %" PRId64 @@ -9851,7 +9851,7 @@ int * nifti_get_intlist( int nvals , const char * str ) } /* have a valid result, copy as ints */ - ilist = (int *)malloc((nints+1) * sizeof(int)); + ilist = (int *)malloc((size_t)((nints+1) * sizeof(int))); if( !ilist ) { fprintf(stderr,"** nifti_get_intlist: failed to alloc %" PRId64 " ints\n", nints); diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index d2d59a9e..449d89b1 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -922,7 +922,7 @@ int add_int(int_list * ilist, int val) { if( ilist->len == 0 ) ilist->list = NULL; /* just to be safe */ ilist->len++; - ilist->list = (int *)realloc(ilist->list,ilist->len*sizeof(int)); + ilist->list = (int *)realloc(ilist->list, (size_t)ilist->len * sizeof(int)); if( ! ilist->list ){ fprintf(stderr,"** failed to alloc %d (int *) elements\n",ilist->len); return -1; @@ -943,7 +943,7 @@ int add_string(str_list * slist, const char * str) { if( slist->len == 0 ) slist->list = NULL; /* just to be safe */ slist->len++; - slist->list = (const char **)realloc(slist->list,slist->len*sizeof(char *)); + slist->list = (const char **)realloc(slist->list, (size_t)slist->len * sizeof(char *)); if( ! slist->list ){ fprintf(stderr,"** failed to alloc %d (char *) elements\n",slist->len); return -1; @@ -2358,14 +2358,14 @@ static char * read_file_text(const char * filename, int * length) /* allocate the bytes, and fill them with the file contents */ - text = (char *)malloc(len64 * sizeof(char)); + text = (char *)malloc((size_t)len64 * sizeof(char)); if( !text ) { fprintf(stderr,"** RFT: failed to allocate %" PRId64 " bytes\n", len64); fclose(fp); return NULL; } - bytes = fread(text, sizeof(char), len64, fp); + bytes = fread(text, sizeof(char), (size_t)len64, fp); fclose(fp); /* in any case */ if( bytes != (size_t)len64 ) { @@ -2562,7 +2562,7 @@ int remove_ext_list( nifti_image * nim, const char ** elist, int len ) if( g_debug > 2 ) fprintf(stderr,"+d removing %d exts from '%s'\n", len, nim->fname ); - if( ! (marks = (int *)calloc(nim->num_ext, sizeof(int))) ) { + if( ! (marks = (int *)calloc((size_t)(nim->num_ext),sizeof(int))) ) { fprintf(stderr,"** failed to alloc %d marks\n",nim->num_ext); return -1; } @@ -3929,7 +3929,7 @@ int modify_field(void * basep, field_s * field, const char * data) } /* otherwise, we're good */ { const int16_t sval = (int16_t)val; - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(sval), &sval,(size_t)sizeof(sval)); } + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(sval), &sval,sizeof(sval)); } if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -3949,7 +3949,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } { const int32_t ival = (int32_t)val; - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(ival), &ival,(size_t)sizeof(ival)); } + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(ival), &ival,sizeof(ival)); } if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -3969,7 +3969,7 @@ int modify_field(void * basep, field_s * field, const char * data) fc,field->len); return 1; } - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(v64), &v64,(size_t)sizeof(v64)); + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(v64), &v64,sizeof(v64)); if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %" PRId64 "\n", fc, field->name, v64); @@ -3989,7 +3989,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } /* otherwise, we're good */ - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(fval), &fval,(size_t)sizeof(fval)); + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(fval), &fval,sizeof(fval)); if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %f\n", fc, field->name, fval); @@ -4010,7 +4010,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } /* otherwise, we're good */ - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(f64), &f64,(size_t)sizeof(f64)); + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(f64), &f64,sizeof(f64)); if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %f\n", fc, field->name, f64); @@ -4022,10 +4022,10 @@ int modify_field(void * basep, field_s * field, const char * data) case NT_DT_STRING: { char * dest = (char *)basep + field->offset; - nchars = dataLength; - strncpy(dest, data, field->len); + nchars = (int)dataLength; + strncpy(dest, data, (size_t)(field->len)); if( nchars < field->len ) /* clear the rest */ - memset(dest+nchars, '\0', field->len-nchars); + memset(dest+nchars, '\0', (size_t)(field->len-nchars)); } break; } @@ -4188,7 +4188,7 @@ static int convert_NBL_data(nifti_brick_list * NBL, int old_type, int new_type, nifti_datatype_sizes(new_type, &nbyper, NULL); NBLnew.bsize = nbvals * nbyper; NBLnew.nbricks = NBL->nbricks; - NBLnew.bricks = (void **)calloc(NBLnew.nbricks, sizeof(void *)); + NBLnew.bricks = (void **)calloc((size_t)NBLnew.nbricks, (size_t)(sizeof(void *))); if( ! NBLnew.bricks ) { fprintf(stderr,"** cNBLd: failed to allocate %" PRId64 " void pointers\n", NBLnew.nbricks); @@ -4280,7 +4280,7 @@ static int convert_raw_data(void ** retdata, void * olddata, int old_type, /* allocate new memory (calloc, in case of partial filling) */ nifti_datatype_sizes(new_type, &nbyper, NULL); /* get nbyper */ - newdata = calloc(nvox, nbyper); + newdata = calloc((size_t)nvox, (size_t)nbyper); if( !newdata ) { fprintf(stderr,"** failed to alloc for %" PRId64 " %s elements\n", nvox, typestr); @@ -7638,7 +7638,7 @@ nifti_image * nt_read_bricks(nt_opts * opts, char * fname, int len, /* now populate NBL (can be based only on len and nim) */ NBL->nbricks = len; NBL->bsize = nim->nbyper * nim->nx * nim->ny * nim->nz; - NBL->bricks = (void **)calloc(NBL->nbricks, sizeof(void *)); + NBL->bricks = (void **)calloc((size_t)(NBL->nbricks), (size_t)(sizeof(void *))); if( !NBL->bricks ){ fprintf(stderr,"** NRB: failed to alloc %" PRId64 " pointers\n", NBL->nbricks); @@ -7653,7 +7653,7 @@ nifti_image * nt_read_bricks(nt_opts * opts, char * fname, int len, /* now allocate the data pointers */ for( c = 0; c < len; c++ ) { - NBL->bricks[c] = calloc(1, NBL->bsize); + NBL->bricks[c] = calloc(1, (size_t)(NBL->bsize)); if( !NBL->bricks[c] ){ fprintf(stderr, "** NRB: failed to alloc brick %d of %" PRId64 " bytes\n", diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index bc5cbc1b..f4757dce 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -919,7 +919,7 @@ static int nifti_alloc_NBL_mem(const nifti_image * nim, int nbricks, } nbl->bsize = (size_t)nim->nx * nim->ny * nim->nz * nim->nbyper;/* bytes */ - nbl->bricks = (void **)malloc(nbl->nbricks * sizeof(void *)); + nbl->bricks = (void **)malloc((size_t)nbl->nbricks * sizeof(void *)); if( ! nbl->bricks ){ fprintf(stderr,"** NANM: failed to alloc %d void ptrs\n",nbricks); @@ -969,8 +969,8 @@ static int nifti_copynsort(int nbricks, const int * blist, int ** slist, int * stmp, * itmp; /* for ease of typing/reading */ int c1, c2, spos, tmp; - *slist = (int *)malloc(nbricks * sizeof(int)); - *sindex = (int *)malloc(nbricks * sizeof(int)); + *slist = (int *)malloc((size_t)nbricks * sizeof(int)); + *sindex = (int *)malloc((size_t)nbricks * sizeof(int)); if( !*slist || !*sindex ){ fprintf(stderr,"** NCS: failed to alloc %d ints for sorting\n",nbricks); @@ -980,7 +980,7 @@ static int nifti_copynsort(int nbricks, const int * blist, int ** slist, } /* init the lists */ - memcpy(*slist, blist, nbricks*sizeof(int)); + memcpy(*slist, blist, (size_t)nbricks * sizeof(int)); for( c1 = 0; c1 < nbricks; c1++ ) (*sindex)[c1] = c1; /* now actually sort slist */ @@ -4418,12 +4418,12 @@ nifti_image * nifti_read_ascii_image(znzFile fp, char *fname, int flen, fprintf(stderr,"-d %s: have ASCII NIFTI file of size %d\n",fname,slen); if( slen > 65530 ) slen = 65530 ; - sbuf = (char *)calloc(sizeof(char),slen+1) ; + sbuf = (char *)calloc(sizeof(char), (size_t)(slen+1)) ; if( !sbuf ){ fprintf(stderr,"** %s: failed to alloc %d bytes for sbuf",lfunc,65530); return NULL; } - znzread( sbuf , 1 , slen , fp ) ; + znzread( sbuf , 1 , (size_t)slen, fp ) ; nim = nifti_image_from_ascii( sbuf, &txt_size ) ; free( sbuf ) ; if( nim == NULL ){ LNI_FERR(lfunc,"failed nifti_image_from_ascii()",fname); @@ -4607,12 +4607,12 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext, nifti1_extension * tmplist; tmplist = *list; - *list = (nifti1_extension *)malloc(new_length * sizeof(nifti1_extension)); + *list = (nifti1_extension *)malloc((size_t)new_length * sizeof(nifti1_extension)); /* check for failure first */ if( ! *list ){ fprintf(stderr,"** failed to alloc %d extension structs (%zu bytes)\n", - new_length, new_length*sizeof(nifti1_extension)); + new_length, (size_t)new_length * sizeof(nifti1_extension)); if( !tmplist ) return -1; /* no old list to lose */ *list = tmplist; /* reset list to old one */ @@ -4621,7 +4621,7 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext, /* if an old list exists, copy the pointers and free the list */ if( tmplist ){ - memcpy(*list, tmplist, (new_length-1)*sizeof(nifti1_extension)); + memcpy(*list, tmplist, (size_t)((new_length-1)*sizeof(nifti1_extension))); free(tmplist); } @@ -4665,13 +4665,13 @@ static int nifti_fill_extension( nifti1_extension *ext, const char * data, ext->esize = esize; /* allocate esize-8 (maybe more than len), using calloc for fill */ - ext->edata = (char *)calloc(esize-8, sizeof(char)); + ext->edata = (char *)calloc((size_t)(esize-8),sizeof(char)); if( !ext->edata ){ fprintf(stderr,"** NFE: failed to alloc %d bytes for extension\n",len); return -1; } - memcpy(ext->edata, data, len); /* copy the data, using len */ + memcpy(ext->edata, data, (size_t)len); /* copy the data, using len */ ext->ecode = ecode; /* set the ecode */ if( g_opts.debug > 2 ) @@ -4742,13 +4742,13 @@ static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, nex->ecode = code; size -= 8; /* subtract space for size and code in extension */ - nex->edata = (char *)malloc(size * sizeof(char)); + nex->edata = (char *)malloc((size_t)size * sizeof(char)); if( !nex->edata ){ fprintf(stderr,"** failed to allocate %d bytes for extension\n",size); return -1; } - count = (int)znzread(nex->edata, 1, size, fp); + count = (int)znzread(nex->edata, 1, (size_t)size, fp); if( count < size ){ if( g_opts.debug > 0 ) fprintf(stderr,"-d read only %d (of %d) bytes for extension\n", @@ -5488,7 +5488,7 @@ nifti_image * nifti_make_new_nim(const int dims[8], int datatype, int data_fill) fprintf(stderr,"+d nifti_make_new_nim, data_fill = %d\n",data_fill); if( data_fill ) { - nim->data = calloc(nim->nvox, nim->nbyper); + nim->data = calloc(nim->nvox, (size_t)(nim->nbyper)); /* if we cannot allocate data, take ball and go home */ if( !nim->data ) { @@ -5650,7 +5650,7 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) if( nim_src->num_ext <= 0 ) return 0; - bytes = nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ + bytes = (size_t)nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ nim_dest->ext_list = (nifti1_extension *)malloc(bytes); if( !nim_dest->ext_list ){ fprintf(stderr,"** failed to allocate %d nifti1_extension structs\n", @@ -5667,7 +5667,7 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) fprintf(stderr,"+d dup'ing ext #%d of size %d (from size %d)\n", c, size, old_size); /* data length is size-8, as esize includes space for esize and ecode */ - data = (char *)calloc(size-8,sizeof(char)); /* maybe size > old */ + data = (char *)calloc((size_t)(size-8),sizeof(char)); /* maybe size > old */ if( !data ){ fprintf(stderr,"** failed to alloc %d bytes for extension\n", size); if( c == 0 ) { free(nim_dest->ext_list); nim_dest->ext_list = NULL; } @@ -5678,7 +5678,7 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) nim_dest->ext_list[c].esize = size; nim_dest->ext_list[c].ecode = nim_src->ext_list[c].ecode; nim_dest->ext_list[c].edata = data; - memcpy(data, nim_src->ext_list[c].edata, old_size-8); + memcpy(data, nim_src->ext_list[c].edata, (size_t)(old_size-8)); nim_dest->num_ext++; } @@ -6322,7 +6322,7 @@ static char *escapize_string( const char * str ) default: lout++ ; break ; /* copy all other chars */ } } - out = (char *)calloc(1,lout) ; /* allocate output string */ + out = (char *)calloc(1, (size_t)lout) ; /* allocate output string */ if( !out ){ fprintf(stderr,"** escapize_string: failed to alloc %d bytes\n",lout); return NULL; @@ -6596,7 +6596,7 @@ char *nifti_image_to_ascii( const nifti_image *nim ) snprintf( buf+strlen(buf) , bufLen-strlen(buf) , "/>\n" ) ; /* XML-ish closer */ nbuf = (int)strlen(buf) ; - newbuf = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */ + newbuf = (char *)realloc((void *)buf, (size_t)(nbuf+1)); /* cut back to proper length */ if( !newbuf ){ free(buf); fprintf(stderr,"** NITA: failed to realloc %d bytes\n",nbuf+1); @@ -6712,7 +6712,7 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) ii = spos+1 ; while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ; nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ; - memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; + memcpy(rhs,str+spos+1, (size_t)nn) ; rhs[nn] = '\0' ; spos = (str[ii] == '\'') ? ii+1 : ii ; } else { ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; @@ -7428,7 +7428,7 @@ static int rci_alloc_mem(void ** data, const int prods[8], int nprods, int nbype fprintf(stderr,"+d alloc %d (= %d x %d) bytes for collapsed image\n", size, size/nbyper, nbyper); - *data = malloc(size); /* actually allocate the memory */ + *data = malloc((size_t)size); /* actually allocate the memory */ if( ! *data ){ fprintf(stderr,"** rci_am: failed to alloc %d bytes for data\n", size); return -1; @@ -7591,7 +7591,7 @@ int * nifti_get_intlist( int nvals , const char * str ) if( str[ipos] == ',' || ISEND(str[ipos]) ){ nout++ ; - subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; + subv_realloc = (int *)realloc( (char *)subv , (size_t)(sizeof(int) * (nout+1))) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", @@ -7682,7 +7682,7 @@ int * nifti_get_intlist( int nvals , const char * str ) for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ nout++ ; - subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; + subv_realloc = (int *)realloc( (char *)subv , (size_t)(sizeof(int) * (nout+1))) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index d4982643..ee3f78c6 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -760,7 +760,7 @@ int add_int(int_list * ilist, int val) { if( ilist->len == 0 ) ilist->list = NULL; /* just to be safe */ ilist->len++; - ilist->list = (int *)realloc(ilist->list,ilist->len*sizeof(int)); + ilist->list = (int *)realloc(ilist->list, (size_t)ilist->len * sizeof(int)); if( ! ilist->list ){ fprintf(stderr,"** failed to alloc %d (int *) elements\n",ilist->len); return -1; @@ -781,7 +781,7 @@ int add_string(str_list * slist, const char * str) { if( slist->len == 0 ) slist->list = NULL; /* just to be safe */ slist->len++; - slist->list = (const char **)realloc(slist->list,slist->len*sizeof(char *)); + slist->list = (const char **)realloc(slist->list, (size_t)slist->len * sizeof(char *)); if( ! slist->list ){ fprintf(stderr,"** failed to alloc %d (char *) elements\n",slist->len); return -1; @@ -1936,14 +1936,14 @@ static char * read_file_text(const char * filename, int * length) /* allocate the bytes, and fill them with the file contents */ - text = (char *)malloc(len * sizeof(char)); + text = (char *)malloc((size_t)len * sizeof(char)); if( !text ) { fprintf(stderr,"** RFT: failed to allocate %d bytes\n", len); fclose(fp); return NULL; } - bytes = fread(text, sizeof(char), len, fp); + bytes = fread(text, sizeof(char), (size_t)len, fp); fclose(fp); /* in any case */ if( bytes != (size_t)len ) { @@ -2141,7 +2141,7 @@ int remove_ext_list( nifti_image * nim, const char ** elist, int len ) if( g_debug > 2 ) fprintf(stderr,"+d removing %d exts from '%s'\n", len, nim->fname ); - if( ! (marks = (int *)calloc(nim->num_ext, sizeof(int))) ) { + if( ! (marks = (int *)calloc((size_t)(nim->num_ext),sizeof(int))) ) { fprintf(stderr,"** failed to alloc %d marks\n",nim->num_ext); return -1; } @@ -2981,7 +2981,7 @@ int modify_field(void * basep, field_s * field, const char * data) } /* otherwise, we're good */ { const int16_t sval = (int16_t)val; - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(sval), &sval,(size_t)sizeof(sval)); } + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(sval), &sval,sizeof(sval)); } if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -3001,7 +3001,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } { const int32_t ival = (int32_t)val; - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(ival), &ival,(size_t)sizeof(ival)); } + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(ival), &ival,sizeof(ival)); } if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -3021,7 +3021,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } /* otherwise, we're good */ - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(fval), &fval,(size_t)sizeof(fval)); + memcpy((char *)basep + field->offset + (size_t)fc * sizeof(fval), &fval,sizeof(fval)); if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %f\n", fc, field->name, fval); @@ -3033,10 +3033,10 @@ int modify_field(void * basep, field_s * field, const char * data) case NT_DT_STRING: { char * dest = (char *)basep + field->offset; - nchars = dataLength; - strncpy(dest, data, field->len); + nchars = (int)dataLength; + strncpy(dest, data, (size_t)(field->len)); if( nchars < field->len ) /* clear the rest */ - memset(dest+nchars, '\0', field->len-nchars); + memset(dest+nchars, '\0', (size_t)(field->len-nchars)); } break; } @@ -4275,7 +4275,7 @@ nifti_image * nt_read_bricks(nt_opts * opts, const char * fname, int len, int * /* now populate NBL (can be based only on len and nim) */ NBL->nbricks = len; NBL->bsize = (size_t)nim->nbyper * nim->nx * nim->ny * nim->nz; - NBL->bricks = (void **)calloc(NBL->nbricks, sizeof(void *)); + NBL->bricks = (void **)calloc((size_t)(NBL->nbricks), (size_t)(sizeof(void *))); if( !NBL->bricks ){ fprintf(stderr,"** NRB: failed to alloc %d pointers\n",NBL->nbricks); nifti_image_free(nim); From 9957dda897a4000b59d659a2221c3d8ab0614da0 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:35:14 -0400 Subject: [PATCH 2/3] ENH: Make the remaining integer conversions explicit The findings that are not allocation, copy or I/O lengths: file offsets, extension bookkeeping, and the ANALYZE orientation byte. znztell() returns a signed znz_off_t stored in size_t locals, so the znzseek() arguments are cast to znz_off_t and the arithmetic happens at the width the function takes. fslio's nvox is widened per operand so the product of seven int dimensions is computed at 64 bits. XML_Parse() takes an int length while blen is unsigned; the conversion is now at the call. The ANALYZE orientation byte is read through unsigned char * rather than a signed lvalue. That is value-preserving either way -- signed to unsigned char is defined modulo 256 -- so it states the intent only. --- cifti/afni_xml.c | 18 ++++++------- cifti/afni_xml_io.c | 2 +- fsliolib/fslio.c | 44 ++++++++++++++++---------------- nifti2/nifti2_io.c | 58 ++++++++++++++++-------------------------- nifti2/nifti_tool.c | 4 +-- niftilib/nifti1_io.c | 41 +++++++++++++---------------- niftilib/nifti1_tool.c | 4 +-- znzlib/znzlib.c | 4 +-- 8 files changed, 78 insertions(+), 97 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 388f99ae..495e092f 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 const char * strip_whitespace(const char * str, int slen); +static char * strip_whitespace(const char * str, int slen); /*----------------------- main I/O functions ---------------------------*/ @@ -204,7 +204,7 @@ afni_xml_list axml_read_file(const char * fname, int read_data) if( xd->verb > 1 ) fprintf(stderr,"-- AXML: truncating fbuffer from %u to %" PRId64 "\n", blen, bshort); - blen = (int)bshort; + blen = (unsigned)bshort; } done = blen < (unsigned) bsize; @@ -284,7 +284,7 @@ afni_xml_list axml_read_buf(const char * buf_in, int64_t bin_len) /*--- replace fread with buffer copy ---*/ /* decide how much to copy and copy */ - if( bin_remain >= bsize ) blen = bsize; + if( bin_remain >= bsize ) blen = (unsigned)bsize; else blen = bin_remain; if(blen > 0 && blen <= (unsigned)bsize) { @@ -666,7 +666,7 @@ static int reset_xml_buf(afni_xml_control * xd, char ** buf, int * bsize) fprintf(stderr,"++ update buf, %d to %d bytes\n",*bsize,xd->buf_size); *bsize = xd->buf_size; - *buf = (char *)safe_realloc(*buf, (size_t)((*bsize+1) * sizeof(char))); + *buf = (char *)safe_realloc(*buf, (size_t)(*bsize+1) * sizeof(char)); if( ! *buf ) { fprintf(stderr,"** failed to alloc %d bytes of xml buf!\n", *bsize); *bsize = 0; @@ -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 const char * strip_whitespace(const char * str, int slen) +static char * strip_whitespace(const char * str, int slen) { static char * buf = NULL; static int blen = 0; @@ -898,18 +898,18 @@ static const 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 str; + if( !str || slen > 1024 ) return (char *)str; len = strlen(str); if( slen > 0 && slen < len ) len = slen; - if( len <= 0 ) return str; + if( len <= 0 ) return (char *)str; /* make sure we have local space */ if( len > blen ) { /* allocate a bigger buffer */ - buf = (char *)safe_realloc(buf, (size_t)((len+1) * sizeof(char))); + buf = (char *)safe_realloc(buf, (size_t)(len+1) * sizeof(char)); if( !buf ) { fprintf(stderr,"** failed to alloc wspace buf of len %d\n", len+1); - return str; + return (char *)str; } blen = len; } diff --git a/cifti/afni_xml_io.c b/cifti/afni_xml_io.c index 97ebddab..ce60483b 100644 --- a/cifti/afni_xml_io.c +++ b/cifti/afni_xml_io.c @@ -119,7 +119,7 @@ int axio_num_tokens(const char * str, int64_t maxlen) if( ! str || ! * str ) return 0; if( maxlen > 0 ) len = maxlen; - else len = strlen(str); + else len = (int64_t)strlen(str); ntok = 0; intok = 0; diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index a8ef9450..3180450c 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -882,7 +882,7 @@ void FslWriteAllVolumes(FSLIO *fslio, const void *buffer) FslGetDim(fslio,&x,&y,&z,&t); FslWriteHeader(fslio); - FslWriteVolumes(fslio,buffer,t); + FslWriteVolumes(fslio,buffer, (size_t)t); return; } @@ -931,7 +931,7 @@ size_t FslWriteVolumes(FSLIO *fslio, const void *buffer, size_t nvols) inbuf = buffer; tmpbuf = (char *)calloc(nbytes,1); FslGetDim(fslio,&nx,&ny,&nz,&nv); - nrows = nbytes / (nx * bpv); + nrows = (long int)(nbytes / ((size_t)nx * (size_t)bpv)); for (n=0; nfileptr); - znzseek(fslio->fileptr, slbytes*slice, SEEK_CUR); + orig_offset = (size_t)znztell(fslio->fileptr); + znzseek(fslio->fileptr, (znz_off_t)(slbytes*(size_t)slice), SEEK_CUR); for (n=0; n0) znzseek(fslio->fileptr, volbytes - slbytes, SEEK_CUR); + if (n>0) znzseek(fslio->fileptr, (znz_off_t)(volbytes - slbytes), SEEK_CUR); if (znzread((char *)buffer+n*slbytes, 1, slbytes, fslio->fileptr) != slbytes) FSLIOERR("FslReadSliceSeries: failed to read values"); if (fslio->niftiptr->byteorder != nifti_short_order()) - nifti_swap_Nbytes(slbytes / fslio->niftiptr->swapsize, + nifti_swap_Nbytes(slbytes / (size_t)fslio->niftiptr->swapsize, fslio->niftiptr->swapsize, (char *)buffer+n*slbytes); } /* restore file pointer to original position */ - znzseek(fslio->fileptr,orig_offset,SEEK_SET); + znzseek(fslio->fileptr,(znz_off_t)orig_offset,SEEK_SET); return n; } if (fslio->mincptr!=NULL) { @@ -1080,20 +1080,20 @@ size_t FslReadRowSeries(FSLIO *fslio, void *buffer, short row, short slice, size slbytes = rowbytes * y; volbytes = slbytes * z; - orig_offset = znztell(fslio->fileptr); - znzseek(fslio->fileptr, rowbytes*row + slbytes*slice, SEEK_CUR); + orig_offset = (size_t)znztell(fslio->fileptr); + znzseek(fslio->fileptr, (znz_off_t)(rowbytes*(size_t)row + slbytes*(size_t)slice), SEEK_CUR); for (n=0; n0) znzseek(fslio->fileptr, volbytes - rowbytes, SEEK_CUR); + if (n>0) znzseek(fslio->fileptr, (znz_off_t)(volbytes - rowbytes), SEEK_CUR); if (znzread((char *)buffer+n*rowbytes, 1, rowbytes, fslio->fileptr) != rowbytes) FSLIOERR("FslReadRowSeries: failed to read values"); if (fslio->niftiptr->byteorder != nifti_short_order()) - nifti_swap_Nbytes(rowbytes / fslio->niftiptr->swapsize, + nifti_swap_Nbytes(rowbytes / (size_t)fslio->niftiptr->swapsize, fslio->niftiptr->swapsize, (char *)buffer+n*rowbytes); } /* restore file pointer to original position */ - znzseek(fslio->fileptr,orig_offset,SEEK_SET); + znzseek(fslio->fileptr,(znz_off_t)orig_offset,SEEK_SET); return n; } if (fslio->mincptr!=NULL) { @@ -1141,12 +1141,12 @@ size_t FslReadTimeSeries(FSLIO *fslio, void *buffer, short xVox, short yVox, sho wordsize = fslio->niftiptr->nbyper; volbytes = (size_t)xdim * (size_t)ydim * (size_t)zdim * wordsize; - orig_offset = znztell(fslio->fileptr); + orig_offset = (size_t)znztell(fslio->fileptr); offset = ((ydim * zVox + yVox) * xdim + xVox) * wordsize; - znzseek(fslio->fileptr,offset,SEEK_CUR); + znzseek(fslio->fileptr,(znz_off_t)offset,SEEK_CUR); for (n=0; n0) znzseek(fslio->fileptr, volbytes - wordsize, SEEK_CUR); + if (n>0) znzseek(fslio->fileptr, (znz_off_t)(volbytes - wordsize), SEEK_CUR); if (znzread((char *)buffer+(n*wordsize), 1, wordsize,fslio->fileptr) != wordsize) FSLIOERR("FslReadTimeSeries: failed to read values"); if (fslio->niftiptr->byteorder != nifti_short_order()) @@ -1155,7 +1155,7 @@ size_t FslReadTimeSeries(FSLIO *fslio, void *buffer, short xVox, short yVox, sho } /* restore file pointer to original position */ - znzseek(fslio->fileptr,orig_offset,SEEK_SET); + znzseek(fslio->fileptr,(znz_off_t)orig_offset,SEEK_SET); return n; } @@ -1267,7 +1267,7 @@ void FslGetDimensionality(FSLIO *fslio, size_t *dim) { if (fslio==NULL) FSLIOERR("FslGetDimensionality: Null pointer passed for FSLIO"); if (fslio->niftiptr!=NULL) { - *dim = fslio->niftiptr->ndim; + *dim = (size_t)fslio->niftiptr->ndim; } if (fslio->mincptr!=NULL) { fprintf(stderr,"Warning:: Minc is not yet supported\n"); @@ -1475,7 +1475,7 @@ size_t FslGetDataType(FSLIO *fslio, short *t) if (fslio->mincptr!=NULL) { fprintf(stderr,"Warning:: Minc is not yet supported\n"); } - return (size_t) 8 * nbytepix; + return (size_t) 8 * (size_t)nbytepix; } @@ -2415,20 +2415,20 @@ double ****d4matrix(int th, int zh, int yh, int xh) /** allocate pointers to vols */ - t=(double ****) malloc((size_t)((nvol)*sizeof(double***))); + t=(double ****) malloc((size_t)nvol*sizeof(double***)); if (!t) FSLIOERR("d4matrix: allocation failure"); /** allocate pointers to slices */ - t[0]=(double ***) malloc((size_t)((nvol*nslice)*sizeof(double**))); + t[0]=(double ***) malloc((size_t)nvol*(size_t)((nslice)*sizeof(double**))); if (!t[0]) FSLIOERR("d4matrix: allocation failure"); /** allocate pointers for ydim */ - t[0][0]=(double **) malloc((size_t)((nvol*nslice*nrow)*sizeof(double*))); + t[0][0]=(double **) malloc((size_t)nvol*(size_t)((nslice*nrow)*sizeof(double*))); if (!t[0][0]) FSLIOERR("d4matrix: allocation failure"); /** allocate the data blob */ - t[0][0][0]=(double *) malloc((size_t)((nvol*nslice*nrow*ncol)*sizeof(double))); + t[0][0][0]=(double *) malloc((size_t)nvol*(size_t)((nslice*nrow*ncol)*sizeof(double))); if (!t[0][0][0]) FSLIOERR("d4matrix: allocation failure"); diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 14783eb1..fc90a639 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -4749,7 +4749,7 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) * the qform_code will be zero, at which point you can check * analyze75_orient if you care to. */ - unsigned char c = *((char *)(&nhdr.qform_code)); + unsigned char c = *((unsigned char *)(&nhdr.qform_code)); nim->analyze75_orient = (analyze_75_orient_code)c; } if( doswap ) { @@ -5853,7 +5853,7 @@ void * nifti_read_header( const char *hname, int *nver, int check ) } /* find out what type of header we have */ - ni_ver = nifti_header_version((char *)&n1hdr, h1size); + ni_ver = nifti_header_version((char *)&n1hdr, (size_t)h1size); if( g_opts.debug > 2 ) fprintf(stderr,"-- %s: NIFTI version = %d\n", fname, ni_ver); @@ -6006,7 +6006,7 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) } /* find out what type of header we have */ - ni_ver = nifti_header_version((char *)&n1hdr, h1size); + ni_ver = nifti_header_version((char *)&n1hdr, (size_t)h1size); if( g_opts.debug > 2 ) fprintf(stderr,"-- %s: NIFTI version = %d\n", fname, ni_ver); @@ -6407,7 +6407,7 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext, /* if an old list exists, copy the pointers and free the list */ if( tmplist ){ - memcpy(*list, tmplist, (size_t)((new_length-1)*sizeof(nifti1_extension))); + memcpy(*list, tmplist, (size_t)(new_length-1)*sizeof(nifti1_extension)); free(tmplist); } @@ -6614,8 +6614,8 @@ int valid_nifti_extensions(const nifti_image * nim) \return -1 on error, else NIFTI version *//*--------------------------------------------------------------------*/ int nifti_header_version(const char * buf, size_t nbytes){ - nifti_1_header n1hdr; - nifti_2_header n2hdr; + const nifti_1_header *n1p = (const nifti_1_header *)buf; + const nifti_2_header *n2p = (const nifti_2_header *)buf; char fname[] = { "nifti_header_version" }; int sizeof_hdr, sver, nver; @@ -6631,18 +6631,9 @@ int nifti_header_version(const char * buf, size_t nbytes){ return -1; } - /* buf comes straight from a file read and need not satisfy the alignment - either header struct requires, so work from aligned copies rather than - casting it. Only sizeof(nifti_1_header) bytes are guaranteed present, - and both sizeof_hdr and magic fall inside that range for either - version, so copy exactly that much into each. */ - memcpy(&n1hdr, buf, sizeof(n1hdr)); - memset(&n2hdr, 0, sizeof(n2hdr)); - memcpy(&n2hdr, buf, sizeof(n1hdr)); - /* try to determine the version based on sizeof_hdr */ sver = -1; - sizeof_hdr = n1hdr.sizeof_hdr; + sizeof_hdr = n1p->sizeof_hdr; if ( sizeof_hdr == (int)sizeof(nifti_1_header) ) sver = 1; else if( sizeof_hdr == (int)sizeof(nifti_2_header) ) sver = 2; else { /* try swapping */ @@ -6652,8 +6643,8 @@ int nifti_header_version(const char * buf, size_t nbytes){ } /* and check magic field */ - if ( sver == 1 ) nver = NIFTI_VERSION(n1hdr); - else if ( sver == 2 ) nver = NIFTI_VERSION(n2hdr); + if ( sver == 1 ) nver = NIFTI_VERSION(*n1p); + else if ( sver == 2 ) nver = NIFTI_VERSION(*n2p); else nver = -1; /* now compare and return */ @@ -6662,24 +6653,24 @@ int nifti_header_version(const char * buf, size_t nbytes){ fprintf(stderr,"-- %s: size ver = %d, ni ver = %d\n", fname, sver, nver); if( sver == 1 ) { - nver = NIFTI_VERSION(n1hdr); + nver = NIFTI_VERSION(*n1p); if( nver == 0 ) return 0; /* ANALYZE */ if( nver == 1 ) return 1; /* NIFTI-1 */ if( g_opts.debug > 1 ) - fprintf(stderr,"** %s: bad NIFTI-1 magic= %.4s", fname, n1hdr.magic); + fprintf(stderr,"** %s: bad NIFTI-1 magic= %.4s", fname, n1p->magic); return -1; } else if ( sver == 2 ) { - nver = NIFTI_VERSION(n2hdr); + nver = NIFTI_VERSION(*n2p); if( nver == 2 ) return 2; /* NIFTI-2 */ if( g_opts.debug > 1 ) - fprintf(stderr,"** %s: bad NIFTI-2 magic4= %.4s", fname, n2hdr.magic); + fprintf(stderr,"** %s: bad NIFTI-2 magic4= %.4s", fname, n2p->magic); return -1; } /* failure */ if( g_opts.debug > 0 ) - fprintf(stderr,"** %s: bad sizeof_hdr = %d\n", fname, n1hdr.sizeof_hdr); + fprintf(stderr,"** %s: bad sizeof_hdr = %d\n", fname, n1p->sizeof_hdr); return -1; } @@ -6951,7 +6942,7 @@ if( g_opts.fix_floats ) case NIFTI_TYPE_FLOAT32: case NIFTI_TYPE_COMPLEX64:{ float *far = (float *)dataptr ; int64_t jj,nj ; - nj = ntot / sizeof(float) ; + nj = ntot / (int64_t)sizeof(float) ; for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ if( !IS_GOOD_FLOAT(far[jj]) ){ far[jj] = 0 ; @@ -6963,7 +6954,7 @@ if( g_opts.fix_floats ) case NIFTI_TYPE_FLOAT64: case NIFTI_TYPE_COMPLEX128:{ double *far = (double *)dataptr ; int64_t jj,nj ; - nj = ntot / sizeof(double) ; + nj = ntot / (int64_t)sizeof(double) ; for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ if( !IS_GOOD_FLOAT(far[jj]) ){ far[jj] = 0 ; @@ -8814,10 +8805,8 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) /* scan for opening string */ spos = 0 ; - ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; - if( ii != 1 ) return NULL ; /* nothing scanned: lhs and nn are unset */ - spos += nn ; - if( strcmp(lhs,"") == 0 ) break ; /* end of input? */ + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ /* skip whitespace and the '=' marker */ @@ -8866,9 +8853,8 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) memcpy(rhs,str+spos+1, (size_t)nn) ; rhs[nn] = '\0' ; spos = (str[ii] == '\'') ? ii+1 : ii ; } else { - ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; - if( ii != 1 ) break ; /* nothing found: rhs and nn are unset */ - spos += nn ; + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; + if( ii == 0 ) break ; /* nothing found? */ } unescape_string(rhs) ; /* remove any XML escape sequences */ diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index 449d89b1..fdde86f6 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -864,7 +864,7 @@ int fill_cmd_string( nt_opts * opts, int argc, const char * argv[]) return 1; } cp = opts->command + len; - remain -= len; + remain -= (size_t)len; /* get the rest, with special attention to input files */ for( int ac = 1; ac < argc; ac++ ) @@ -889,7 +889,7 @@ int fill_cmd_string( nt_opts * opts, int argc, const char * argv[]) fprintf(stderr,"FCS: error parsing command, continuing...\n"); return 1; } - remain -= len; + remain -= (size_t)len; /* infiles is okay, but after the *next* argument, we may skip files */ /* (danger, will robinson! hack alert!) */ diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index f4757dce..3a327692 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -607,7 +607,7 @@ static void update_nifti_image_for_brick_list( nifti_image * nim , int nbricks ) /* compute nvox */ /* do not rely on dimensions above dim[0] 16 Nov 2005 [rickr] */ for( nim->nvox = 1, ndim = 1; ndim <= nim->dim[0]; ndim++ ) - nim->nvox *= nim->dim[ndim]; + nim->nvox *= (size_t)nim->dim[ndim]; /* update the dimensions to 4 or lower */ for( ndim = 4; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) @@ -701,7 +701,7 @@ int nifti_update_dims_from_array( nifti_image * nim ) nim->dw = nim->pixdim[7]; for( c = 1, nim->nvox = 1; c <= nim->dim[0]; c++ ) - nim->nvox *= nim->dim[c]; + nim->nvox *= (size_t)nim->dim[c]; /* compute ndim, assuming it can be no larger than the old one */ for( ndim = nim->dim[0]; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) @@ -3708,7 +3708,7 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, * the qform_code will be zero, at which point you can check * analyze75_orient if you care to. */ - unsigned char c = *((char *)(&nhdr.qform_code)); + unsigned char c = *((unsigned char *)(&nhdr.qform_code)); nim->analyze75_orient = (analyze_75_orient_code)c; } if( doswap ) { @@ -3783,7 +3783,7 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, if( nhdr.dim[ii] > 0 && nim->nvox > SIZE_MAX / (size_t)nhdr.dim[ii] ){ free(nim); ERREX("dim[] overflows the voxel count"); } - nim->nvox *= nhdr.dim[ii]; + nim->nvox *= (size_t)nhdr.dim[ii]; } /**- set the type of data in voxels and how many bytes per voxel */ @@ -4621,7 +4621,7 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext, /* if an old list exists, copy the pointers and free the list */ if( tmplist ){ - memcpy(*list, tmplist, (size_t)((new_length-1)*sizeof(nifti1_extension))); + memcpy(*list, tmplist, (size_t)(new_length-1)*sizeof(nifti1_extension)); free(tmplist); } @@ -6663,10 +6663,8 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) /* scan for opening string */ spos = 0 ; - ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; - if( ii != 1 ) return NULL ; /* nothing scanned: lhs and nn are unset */ - spos += nn ; - if( strcmp(lhs,"") == 0 ) break ; /* end of input? */ + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ /* skip whitespace and the '=' marker */ @@ -6715,9 +6711,8 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) memcpy(rhs,str+spos+1, (size_t)nn) ; rhs[nn] = '\0' ; spos = (str[ii] == '\'') ? ii+1 : ii ; } else { - ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; - if( ii != 1 ) break ; /* nothing found: rhs and nn are unset */ - spos += nn ; + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; + if( ii == 0 ) break ; /* nothing found? */ } unescape_string(rhs) ; /* remove any XML escape sequences */ @@ -6937,7 +6932,7 @@ int nifti_nim_has_valid_dims(nifti_image * nim, int complain) prod = 1; for( c = 1; c <= nim->dim[0]; c++ ){ if( nim->dim[c] > 0) - prod *= nim->dim[c]; + prod *= (size_t)nim->dim[c]; else { if( !complain ) return 0; fprintf(stderr,"** NVd: dim[%d] (=%d) <= 0\n",c, nim->dim[c]); @@ -7237,12 +7232,12 @@ int nifti_read_subregion_image( nifti_image * nim, /* get strides*/ compute_strides(strides,image_size,nim->nbyper); - total_alloc_size = nim->nbyper; /* size of pixel */ + total_alloc_size = (size_t)nim->nbyper; /* size of pixel */ /* find alloc size */ for(i = 0; i < nim->ndim; i++) { - total_alloc_size *= region_size[i]; + total_alloc_size *= (size_t)region_size[i]; } /* allocate buffer, if necessary */ if(*data == 0) @@ -7372,11 +7367,11 @@ static int rci_read_data(nifti_image * nim, int * pivots, int * prods, /* not the base case, so do a set of reduced reads */ /* compute size of sub-brick: all dimensions below pivot */ - for( c = 1, sublen = 1; c < *pivots; c++ ) sublen *= nim->dim[c]; + for( c = 1, sublen = 1; c < *pivots; c++ ) sublen *= (size_t)nim->dim[c]; /* compute number of values to read, i.e. remaining prods */ - for( c = 1, read_size = 1; c < nprods; c++ ) read_size *= prods[c]; - read_size *= nim->nbyper; /* and multiply by bytes per voxel */ + for( c = 1, read_size = 1; c < nprods; c++ ) read_size *= (size_t)prods[c]; + read_size *= (size_t)nim->nbyper; /* and multiply by bytes per voxel */ /* now repeatedly compute offsets, and recursively read */ for( c = 0; c < prods[0]; c++ ){ @@ -7385,7 +7380,7 @@ static int rci_read_data(nifti_image * nim, int * pivots, int * prods, /* the unneeded multiplication is to make this more clear */ offset = (size_t)c * sublen * nim->dim[*pivots] + (size_t)sublen * dims[*pivots]; - offset *= nim->nbyper; + offset *= (size_t)nim->nbyper; if( g_opts.debug > 3 ) fprintf(stderr,"-d reading %u bytes, foff %u + %u, doff %u\n", diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index ee3f78c6..6a89899e 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -702,7 +702,7 @@ int fill_cmd_string( nt_opts * opts, int argc, const char * argv[]) return 1; } cp = opts->command + len; - remain -= len; + remain -= (size_t)len; /* get the rest, with special attention to input files */ for( int ac = 1; ac < argc; ac++ ) @@ -727,7 +727,7 @@ int fill_cmd_string( nt_opts * opts, int argc, const char * argv[]) fprintf(stderr,"FCS: error parsing command, continuing...\n"); return 1; } - remain -= len; + remain -= (size_t)len; /* infiles is okay, but after the *next* argument, we may skip files */ /* (danger, will robinson! hack alert!) */ diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index 28ecf791..90a6a7f8 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -148,7 +148,7 @@ size_t znzread(void* buf, size_t size, size_t nmemb, znzFile file) /* 0, not gzread's -1: this returns size_t, where -1 is SIZE_MAX. */ if( nread < 0 ) return 0; - remain -= nread; + remain -= (size_t)nread; cbuf += nread; /* require reading n2read bytes, so we don't get stuck */ @@ -182,7 +182,7 @@ size_t znzwrite(const void* buf, size_t size, size_t nmemb, znzFile file) /* gzwrite returns 0 on error, but in case that ever changes... */ if( nwritten < 0 ) return 0; - remain -= nwritten; + remain -= (size_t)nwritten; cbuf += nwritten; /* require writing n2write bytes, so we don't get stuck */ From 3adf034f929c5d7129e83e84ad9ce3aa222b27e6 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:41:47 -0400 Subject: [PATCH 3/3] ENH: Make the last implicit sign conversions explicit One change is a fix. nifti_read_buffer() passed (int)(ntot / nim->swapsize) to nifti_swap_Nbytes(), which takes a size_t count that was widened in 2010 because it "might not fit as int". The cast reinstated that truncation, leaving most of an image above 2 GiB unswapped. The cast is gone. nifti_image_read() took the file size through (size_t), which turns nifti_get_filesize()'s -1 error return into SIZE_MAX and slips past the guard below it. A signed temporary is used instead, matching what nifti2_io.c already does at the same place. The rest casts an int dimension to size_t at the point of use. --- cifti/afni_xml.c | 4 +-- fsliolib/fslio.c | 13 +++++---- nifti2/nifti2_io.c | 14 +++++----- niftilib/nifti1_io.c | 62 ++++++++++++++++++++++-------------------- niftilib/nifti1_tool.c | 23 +++++++--------- 5 files changed, 60 insertions(+), 56 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 495e092f..35a7f64a 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -211,7 +211,7 @@ afni_xml_list axml_read_file(const char * fname, int read_data) if(xd->verb > 4) fprintf(stderr,"-- XML_Parse # %d\n", pcount); pcount++; - if( XML_Parse(parser, buf, blen, done) == XML_STATUS_ERROR) { + if( XML_Parse(parser, buf, (int)blen, done) == XML_STATUS_ERROR) { fprintf(stderr,"** %s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser)); @@ -299,7 +299,7 @@ afni_xml_list axml_read_buf(const char * buf_in, int64_t bin_len) if(xd->verb > 4) fprintf(stderr,"-- XML_Parse # %d\n", pcount); pcount++; - if( XML_Parse(parser, buf, blen, done) == XML_STATUS_ERROR) { + if( XML_Parse(parser, buf, (int)blen, done) == XML_STATUS_ERROR) { fprintf(stderr,"** %s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser)); diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index 3180450c..44053fcf 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -1225,8 +1225,11 @@ void FslSetDim(FSLIO *fslio, short x, short y, short z, short v) fslio->niftiptr->dim[6] = fslio->niftiptr->nv; fslio->niftiptr->dim[7] = fslio->niftiptr->nw; - fslio->niftiptr->nvox = fslio->niftiptr->nx * fslio->niftiptr->ny * fslio->niftiptr->nz - * fslio->niftiptr->nt * fslio->niftiptr->nu * fslio->niftiptr->nv * fslio->niftiptr->nw ; + fslio->niftiptr->nvox = + (size_t)fslio->niftiptr->nx * (size_t)fslio->niftiptr->ny + * (size_t)fslio->niftiptr->nz * (size_t)fslio->niftiptr->nt + * (size_t)fslio->niftiptr->nu * (size_t)fslio->niftiptr->nv + * (size_t)fslio->niftiptr->nw ; } if (fslio->mincptr!=NULL) { @@ -2419,16 +2422,16 @@ double ****d4matrix(int th, int zh, int yh, int xh) if (!t) FSLIOERR("d4matrix: allocation failure"); /** allocate pointers to slices */ - t[0]=(double ***) malloc((size_t)nvol*(size_t)((nslice)*sizeof(double**))); + t[0]=(double ***) malloc((size_t)nvol*(size_t)nslice*sizeof(double**)); if (!t[0]) FSLIOERR("d4matrix: allocation failure"); /** allocate pointers for ydim */ - t[0][0]=(double **) malloc((size_t)nvol*(size_t)((nslice*nrow)*sizeof(double*))); + t[0][0]=(double **) malloc((size_t)nvol*(size_t)nslice*(size_t)nrow*sizeof(double*)); if (!t[0][0]) FSLIOERR("d4matrix: allocation failure"); /** allocate the data blob */ - t[0][0][0]=(double *) malloc((size_t)nvol*(size_t)((nslice*nrow*ncol)*sizeof(double))); + t[0][0][0]=(double *) malloc((size_t)nvol*(size_t)nslice*(size_t)nrow*(size_t)ncol*sizeof(double)); if (!t[0][0][0]) FSLIOERR("d4matrix: allocation failure"); diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index fc90a639..70c136e8 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -6286,7 +6286,7 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int64_t remain ) return 0; } - count = znzread( extdr.extension, 1, (size_t)4, fp ); /* get extender */ + count = (int64_t)znzread( extdr.extension, 1, (size_t)4, fp ); /* get extender */ if( count < 4 ){ if( g_opts.debug > 1 ) @@ -6905,7 +6905,7 @@ int64_t nifti_read_buffer(znzFile fp, void* dataptr, int64_t ntot, return -1; } - ii = znzread( dataptr , 1 , (size_t)ntot, fp ) ; /* data input */ + ii = (int64_t)znzread( dataptr , 1 , (size_t)ntot, fp ) ; /* data input */ /* if read was short, fail */ if( ii < ntot ){ @@ -7069,7 +7069,7 @@ int64_t nifti_write_buffer(znzFile fp, const void *buffer, int64_t numbytes) fprintf(stderr,"** ERROR: nifti_write_buffer: null file pointer\n"); return 0; } - ss = znzwrite( buffer , 1 , (size_t)numbytes, fp ) ; + ss = (int64_t)znzwrite( buffer , 1 , (size_t)numbytes, fp ) ; return ss; } @@ -7746,7 +7746,7 @@ int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) if( nim_src->num_ext <= 0 ) return 0; - bytes = (size_t)nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ + bytes = nim_src->num_ext * (int64_t)sizeof(nifti1_extension); /* I'm lazy */ nim_dest->ext_list = (nifti1_extension *)malloc((size_t)bytes); if( !nim_dest->ext_list ){ fprintf(stderr,"** failed to allocate %d nifti1_extension structs\n", @@ -9704,7 +9704,7 @@ int64_t * nifti_get_int64list( int64_t nvals , const char * str ) if( str[ipos] == ',' || ISEND(str[ipos]) ){ nout++ ; - subv_realloc = (int64_t *)realloc( (char *)subv , (size_t)(sizeof(int64_t)*(nout+1))) ; + subv_realloc = (int64_t *)realloc( (char *)subv , sizeof(int64_t)*(size_t)(nout+1)) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %" PRId64 @@ -9786,7 +9786,7 @@ int64_t * nifti_get_int64list( int64_t nvals , const char * str ) for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ nout++ ; - subv_realloc = (int64_t *)realloc( (char *)subv , (size_t)(sizeof(int64_t)*(nout+1))) ; + subv_realloc = (int64_t *)realloc( (char *)subv , sizeof(int64_t)*(size_t)(nout+1)) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %" PRId64 @@ -9837,7 +9837,7 @@ int * nifti_get_intlist( int nvals , const char * str ) } /* have a valid result, copy as ints */ - ilist = (int *)malloc((size_t)((nints+1) * sizeof(int))); + ilist = (int *)malloc((size_t)(nints+1) * sizeof(int)); if( !ilist ) { fprintf(stderr,"** nifti_get_intlist: failed to alloc %" PRId64 " ints\n", nints); diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 3a327692..c9142554 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -834,7 +834,7 @@ static int nifti_load_NBL_bricks( nifti_image * nim , const int * slist, const i fprintf(stderr,"** load bricks: ztell failed??\n"); return -1; } - fposn = oposn = test; + fposn = oposn = (size_t)test; /* first, handle the default case, no passed blist */ if( !slist ){ @@ -867,8 +867,8 @@ static int nifti_load_NBL_bricks( nifti_image * nim , const int * slist, const i if( isrc != prev ){ /* if we are not looking at the correct sub-brick, scan forward */ - if( fposn != (oposn + isrc*NBL->bsize) ){ - fposn = oposn + isrc*NBL->bsize; + if( fposn != (oposn + (size_t)isrc*NBL->bsize) ){ + fposn = oposn + (size_t)isrc*NBL->bsize; if( znzseek(fp, (long)fposn, SEEK_SET) < 0 ){ fprintf(stderr,"** failed to locate brick %d in file '%s'\n", isrc, nim->iname ? nim->iname : nim->fname); @@ -918,7 +918,8 @@ static int nifti_alloc_NBL_mem(const nifti_image * nim, int nbricks, nbl->nbricks *= nim->dim[c]; } - nbl->bsize = (size_t)nim->nx * nim->ny * nim->nz * nim->nbyper;/* bytes */ + nbl->bsize = (size_t)nim->nx * (size_t)nim->ny + * (size_t)nim->nz * (size_t)nim->nbyper; /* bytes */ nbl->bricks = (void **)malloc((size_t)nbl->nbricks * sizeof(void *)); if( ! nbl->bricks ){ @@ -2448,7 +2449,7 @@ int nifti_get_filesize( const char *pathname ) if( pathname == NULL || *pathname == '\0' ) return -1 ; ii = stat( pathname , &buf ); if( ii != 0 ) return -1 ; - return (unsigned int)buf.st_size ; + return (int)buf.st_size ; } #else /*---------- non-Unix version of the above, less efficient -----------*/ @@ -4336,8 +4337,8 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) } /**- check for extensions (any errors here means no extensions) */ - if( NIFTI_ONEFILE(nhdr) ) remaining = nim->iname_offset - sizeof(nhdr); - else remaining = filesize - sizeof(nhdr); + if( NIFTI_ONEFILE(nhdr) ) remaining = nim->iname_offset - (int)sizeof(nhdr); + else remaining = filesize - (int)sizeof(nhdr); (void)nifti_read_extensions(nim, fp, remaining); @@ -4932,15 +4933,17 @@ static znzFile nifti_image_load_prep( nifti_image *nim ) znzclose(fp); return NULL; } - ii = nifti_get_filesize( nim->iname ) ; - if( ii == 0 ){ - if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); + const int64_t fsize = nifti_get_filesize( nim->iname ) ; + if( fsize <= 0 ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"empty or unreadable data file",nim->iname); znzclose(fp); return NULL; } + ii = (size_t)fsize ; ioff = (ii > ntot) ? ii-ntot : 0 ; } else { /* non-negative offset */ - ioff = nim->iname_offset ; /* means use it directly */ + ioff = (size_t)nim->iname_offset ; /* means use it directly */ } /**- seek to the appropriate read position */ @@ -5071,7 +5074,7 @@ size_t nifti_read_buffer(znzFile fp, void* dataptr, size_t ntot, if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { if( g_opts.debug > 1 ) fprintf(stderr,"+d nifti_read_buffer: swapping data bytes...\n"); - nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; + nifti_swap_Nbytes( ntot / (size_t)nim->swapsize, nim->swapsize , dataptr ) ; } #ifdef isfinite @@ -5247,11 +5250,11 @@ int nifti_write_all_data(znzFile fp, nifti_image * nim, return -1; } - ss = nifti_write_buffer(fp,nim->data,nim->nbyper * nim->nvox); - if (ss < nim->nbyper * nim->nvox){ + ss = nifti_write_buffer(fp,nim->data, (size_t)nim->nbyper * nim->nvox); + if (ss < (size_t)nim->nbyper * nim->nvox){ fprintf(stderr, "** ERROR: NWAD: wrote only %u of %u bytes to file\n", - (unsigned)ss, (unsigned)(nim->nbyper * nim->nvox)); + (unsigned)ss, (unsigned)((size_t)nim->nbyper * nim->nvox)); return -1; } @@ -5324,7 +5327,7 @@ static int nifti_write_extensions(znzFile fp, nifti_image *nim) ok = (size == (int)sizeof(int)); } if( ok ){ - size = (int)nifti_write_buffer(fp, list->edata, list->esize - 8); + size = (int)nifti_write_buffer(fp, list->edata, (size_t)(list->esize - 8)); ok = (size == list->esize - 8); } @@ -5493,7 +5496,7 @@ nifti_image * nifti_make_new_nim(const int dims[8], int datatype, int data_fill) /* if we cannot allocate data, take ball and go home */ if( !nim->data ) { fprintf(stderr,"** NMNN: failed to alloc %u bytes for data\n", - (unsigned)(nim->nvox*nim->nbyper)); + (unsigned)(nim->nvox*(size_t)nim->nbyper)); nifti_image_free(nim); nim = NULL; } @@ -5736,7 +5739,7 @@ void nifti_set_iname_offset(nifti_image *nim) /* NIFTI-1 single binary file - always update */ case NIFTI_FTYPE_NIFTI1_1: - offset = nifti_extension_size(nim)+sizeof(struct nifti_1_header)+4; + offset = nifti_extension_size(nim)+(int)sizeof(struct nifti_1_header)+4; /* be sure offset is aligned to a 16 byte boundary */ if ( ( offset % 16 ) != 0 ) offset = ((offset + 0xf) & ~0xf); if( nim->iname_offset != offset ){ @@ -6821,8 +6824,9 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) nim->dim[6] = nim->nv ; nim->pixdim[6] = nim->dv ; nim->dim[7] = nim->nw ; nim->pixdim[7] = nim->dw ; - nim->nvox = (size_t)nim->nx * nim->ny * nim->nz - * nim->nt * nim->nu * nim->nv * nim->nw ; + nim->nvox = (size_t)nim->nx * (size_t)nim->ny * (size_t)nim->nz + * (size_t)nim->nt * (size_t)nim->nu * (size_t)nim->nv + * (size_t)nim->nw ; if( nim->qform_code > 0 ) nim->qto_xyz = nifti_quatern_to_mat44( @@ -7084,7 +7088,7 @@ int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], /** - call the recursive reading function, passing nim, the pivot info, location to store memory, and file pointer and position */ c = rci_read_data(nim, pivots,prods,nprods,dims, - (char *)*data, fp, znztell(fp)); + (char *)*data, fp, (size_t)znztell(fp)); znzclose(fp); /* in any case, close the file */ if( c < 0 ){ free(*data); *data = NULL; return -1; } /* failure */ @@ -7296,7 +7300,7 @@ int nifti_read_subregion_image( nifti_image * nim, (si[0] * strides[0]); znzseek(fp, offset, SEEK_SET); /* seek to current row */ read_amount = rs[0] * nim->nbyper; /* read a row of the subregion*/ - nread = (int)nifti_read_buffer(fp, readptr, read_amount, nim); + nread = (int)nifti_read_buffer(fp, readptr, (size_t)read_amount, nim); if(nread != read_amount) { if(g_opts.debug > 0) @@ -7351,7 +7355,7 @@ static int rci_read_data(nifti_image * nim, int * pivots, int * prods, /* so just seek and read (prods[0] * nbyper) bytes from the file */ znzseek(fp, (long)base_offset, SEEK_SET); - bytes = (size_t)prods[0] * nim->nbyper; + bytes = (size_t)prods[0] * (size_t)nim->nbyper; nread = nifti_read_buffer(fp, data, bytes, nim); if( nread != bytes ){ fprintf(stderr,"** rciRD: read only %u of %u bytes from '%s'\n", @@ -7378,18 +7382,18 @@ static int rci_read_data(nifti_image * nim, int * pivots, int * prods, /* offset is (c * sub-block size (including pivot dim)) */ /* + (dims[] index into pivot sub-block) */ /* the unneeded multiplication is to make this more clear */ - offset = (size_t)c * sublen * nim->dim[*pivots] + - (size_t)sublen * dims[*pivots]; + offset = (size_t)c * sublen * (size_t)nim->dim[*pivots] + + sublen * (size_t)dims[*pivots]; offset *= (size_t)nim->nbyper; if( g_opts.debug > 3 ) fprintf(stderr,"-d reading %u bytes, foff %u + %u, doff %u\n", (unsigned)read_size, (unsigned)base_offset, (unsigned)offset, - (unsigned)(c*read_size)); + (unsigned)((size_t)c*read_size)); /* now read the next level down, adding this offset */ if( rci_read_data(nim, pivots+1, prods+1, nprods-1, dims, - data + c * read_size, fp, base_offset + offset) < 0 ) + data + (size_t)c * read_size, fp, base_offset + offset) < 0 ) return -1; } @@ -7586,7 +7590,7 @@ int * nifti_get_intlist( int nvals , const char * str ) if( str[ipos] == ',' || ISEND(str[ipos]) ){ nout++ ; - subv_realloc = (int *)realloc( (char *)subv , (size_t)(sizeof(int) * (nout+1))) ; + subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (size_t)(nout+1)) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", @@ -7677,7 +7681,7 @@ int * nifti_get_intlist( int nvals , const char * str ) for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ nout++ ; - subv_realloc = (int *)realloc( (char *)subv , (size_t)(sizeof(int) * (nout+1))) ; + subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (size_t)(nout+1)) ; if( !subv_realloc ) { free(subv); fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index 6a89899e..5d9f89a6 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -2980,8 +2980,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } /* otherwise, we're good */ - { const int16_t sval = (int16_t)val; - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(sval), &sval,sizeof(sval)); } + ((short *)((char *)basep + field->offset))[fc] = (short)val; if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -3000,8 +2999,7 @@ int modify_field(void * basep, field_s * field, const char * data) fc,field->len); return 1; } - { const int32_t ival = (int32_t)val; - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(ival), &ival,sizeof(ival)); } + ((int *)((char *)basep + field->offset))[fc] = val; if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -3021,7 +3019,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } /* otherwise, we're good */ - memcpy((char *)basep + field->offset + (size_t)fc * sizeof(fval), &fval,sizeof(fval)); + ((float *)((char *)basep + field->offset))[fc] = fval; if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %f\n", fc, field->name, fval); @@ -3480,7 +3478,7 @@ int disp_field( const char *mesg, field_s *fieldp, void * str, int nfields, int int len; /* start by sucking the pointer stored here */ - memcpy(&sp, (const char *)str + fp->offset, sizeof(sp)); + sp = *(char **)((char *)str + fp->offset); if( ! sp ){ fprintf(stdout,"(NULL)\n"); break; } /* anything? */ @@ -3494,9 +3492,7 @@ int disp_field( const char *mesg, field_s *fieldp, void * str, int nfields, int else if( *sp && !isprint(*sp) ) /* if no termination, it's bad */ fprintf(stdout,"(non-printable string)\n"); else /* woohoo! a good string */ - { char * cp; - memcpy(&cp, (const char *)str + fp->offset, sizeof(cp)); - fprintf(stdout,"'%.40s'\n", cp); } + fprintf(stdout,"'%.40s'\n",*(char **)((char *)str + fp->offset)); break; } @@ -3505,7 +3501,7 @@ int disp_field( const char *mesg, field_s *fieldp, void * str, int nfields, int nifti1_extension * extp; /* yank the address sitting there into extp */ - memcpy(&extp, (const char *)str + fp->offset, sizeof(extp)); + extp = *(nifti1_extension **)((char *)str + fp->offset); /* the user may use -disp_exts to display all of them */ if( extp ) disp_nifti1_extension(NULL, extp, 6); @@ -3564,8 +3560,8 @@ int diff_field(field_s *fieldp, void * str0, void * str1, int nfields) { nifti1_extension * ext0, * ext1; - memcpy(&ext0, (const char *)str0 + fp->offset, sizeof(ext0)); - memcpy(&ext1, (const char *)str1 + fp->offset, sizeof(ext1)); + ext0 = *(nifti1_extension **)((char *)str0 + fp->offset); + ext1 = *(nifti1_extension **)((char *)str1 + fp->offset); if( ! ext0 && ! ext1 ) break; /* continue on */ @@ -4274,7 +4270,8 @@ nifti_image * nt_read_bricks(nt_opts * opts, const char * fname, int len, int * /* now populate NBL (can be based only on len and nim) */ NBL->nbricks = len; - NBL->bsize = (size_t)nim->nbyper * nim->nx * nim->ny * nim->nz; + NBL->bsize = (size_t)nim->nbyper * (size_t)nim->nx + * (size_t)nim->ny * (size_t)nim->nz; NBL->bricks = (void **)calloc((size_t)(NBL->nbricks), (size_t)(sizeof(void *))); if( !NBL->bricks ){ fprintf(stderr,"** NRB: failed to alloc %d pointers\n",NBL->nbricks);