Skip to content

Commit

Permalink
nifti 2023-03-29 (2acc4449)
Browse files Browse the repository at this point in the history
Code extracted from:

    https://github.com/NIFTI-Imaging/nifti_clib.git

at commit 2acc4449985942b66dbc391c93392e12f8b95b75 (master).
  • Loading branch information
NIFTI Upstream authored and seanm committed Mar 29, 2023
1 parent 08343ec commit 5e40191
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 61 deletions.
54 changes: 37 additions & 17 deletions niftilib/nifti1_io.c
Expand Up @@ -4,6 +4,9 @@
#include "nifti1_io.h" /* typedefs, prototypes, macros, etc. */
#include "nifti1_io_version.h"

#include <errno.h>
#include <limits.h>

/*****===================================================================*****/
/***** Sample functions to deal with NIFTI-1 and ANALYZE files *****/
/*****...................................................................*****/
Expand Down Expand Up @@ -2661,8 +2664,7 @@ int nifti_is_gzfile(const char* fname)
if (fname == NULL) { return 0; }
#ifdef HAVE_ZLIB
{ /* just so len doesn't generate compile warning */
int len;
len = (int)strlen(fname);
size_t len = strlen(fname);
if (len < 3) return 0; /* so we don't search before the name */
if (fileext_compare(fname + strlen(fname) - 3,".gz")==0) { return 1; }
}
Expand Down Expand Up @@ -4394,7 +4396,7 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain )
nifti1_extender extdr; /* defines extension existence */
nifti1_extension extn; /* single extension to process */
nifti1_extension * Elist; /* list of processed extensions */
int posn, count;
int count;

if( !nim || znz_isnull(fp) ) {
if( g_opts.debug > 0 )
Expand All @@ -4403,15 +4405,15 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain )
return -1;
}

posn = znztell(fp);
znz_off_t posn = znztell(fp);

if( (posn != sizeof(nifti_1_header)) &&
(nim->nifti_type != NIFTI_FTYPE_ASCII) )
fprintf(stderr,"** WARNING: posn not header size (%d, %d)\n",
posn, (int)sizeof(nifti_1_header));
fprintf(stderr,"** WARNING: posn not header size (%lld, %lu)\n",
posn, sizeof(nifti_1_header));

if( g_opts.debug > 2 )
fprintf(stderr,"-d nre: posn = %d, offset = %d, type = %d, remain = %d\n",
fprintf(stderr,"-d nre: posn = %lld, offset = %d, type = %d, remain = %d\n",
posn, nim->iname_offset, nim->nifti_type, remain);

if( remain < 16 ){
Expand Down Expand Up @@ -7075,7 +7077,7 @@ int nifti_read_subregion_image( nifti_image * nim,
{
if(g_opts.debug > 1)
{
fprintf(stderr,"allocation of %d bytes failed\n",total_alloc_size);
fprintf(stderr,"allocation of %zu bytes failed\n",total_alloc_size);
}
znzclose(fp);
return -1;
Expand Down Expand Up @@ -7344,7 +7346,7 @@ int * nifti_get_intlist( int nvals , const char * str )
int *subv = NULL ;
int *subv_realloc = NULL;
int ii , ipos , nout , slen ;
int ibot,itop,istep , nused ;
int ibot,itop,istep ;
char *cpt ;

/* Meaningless input? */
Expand Down Expand Up @@ -7380,18 +7382,24 @@ int * nifti_get_intlist( int nvals , const char * str )
if( str[ipos] == '$' ){ /* special case */
ibot = nvals-1 ; ipos++ ;
} else { /* decode an integer */
ibot = strtol( str+ipos , &cpt , 10 ) ;
errno = 0;
long temp = strtol( str+ipos , &cpt , 10 ) ;
if( (temp == 0 && errno != 0) || temp <= INT_MIN || temp >= INT_MAX){
fprintf(stderr,"** ERROR: list index does not fit in int\n") ;
free(subv) ; return NULL ;
}
ibot = (int)temp;
if( ibot < 0 ){
fprintf(stderr,"** ERROR: list index %d is out of range 0..%d\n",
fprintf(stderr,"** ERROR: list index %d is out of range 0..%d\n",
ibot,nvals-1) ;
free(subv) ; return NULL ;
}
if( ibot >= nvals ){
fprintf(stderr,"** ERROR: list index %d is out of range 0..%d\n",
fprintf(stderr,"** ERROR: list index %d is out of range 0..%d\n",
ibot,nvals-1) ;
free(subv) ; return NULL ;
}
nused = (cpt-(str+ipos)) ;
long nused = (cpt-(str+ipos)) ;
if( ibot == 0 && nused == 0 ){
fprintf(stderr,"** ERROR: list syntax error '%s'\n",str+ipos) ;
free(subv) ; return NULL ;
Expand Down Expand Up @@ -7437,7 +7445,13 @@ int * nifti_get_intlist( int nvals , const char * str )
if( str[ipos] == '$' ){ /* special case */
itop = nvals-1 ; ipos++ ;
} else { /* decode an integer */
itop = strtol( str+ipos , &cpt , 10 ) ;
errno = 0;
long temp = strtol( str+ipos , &cpt , 10 ) ;
if( (temp == 0 && errno != 0) || temp <= INT_MIN || temp >= INT_MAX){
fprintf(stderr,"** ERROR: list index does not fit in int\n") ;
free(subv) ; return NULL ;
}
itop = (int)temp;
if( itop < 0 ){
fprintf(stderr,"** ERROR: index %d is out of range 0..%d\n",
itop,nvals-1) ;
Expand All @@ -7448,7 +7462,7 @@ int * nifti_get_intlist( int nvals , const char * str )
itop,nvals-1) ;
free(subv) ; return NULL ;
}
nused = (cpt-(str+ipos)) ;
long nused = (cpt-(str+ipos)) ;
if( itop == 0 && nused == 0 ){
fprintf(stderr,"** ERROR: index list syntax error '%s'\n",str+ipos) ;
free(subv) ; return NULL ;
Expand All @@ -7466,12 +7480,18 @@ int * nifti_get_intlist( int nvals , const char * str )

if( str[ipos] == '(' ){ /* decode an integer */
ipos++ ;
istep = strtol( str+ipos , &cpt , 10 ) ;
errno = 0;
long temp = strtol( str+ipos , &cpt , 10 ) ;
if( (temp == 0 && errno != 0) || temp <= INT_MIN || temp >= INT_MAX){
fprintf(stderr,"** ERROR: list index does not fit in int\n") ;
free(subv) ; return NULL ;
}
istep = (int)temp;
if( istep == 0 ){
fprintf(stderr,"** ERROR: index loop step is 0!\n") ;
free(subv) ; return NULL ;
}
nused = (cpt-(str+ipos)) ;
long nused = (cpt-(str+ipos)) ;
ipos += nused ;
if( str[ipos] == ')' ) ipos++ ;
if( (ibot-itop)*istep > 0 ){
Expand Down
4 changes: 2 additions & 2 deletions niftilib/nifti1_test.c
Expand Up @@ -17,7 +17,7 @@ static int local_fileexists(const char* fname)
int main( int argc , char *argv[] )
{
nifti_image *nim ;
int iarg=1 , outmode=1 , ll , argn=1, usegzip=0;
int iarg=1 , outmode=1 , argn=1, usegzip=0;
char *tmpstr;

if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
Expand Down Expand Up @@ -78,7 +78,7 @@ int main( int argc , char *argv[] )
if( nim->fname != NULL ) free(nim->fname) ;
if( nim->iname != NULL ) free(nim->iname) ;

ll = strlen(argv[iarg]) ;
size_t ll = strlen(argv[iarg]) ;
tmpstr = nifti_makebasename(argv[iarg]);
nim->fname = (char *)calloc(1,ll+8) ; strcpy(nim->fname,tmpstr) ;
nim->iname = (char *)calloc(1,ll+8) ; strcpy(nim->iname,tmpstr) ;
Expand Down

0 comments on commit 5e40191

Please sign in to comment.