diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e2bd685b..3bb63fbe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -98,12 +98,12 @@ jobs: if: runner.os == 'Linux' run: | sudo apt-get update - sudo apt-get install -y cmake valgrind help2man + sudo apt-get install -y cmake valgrind help2man clang-tools - name: Install Dependencies (macOS) if: runner.os == 'macOS' run: | - brew install cmake sed help2man + brew install cmake gnu-sed help2man - name: Set Compiler and Flags run: | diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index a9910a1b..d7cc9a4f 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -1021,7 +1021,8 @@ static int64_t loc_strnlen(const char * str, int64_t maxlen) const char * sptr; int64_t len; - for( sptr=str, len=0; *sptr && len 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , ni_ver ) ; + /* nhdr is a nifti_1_header, so swap it as one. ni_ver comes from the + magic string, and a magic of "n+2" would otherwise have + swap_nifti_header() treat these 348 bytes as a 540 byte header. */ + swap_nifti_header( &nhdr , ni_ver ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr); @@ -4807,8 +4810,14 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -4817,6 +4826,11 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > INT64_MAX / nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; @@ -5036,6 +5050,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) ERREX("bad datatype") ; } + /* dim[0] is the number of dimensions and the loops below index dim[] + with it; the NIFTI-1 path gets this check from need_nhdr_swap() */ + if( nhdr.dim[0] < 0 || nhdr.dim[0] > 7 ) + { + free(nim); + ERREX("bad dim[0]") ; + } + if( nhdr.dim[1] <= 0 ) { free(nim); @@ -5077,8 +5099,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -5087,6 +5115,11 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > INT64_MAX / nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; @@ -5367,7 +5400,9 @@ nifti_1_header * nifti_read_n1_hdr(const char * hname, int *swapped, int check) if( lswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ; + /* only sizeof(nifti_1_header) bytes were read, so swap as that; see + the same guard in nifti_convert_n1hdr2nim() */ + swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr); @@ -5985,8 +6020,8 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) #ifdef REJECT_COMPLEX if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { - fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); - exit(13); + fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname); + return NULL; } #endif @@ -7859,68 +7894,68 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB -int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; -} - -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; +static int doPigz2(nifti_image * nim, const nifti_2_header * nhdr, const nifti_brick_list * NBL) { + FILE *pigzPipe; + char command[4096]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); +#ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; +#else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; +#endif + znzFile fp; + fp = (znzFile) calloc(1, sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe); + if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp, nim); + nifti_write_all_data(fp, nim, NBL); +#ifdef _MSC_VER + _pclose(pigzPipe); +#else + pclose(pigzPipe); +#endif + free(fp); + return 0; +} + +static int doPigz(nifti_image * nim, const nifti_1_header * nhdr, const nifti_brick_list * NBL) { + FILE *pigzPipe; + char command[4096]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); +#ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; +#else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; +#endif + znzFile fp; + fp = (znzFile) calloc(1, sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe); + if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp, nim); + nifti_write_all_data(fp, nim, NBL); +#ifdef _MSC_VER + _pclose(pigzPipe); +#else + pclose(pigzPipe); +#endif + free(fp); + return 0; } #endif //HAVE_ZLIB #endif //PIGZ @@ -8043,10 +8078,10 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, char pigzKey[5] = "PIGZ"; if ((value != NULL) && (strstr(value,pigzKey))) { if( nver == 2 ) { - if (doPigz2(nim, n2hdr, NBL) == 0) + if (doPigz2(nim, &n2hdr, NBL) == 0) return 0; } else { - if (doPigz(nim, n1hdr, NBL) == 0) //success writing with pigz + if (doPigz(nim, &n1hdr, NBL) == 0) //success writing with pigz return 0; } } diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index 7dfa3272..85bc3f54 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -3391,7 +3391,11 @@ int act_mod_hdrs( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_1_header, so swap it as one. NIFTI_VERSION() + reads the magic string, and a magic of "n+2" would otherwise + have swap_nifti_header() treat these 348 bytes as a 540 byte + header. ni_ver 0 and 1 are both 348 byte layouts. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); /* if all is well, overwrite header in fname dataset */ (void)write_hdr_to_file(nhdr, fname); /* errors printed in function */ @@ -3512,7 +3516,9 @@ int act_mod_hdr2s( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_2_header; use the explicit version rather than + the magic, which could claim "n+1" and swap 540 bytes as 348 */ + swap_nifti_header(nhdr, 2); /* if all is well, overwrite header in fname dataset */ (void)write_hdr2_to_file(nhdr, fname); /* errors printed in function */ @@ -3613,8 +3619,12 @@ int act_swap_hdrs( nt_opts * opts ) swap_nifti_header(nhdr, 0); /* undo ANALYZE */ swap_nifti_header(nhdr, 1); /* swap NIFTI */ } else if ( opts->swap_old ) { - /* undo whichever was done and apply the old way */ - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* undo whichever was done and apply the old way. As above, + nhdr is a nifti_1_header, so it must not be swapped as a + 540 byte NIFTI-2 header just because its magic says "n+2". + old_swap_nifti_header() takes a nifti_1_header and a + boolean, so it needs no such guard. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); old_swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); } diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 0580c629..ed43a99f 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -2829,7 +2829,7 @@ char * nifti_findhdrname(const char* fname) if (nifti_fileexists(gzname)) { fprintf(stderr,"Image Exception : Multiple possible filenames detected for basename (*.nii, *.nii.gz): %s\n", basename); free(gzname); - exit(134); + return NULL; } free(gzname); return hdrname; @@ -3766,8 +3766,15 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap. nvox is a size_t here, where the + NIFTI-2 library uses int64_t, so the bound is SIZE_MAX. */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + 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]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -3776,6 +3783,11 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > SIZE_MAX / (size_t)nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; @@ -4295,7 +4307,7 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) #ifdef REJECT_COMPLEX if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { - fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); + fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname); exit(13); } #endif @@ -5776,68 +5788,36 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB -int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; -} - -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; +static int doPigz(nifti_image * nim, const nifti_1_header * nhdr, const nifti_brick_list * NBL) { + FILE *pigzPipe; + char command[4096]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); +#ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; +#else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; +#endif + znzFile fp; + fp = (znzFile) calloc(1, sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe); + if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp,nim); + nifti_write_all_data(fp,nim,NBL); +#ifdef _MSC_VER + _pclose(pigzPipe); +#else + pclose(pigzPipe); +#endif + free(fp); + return 0; } #endif //HAVE_ZLIB #endif //PIGZ @@ -5939,7 +5919,7 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, //export AFNI_COMPRESSOR=PIGZ char pigzKey[5] = "PIGZ"; if ((value != NULL) && (strstr(value,pigzKey))) { - if (doPigz(nim, nhdr, NBL) == 0) //success writing with pigz + if (doPigz(nim, &nhdr, NBL) == 0) //success writing with pigz return 0; } } diff --git a/niftilib/nifti1_tool.h b/niftilib/nifti1_tool.h index a8ca5ddd..14912ed9 100644 --- a/niftilib/nifti1_tool.h +++ b/niftilib/nifti1_tool.h @@ -157,4 +157,4 @@ nifti_1_header * nt_read_header(nt_opts * opts, const char * fname, int * swappe int check); -#endif /* NIFTI1_TOOL_H */ \ No newline at end of file +#endif /* NIFTI1_TOOL_H */