diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 21a4e4cf..7f8ed9df 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -1,9 +1,14 @@ /* popen()/pclose() in the PIGZ write path are POSIX, not ISO C11. */ +#ifdef PIGZ +#ifdef HAVE_ZLIB #define _POSIX_C_SOURCE 200809L +#endif +#endif #define NIFTI2_IO_C #include +#include #include "nifti2_io.h" /* typedefs, prototypes, macros, etc. */ #include "nifti2_io_version.h" @@ -1285,13 +1290,13 @@ char *nifti_strdup(const char *str) { if( !str ) return NULL; /* allow calls passing NULL */ - size_t length = strlen(str); - char *dup = (char *)malloc(length + 1); + size_t length = strlen(str) + 1; + char *dup = (char *)malloc(length); /* check for failure */ - if( dup ) strcpy(dup, str); + if( dup ) strlcpy(dup, str, length); else fprintf(stderr,"** nifti_strdup: failed to alloc %zu bytes\n", - length+1); + length); return dup; } @@ -3529,7 +3534,7 @@ const char * nifti_find_file_extension( const char * name ) ext = name + len - 4; /* make manipulation copy, and possibly convert to lowercase */ - strcpy(extcopy, ext); + strlcpy(extcopy, ext, sizeof(extcopy)); if( g_opts.allow_upper_fext ) make_lowercase(extcopy); /* if it look like a basic extension, fail or return it */ @@ -3548,11 +3553,13 @@ const char * nifti_find_file_extension( const char * name ) ext = name + len - 7; /* make manipulation copy, and possibly convert to lowercase */ - strcpy(extcopy, ext); + strlcpy(extcopy, ext, sizeof(extcopy)); if( g_opts.allow_upper_fext ) make_lowercase(extcopy); /* go after .gz extensions using the modifiable strings */ - strcat(elist[0], extgz); strcat(elist[1], extgz); strcat(elist[2], extgz); + strlcat(elist[0], extgz, sizeof(extnii)); + strlcat(elist[1], extgz, sizeof(exthdr)); + strlcat(elist[2], extgz, sizeof(extimg)); if( compare_strlist(extcopy, elist, 3) >= 0 ) { if( is_mixedcase(ext) ) { @@ -3752,21 +3759,23 @@ char * nifti_findhdrname(const char* fname) make_uppercase(extzip); } - hdrname = (char *)calloc(sizeof(char),strlen(basename)+8); + size_t hdrnamelength = strlen(basename)+8; + hdrname = (char *)calloc(sizeof(char),hdrnamelength); if( !hdrname ){ fprintf(stderr,"** nifti_findhdrname: failed to alloc hdrname\n"); free(basename); return NULL; } - strcpy(hdrname,basename); - strcat(hdrname,elist[efirst]); + strlcpy(hdrname, basename, hdrnamelength); + strlcat(hdrname, elist[efirst], hdrnamelength); #ifdef FSLSTYLE if (nifti_fileexists(hdrname)) { free(basename); - char *gzname = (char *)calloc(sizeof(char),strlen(hdrname)+8); - strcpy(gzname, hdrname); - strcat(gzname,extzip); + size_t gznamelength = strlen(hdrname)+8; + char *gzname = (char *)calloc(sizeof(char), gznamelength); + strlcpy(gzname, hdrname, gznamelength); + strlcat(gzname, extzip, gznamelength); if (nifti_fileexists(gzname)) { fprintf(stderr,"Image Exception : Multiple possible filenames detected for basename (*.nii, *.nii.gz): %s\n", basename); free(gzname); @@ -3779,7 +3788,7 @@ char * nifti_findhdrname(const char* fname) if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #endif #ifdef HAVE_ZLIB - strcat(hdrname,extzip); + strlcat(hdrname, extzip, hdrnamelength); if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #endif @@ -3787,11 +3796,11 @@ char * nifti_findhdrname(const char* fname) efirst = 1 - efirst; - strcpy(hdrname,basename); - strcat(hdrname,elist[efirst]); + strlcpy(hdrname, basename, hdrnamelength); + strlcat(hdrname, elist[efirst], hdrnamelength); if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #ifdef HAVE_ZLIB - strcat(hdrname,extzip); + strlcat(hdrname, extzip, hdrnamelength); if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #endif @@ -3831,8 +3840,9 @@ char * nifti_findimgname(const char* fname , int nifti_type) /* check input file(s) for sanity */ if( !nifti_validfilename(fname) ) return NULL; - basename = nifti_makebasename(fname); - imgname = (char *)calloc(sizeof(char),strlen(basename)+8); + basename = nifti_makebasename(fname); + size_t imgnamelength = strlen(basename)+8; + imgname = (char *)calloc(sizeof(char),imgnamelength); if( !imgname ){ fprintf(stderr,"** nifti_findimgname: failed to alloc imgname\n"); free(basename); @@ -3850,8 +3860,8 @@ char * nifti_findimgname(const char* fname , int nifti_type) /* only valid extension for ASCII type is .nia, handle first */ if( nifti_type == NIFTI_FTYPE_ASCII ){ - strcpy(imgname,basename); - strcat(imgname,extnia); + strlcpy(imgname, basename, imgnamelength); + strlcat(imgname, extnia, imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } } else { @@ -3865,21 +3875,21 @@ char * nifti_findimgname(const char* fname , int nifti_type) else if (nifti_type == NIFTI_FTYPE_NIFTI2_1) first = 0; else first = 1; /* should match .img */ - strcpy(imgname,basename); - strcat(imgname,elist[first]); + strlcpy(imgname, basename, imgnamelength); + strlcat(imgname, elist[first], imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } #ifdef HAVE_ZLIB /* then also check for .gz */ - strcat(imgname,extzip); + strlcat(imgname, extzip, imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } #endif /* failed to find image file with expected extension, try the other */ - strcpy(imgname,basename); - strcat(imgname,elist[1-first]); /* can do this with only 2 choices */ + strlcpy(imgname, basename, imgnamelength); + strlcat(imgname, elist[1-first], imgnamelength); /* can do this with only 2 choices */ if (nifti_fileexists(imgname)) { free(basename); return imgname; } #ifdef HAVE_ZLIB /* then also check for .gz */ - strcat(imgname,extzip); + strlcat(imgname, extzip, imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } #endif } @@ -3920,12 +3930,13 @@ char * nifti_makehdrname(const char * prefix, int nifti_type, int check, if( !nifti_validfilename(prefix) ) return NULL; /* add space for extension, optional ".gz", and null char */ - iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + size_t inamelength = strlen(prefix)+8; + iname = (char *)calloc(sizeof(char),inamelength); if( !iname ){ fprintf(stderr,"** NIFTI small malloc failure!\n"); return NULL; } - strcpy(iname, prefix); + strlcpy(iname, prefix, inamelength); /* use any valid extension */ if( (ext = nifti_find_file_extension(iname)) != NULL ){ @@ -3944,13 +3955,13 @@ char * nifti_makehdrname(const char * prefix, int nifti_type, int check, } } /* otherwise, make one up */ - else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); - else if( nifti_type == NIFTI_FTYPE_NIFTI2_1 ) strcat(iname, extnii); - else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); - else strcat(iname, exthdr); + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strlcat(iname, extnii, inamelength); + else if( nifti_type == NIFTI_FTYPE_NIFTI2_1 ) strlcat(iname, extnii, inamelength); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strlcat(iname, extnia, inamelength); + else strlcat(iname, exthdr, inamelength); #ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ - if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); + if( comp && (!ext || !strstr(iname,extgz)) ) strlcat(iname, extgz, inamelength); #endif /* check for existence failure */ @@ -3996,12 +4007,13 @@ char * nifti_makeimgname(const char * prefix, int nifti_type, int check, if( !nifti_validfilename(prefix) ) return NULL; /* add space for extension, optional ".gz", and null char */ - iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + size_t inamelength = strlen(prefix)+8; + iname = (char *)calloc(sizeof(char),inamelength); if( !iname ){ fprintf(stderr,"** NIFTI: small malloc failure!\n"); return NULL; } - strcpy(iname, prefix); + strlcpy(iname, prefix, inamelength); /* use any valid extension */ if( (ext = nifti_find_file_extension(iname)) != NULL ){ @@ -4020,13 +4032,13 @@ char * nifti_makeimgname(const char * prefix, int nifti_type, int check, } } /* otherwise, make one up */ - else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); - else if( nifti_type == NIFTI_FTYPE_NIFTI2_1 ) strcat(iname, extnii); - else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); - else strcat(iname, extimg); + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strlcat(iname, extnii, inamelength); + else if( nifti_type == NIFTI_FTYPE_NIFTI2_1 ) strlcat(iname, extnii, inamelength); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strlcat(iname, extnia, inamelength); + else strlcat(iname, extimg, inamelength); #ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ - if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); + if( comp && (!ext || !strstr(iname,extgz)) ) strlcat(iname, extgz, inamelength); #endif /* check for existence failure */ @@ -7368,7 +7380,7 @@ nifti_1_header * nifti_make_new_n1_header(const int64_t arg_dims[8], nifti_datatype_sizes( nhdr->datatype , &nbyper, &swapsize ); nhdr->bitpix = 8 * nbyper ; - strcpy(nhdr->magic, "n+1"); /* init to single file */ + strlcpy(nhdr->magic, "n+1", sizeof(nhdr->magic)); /* init to single file */ return nhdr; } @@ -7500,8 +7512,8 @@ int nifti_convert_nim2n1hdr(const nifti_image * nim, nifti_1_header * hdr) if( nim->nifti_type > NIFTI_FTYPE_ANALYZE ){ /* then not ANALYZE */ - if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcpy(nhdr.magic,"n+1") ; - else strcpy(nhdr.magic,"ni1") ; + if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strlcpy(nhdr.magic, "n+1", sizeof(nhdr.magic)) ; + else strlcpy(nhdr.magic, "ni1", sizeof(nhdr.magic)) ; nhdr.pixdim[1] = (float)fabs(nhdr.pixdim[1]) ; nhdr.pixdim[2] = (float)fabs(nhdr.pixdim[2]) ; @@ -7885,10 +7897,9 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, 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, "\""); + int n = snprintf(command, sizeof(command), "pigz -n -f > \"%s\"", nim->fname); + if (n < 0 || (size_t)n >= sizeof(command)) + return -1; #ifdef _MSC_VER if (( pigzPipe = _popen(command, "w")) == NULL) return -1; @@ -7917,10 +7928,10 @@ int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list 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, "\""); + strlcpy(command, "pigz", sizeof(command)); + strlcat(command, " -n -f > \"", sizeof(command)); + strlcat(command, nim->fname, sizeof(command)); + strlcat(command, "\"", sizeof(command)); #ifdef _MSC_VER if (( pigzPipe = _popen(command, "w")) == NULL) return -1; @@ -8758,7 +8769,7 @@ int nifti_short_order(void) /* determine this CPU's byte order */ put rhs string into nim->"nam" string, with field size = "sz" */ #define QSTR(nam,sz) if( strcmp(lhs,#nam) == 0 ) \ - strncpy(nim->nam,rhs,sz), nim->nam[(sz)-1]='\0' + memset(nim->nam, 0, (sz)), strlcpy(nim->nam,rhs,(sz)) /*---------------------------------------------------------------------------*/ /*! Take an XML-ish ASCII string and create a NIFTI image header to match. diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index 3357a373..e3f7cbfd 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -6123,8 +6123,8 @@ int fill_field( field_s * fp, int type, int offset, int num, const char * name ) fp->size = 1; /* init before check */ fp->len = num; - strncpy(fp->name, name, sizeof(fp->name)); - fp->name[sizeof(fp->name) - 1] = 0; + memset(fp->name, 0, sizeof(fp->name)); + strlcpy(fp->name, name, sizeof(fp->name)); switch( type ){ case DT_UNKNOWN: diff --git a/nifticdf/nifticdf.c b/nifticdf/nifticdf.c index a2909d90..b891ab20 100644 --- a/nifticdf/nifticdf.c +++ b/nifticdf/nifticdf.c @@ -11049,10 +11049,11 @@ int nifti_intent_code( const char *name ) if( name == NULL || *name == '\0' ) return -1 ; - unam = (char *)malloc(strlen(name)+1); + size_t unamlen = strlen(name)+1; + unam = (char *)malloc(unamlen); if (!unam) return -1 ; - strcpy(unam,name); + strlcpy(unam,name,unamlen); for( upt=unam ; *upt != '\0' ; upt++ ) *upt = (char)toupper(*upt) ; for( ii=NIFTI_FIRST_STATCODE ; ii <= NIFTI_LAST_STATCODE ; ii++ ) diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 870b3d7d..15d4a7ce 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -1,5 +1,9 @@ /* popen()/pclose() in the PIGZ write path are POSIX, not ISO C11. */ +#ifdef PIGZ +#ifdef HAVE_ZLIB #define _POSIX_C_SOURCE 200809L +#endif +#endif #define NIFTI1_IO_C @@ -10,6 +14,7 @@ #include #include +#include /*****===================================================================*****/ /***** Sample functions to deal with NIFTI-1 and ANALYZE files *****/ @@ -1179,13 +1184,13 @@ char *nifti_strdup(const char *str) { if( !str ) return NULL; /* allow calls passing NULL */ - size_t length = strlen(str); - char *dup = (char *)malloc(length + 1); + size_t length = strlen(str) + 1; + char *dup = (char *)malloc(length); /* check for failure */ - if( dup ) strcpy(dup, str); + if( dup ) strlcpy(dup, str, length); else fprintf(stderr,"** nifti_strdup: failed to alloc %zu bytes\n", - length+1); + length); return dup; } @@ -2615,7 +2620,7 @@ const char * nifti_find_file_extension( const char * name ) ext = name + len - 4; /* make manipulation copy, and possibly convert to lowercase */ - strcpy(extcopy, ext); + strlcpy(extcopy, ext, sizeof(extcopy)); if( g_opts.allow_upper_fext ) make_lowercase(extcopy); /* if it look like a basic extension, fail or return it */ @@ -2633,11 +2638,13 @@ const char * nifti_find_file_extension( const char * name ) ext = name + len - 7; /* make manipulation copy, and possibly convert to lowercase */ - strcpy(extcopy, ext); + strlcpy(extcopy, ext, sizeof(extcopy)); if( g_opts.allow_upper_fext ) make_lowercase(extcopy); /* go after .gz extensions using the modifiable strings */ - strcat(elist[0], extgz); strcat(elist[1], extgz); strcat(elist[2], extgz); + strlcat(elist[0], extgz, sizeof(extnii)); + strlcat(elist[1], extgz, sizeof(exthdr)); + strlcat(elist[2], extgz, sizeof(extimg)); if( compare_strlist(extcopy, elist, 3) >= 0 ) { if( is_mixedcase(ext) ) { @@ -2814,21 +2821,23 @@ char * nifti_findhdrname(const char* fname) make_uppercase(extzip); } - hdrname = (char *)calloc(sizeof(char),strlen(basename)+8); + size_t hdrnamelength = strlen(basename)+8; + hdrname = (char *)calloc(sizeof(char),hdrnamelength); if( !hdrname ){ fprintf(stderr,"** nifti_findhdrname: failed to alloc hdrname\n"); free(basename); return NULL; } - strcpy(hdrname,basename); - strcat(hdrname,elist[efirst]); + strlcpy(hdrname, basename, hdrnamelength); + strlcat(hdrname, elist[efirst], hdrnamelength); #ifdef FSLSTYLE if (nifti_fileexists(hdrname)) { free(basename); - char *gzname = (char *)calloc(sizeof(char),strlen(hdrname)+8); - strcpy(gzname, hdrname); - strcat(gzname,extzip); + size_t gznamelength = strlen(hdrname)+8; + char *gzname = (char *)calloc(sizeof(char), gznamelength); + strlcpy(gzname, hdrname, gznamelength); + strlcat(gzname, extzip, gznamelength); if (nifti_fileexists(gzname)) { fprintf(stderr,"Image Exception : Multiple possible filenames detected for basename (*.nii, *.nii.gz): %s\n", basename); free(gzname); @@ -2841,7 +2850,7 @@ char * nifti_findhdrname(const char* fname) if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #endif #ifdef HAVE_ZLIB - strcat(hdrname,extzip); + strlcat(hdrname, extzip, hdrnamelength); if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #endif @@ -2849,11 +2858,11 @@ char * nifti_findhdrname(const char* fname) efirst = 1 - efirst; - strcpy(hdrname,basename); - strcat(hdrname,elist[efirst]); + strlcpy(hdrname, basename, hdrnamelength); + strlcat(hdrname, elist[efirst], hdrnamelength); if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #ifdef HAVE_ZLIB - strcat(hdrname,extzip); + strlcat(hdrname, extzip, hdrnamelength); if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } #endif @@ -2893,8 +2902,9 @@ char * nifti_findimgname(const char* fname , int nifti_type) /* check input file(s) for sanity */ if( !nifti_validfilename(fname) ) return NULL; - basename = nifti_makebasename(fname); - imgname = (char *)calloc(sizeof(char),strlen(basename)+8); + basename = nifti_makebasename(fname); + size_t imgnamelength = strlen(basename)+8; + imgname = (char *)calloc(sizeof(char),imgnamelength); if( !imgname ){ fprintf(stderr,"** nifti_findimgname: failed to alloc imgname\n"); free(basename); @@ -2912,8 +2922,8 @@ char * nifti_findimgname(const char* fname , int nifti_type) /* only valid extension for ASCII type is .nia, handle first */ if( nifti_type == NIFTI_FTYPE_ASCII ){ - strcpy(imgname,basename); - strcat(imgname,extnia); + strlcpy(imgname, basename, imgnamelength); + strlcat(imgname, extnia, imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } } else { @@ -2926,21 +2936,21 @@ char * nifti_findimgname(const char* fname , int nifti_type) if (nifti_type == NIFTI_FTYPE_NIFTI1_1) first = 0; /* should match .nii */ else first = 1; /* should match .img */ - strcpy(imgname,basename); - strcat(imgname,elist[first]); + strlcpy(imgname, basename, imgnamelength); + strlcat(imgname, elist[first], imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } #ifdef HAVE_ZLIB /* then also check for .gz */ - strcat(imgname,extzip); + strlcat(imgname, extzip, imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } #endif /* failed to find image file with expected extension, try the other */ - strcpy(imgname,basename); - strcat(imgname,elist[1-first]); /* can do this with only 2 choices */ + strlcpy(imgname, basename, imgnamelength); + strlcat(imgname, elist[1-first], imgnamelength); /* can do this with only 2 choices */ if (nifti_fileexists(imgname)) { free(basename); return imgname; } #ifdef HAVE_ZLIB /* then also check for .gz */ - strcat(imgname,extzip); + strlcat(imgname, extzip, imgnamelength); if (nifti_fileexists(imgname)) { free(basename); return imgname; } #endif } @@ -2981,9 +2991,10 @@ char * nifti_makehdrname(const char * prefix, int nifti_type, int check, if( !nifti_validfilename(prefix) ) return NULL; /* add space for extension, optional ".gz", and null char */ - iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + size_t inamelength = strlen(prefix)+8; + iname = (char *)calloc(sizeof(char),inamelength); if( !iname ){ fprintf(stderr,"** small malloc failure!\n"); return NULL; } - strcpy(iname, prefix); + strlcpy(iname, prefix, inamelength); /* use any valid extension */ if( (ext = nifti_find_file_extension(iname)) != NULL ){ @@ -3002,12 +3013,12 @@ char * nifti_makehdrname(const char * prefix, int nifti_type, int check, } } /* otherwise, make one up */ - else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); - else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); - else strcat(iname, exthdr); + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strlcat(iname, extnii, inamelength); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strlcat(iname, extnia, inamelength); + else strlcat(iname, exthdr, inamelength); #ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ - if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); + if( comp && (!ext || !strstr(iname,extgz)) ) strlcat(iname, extgz, inamelength); #endif /* check for existence failure */ @@ -3052,9 +3063,10 @@ char * nifti_makeimgname(const char * prefix, int nifti_type, int check, if( !nifti_validfilename(prefix) ) return NULL; /* add space for extension, optional ".gz", and null char */ - iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + size_t inamelength = strlen(prefix)+8; + iname = (char *)calloc(sizeof(char),inamelength); if( !iname ){ fprintf(stderr,"** small malloc failure!\n"); return NULL; } - strcpy(iname, prefix); + strlcpy(iname, prefix, inamelength); /* use any valid extension */ if( (ext = nifti_find_file_extension(iname)) != NULL ){ @@ -3073,12 +3085,12 @@ char * nifti_makeimgname(const char * prefix, int nifti_type, int check, } } /* otherwise, make one up */ - else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); - else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); - else strcat(iname, extimg); + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strlcat(iname, extnii, inamelength); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strlcat(iname, extnia, inamelength); + else strlcat(iname, extimg, inamelength); #ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ - if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); + if( comp && (!ext || !strstr(iname,extgz)) ) strlcat(iname, extgz, inamelength); #endif /* check for existence failure */ @@ -5356,7 +5368,7 @@ nifti_image* nifti_simple_init_nim(void) nifti_datatype_sizes( nhdr.datatype , &nbyper, &swapsize ); nhdr.bitpix = 8 * nbyper ; - strcpy(nhdr.magic, "n+1"); /* init to single file */ + strlcpy(nhdr.magic, "n+1", sizeof(nhdr.magic)); /* init to single file */ nim = nifti_convert_nhdr2nim(nhdr,NULL); nim->fname = NULL; @@ -5437,7 +5449,7 @@ nifti_1_header * nifti_make_new_header(const int arg_dims[8], int arg_dtype) nifti_datatype_sizes( nhdr->datatype , &nbyper, &swapsize ); nhdr->bitpix = 8 * nbyper ; - strcpy(nhdr->magic, "n+1"); /* init to single file */ + strlcpy(nhdr->magic, "n+1", sizeof(nhdr->magic)); /* init to single file */ return nhdr; } @@ -5546,8 +5558,8 @@ struct nifti_1_header nifti_convert_nim2nhdr(const nifti_image * nim) if( nim->nifti_type > NIFTI_FTYPE_ANALYZE ){ /* then not ANALYZE */ - if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcpy(nhdr.magic,"n+1") ; - else strcpy(nhdr.magic,"ni1") ; + if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strlcpy(nhdr.magic, "n+1", sizeof(nhdr.magic)) ; + else strlcpy(nhdr.magic, "ni1", sizeof(nhdr.magic)) ; nhdr.pixdim[1] = (float)fabs(nhdr.pixdim[1]) ; nhdr.pixdim[2] = (float)fabs(nhdr.pixdim[2]) ; nhdr.pixdim[3] = (float)fabs(nhdr.pixdim[3]) ; nhdr.pixdim[4] = (float)fabs(nhdr.pixdim[4]) ; @@ -5787,10 +5799,9 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, 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, "\""); + int n = snprintf(command, sizeof(command), "pigz -n -f > \"%s\"", nim->fname); + if (n < 0 || (size_t)n >= sizeof(command)) + return -1; #ifdef _MSC_VER if (( pigzPipe = _popen(command, "w")) == NULL) return -1; @@ -5819,10 +5830,10 @@ int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list 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, "\""); + strlcpy(command, "pigz", sizeof(command)); + strlcat(command, " -n -f > \"", sizeof(command)); + strlcat(command, nim->fname, sizeof(command)); + strlcat(command, "\"", sizeof(command)); #ifdef _MSC_VER if (( pigzPipe = _popen(command, "w")) == NULL) return -1; @@ -6627,7 +6638,7 @@ int nifti_short_order(void) /* determine this CPU's byte order */ put rhs string into nim->"nam" string, with field size = "sz" */ #define QSTR(nam,sz) if( strcmp(lhs,#nam) == 0 ) \ - strncpy(nim->nam,rhs,sz), nim->nam[(sz)-1]='\0' + memset(nim->nam, 0, (sz)), strlcpy(nim->nam,rhs,(sz)) /*---------------------------------------------------------------------------*/ /*! Take an XML-ish ASCII string and create a NIFTI image header to match. diff --git a/niftilib/nifti1_test.c b/niftilib/nifti1_test.c index 46808353..33b0bf4d 100644 --- a/niftilib/nifti1_test.c +++ b/niftilib/nifti1_test.c @@ -1,3 +1,4 @@ +#include #include /* directly include I/O library functions */ @@ -19,7 +20,6 @@ int main( int argc , const char *argv[] ) nifti_image *nim ; int iarg=1 , outmode=1 , argn, usegzip=0; char *tmpstr; - size_t ll; if( argc < 2 || strcmp(argv[1],"-help") == 0 ){ printf("Usage: nifti1_test [-n2|-n1|-na|-a2] infile [prefix]\n" @@ -79,24 +79,24 @@ int main( int argc , const char *argv[] ) free(nim->fname) ; free(nim->iname) ; - ll = strlen(argv[iarg]) ; + size_t ll = strlen(argv[iarg]) + 8 ; 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) ; + nim->fname = (char *)calloc(1,ll) ; strlcpy(nim->fname, tmpstr, ll) ; + nim->iname = (char *)calloc(1,ll) ; strlcpy(nim->iname, tmpstr, ll) ; free(tmpstr); if( nim->nifti_type == 1 ){ - strcat(nim->fname,".nii") ; - strcat(nim->iname,".nii") ; + strlcat(nim->fname, ".nii", ll) ; + strlcat(nim->iname, ".nii", ll) ; } else if ( nim->nifti_type == 3 ){ - strcat(nim->fname,".nia") ; - strcat(nim->iname,".nia") ; + strlcat(nim->fname, ".nia", ll) ; + strlcat(nim->iname, ".nia", ll) ; } else { - strcat(nim->fname,".hdr") ; - strcat(nim->iname,".img") ; + strlcat(nim->fname, ".hdr", ll) ; + strlcat(nim->iname, ".img", ll) ; } if (usegzip) { - strcat(nim->fname,".gz"); - strcat(nim->iname,".gz"); + strlcat(nim->fname, ".gz", ll); + strlcat(nim->iname, ".gz", ll); } if( nifti_image_write_status( nim ) ) { fprintf(stderr, "** failed to write nifti_image\n"); diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index a6fb0b3a..4a1b9668 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -3368,8 +3368,8 @@ int fill_field( field_s * fp, int type, int offset, int num, const char * name ) fp->size = 1; /* init before check */ fp->len = num; - strncpy(fp->name, name, sizeof(fp->name)); - fp->name[sizeof(fp->name) - 1] = 0; + memset(fp->name, 0, sizeof(fp->name)); + strlcpy(fp->name, name, sizeof(fp->name)); switch( type ){ case DT_UNKNOWN: diff --git a/niftilib/nifti_tester001.c b/niftilib/nifti_tester001.c index 81677f5d..934ff336 100644 --- a/niftilib/nifti_tester001.c +++ b/niftilib/nifti_tester001.c @@ -2,6 +2,7 @@ * test program for NIFTI lib. */ #include +#include #include static void PrintTest_eng(const int line,const char * message,const int FailureOccured, bool isFatal,int *ErrorAccum,bool verbose) @@ -80,11 +81,9 @@ static nifti_image * generate_reference_image( const char * write_image_filename reference_header.magic[1]='+'; reference_header.magic[2]='1'; reference_header.magic[3]='\0'; - /* String is purposfully too long */ - strncpy(reference_header.intent_name,"PHANTOM_DATA to be used for regression testing the nifti reader/writer",sizeof(reference_header.intent_name)); - reference_header.intent_name[sizeof(reference_header.intent_name) - 1] = 0; - strncpy(reference_header.descrip,"This is a very long dialog here to use up more than 80 characters of space to test to see if the code is robust enough to deal appropriately with very long and obnoxious lines.",sizeof(reference_header.descrip)); - reference_header.descrip[sizeof(reference_header.descrip) - 1] = 0; + /* String is purposefully too long */ + strlcpy(reference_header.intent_name,"PHANTOM_DATA to be used for regression testing the nifti reader/writer",sizeof(reference_header.intent_name)); + strlcpy(reference_header.descrip,"This is a very long dialog here to use up more than 80 characters of space to test to see if the code is robust enough to deal appropriately with very long and obnoxious lines.",sizeof(reference_header.descrip)); { int nbyper; diff --git a/string_helper/string_helper.c b/string_helper/string_helper.c new file mode 100644 index 00000000..a5bea5e0 --- /dev/null +++ b/string_helper/string_helper.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +// Apple provides strlcpy and strlcat, so don't redefine them. +#ifndef __APPLE__ + +// libc 2.38 and above provides strlcpy and strlcat, so don't redefine them. +#if defined (__GLIBC__) && ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 38))) + +// --------------------------------------------------------------------------------------------------------------- +size_t strlcpy(char* ioDestination, const char* inSource, size_t inDestinationSize) +{ + assert(inSource); + + size_t sourceLength = strlen(inSource); + + if (inDestinationSize) + { + size_t length = (sourceLength >= inDestinationSize) ? inDestinationSize - 1 : sourceLength; + memcpy(ioDestination, inSource, length); + ioDestination[length] = '\0'; + } + + return sourceLength; +} + + +// --------------------------------------------------------------------------------------------------------------- +size_t strlcat(char* ioDestination, const char* inSource, size_t inDestinationSize) +{ + assert(ioDestination); + assert(inSource); + + size_t destinationLength = strlen(ioDestination); + size_t sourceLength = strlen(inSource); + size_t totalLength = destinationLength + sourceLength; + + assert(destinationLength < inDestinationSize); + + ioDestination += destinationLength; + inDestinationSize -= destinationLength; + if (sourceLength >= inDestinationSize) + { + sourceLength = inDestinationSize - 1; + } + memcpy(ioDestination, inSource, sourceLength); + ioDestination[sourceLength] = '\0'; + + return totalLength; +} + +#endif +#endif + + diff --git a/string_helper/string_helper.h b/string_helper/string_helper.h new file mode 100644 index 00000000..90ed7f75 --- /dev/null +++ b/string_helper/string_helper.h @@ -0,0 +1,14 @@ +// Apple provides strlcpy and strlcat, so don't redefine them. +#ifndef __APPLE__ + +// libc 2.38 and above provides strlcpy and strlcat, so don't redefine them. +#if defined (__GLIBC__) && ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 38))) + +#include + +size_t strlcpy(char* ioDestination, const char* inSource, size_t inDestinationSize); + +size_t strlcat(char* ioDestination, const char* inSource, size_t inDestinationSize); + +#endif +#endif