From 49f5b83eb24b7c0cebdff76977110b290d754833 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Mon, 21 Sep 2026 15:48:42 -0500 Subject: [PATCH 1/6] ENH: Declare the three functions that have a published counterpart FslGetHdrImgNames and FslSetIntensityScaling are defined here but declared nowhere, while FslGetIntensityScaling and FslInit are already published. axml_recur_find_xml sits beside axml_recur in afni_xml.h the same way. The upstream fslio was deleted in 2015 in favor of a C++ replacement, so this copy is the surviving one and its header is ours to correct. Additive; no symbol changes. --- cifti/afni_xml.h | 2 ++ fsliolib/fslio.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/cifti/afni_xml.h b/cifti/afni_xml.h index b48758af..ecc4ab3e 100644 --- a/cifti/afni_xml.h +++ b/cifti/afni_xml.h @@ -88,6 +88,8 @@ int axml_free_xlist(afni_xml_list * axlist); char * axml_attr_value(afni_xml_t * ax, const char * name); int axml_recur(int(*func)(FILE*,afni_xml_t*,int), afni_xml_t * ax); +afni_xml_t * axml_recur_find_xml(int (*func)(afni_xml_t *, int), afni_xml_t * ax, + int depth, int max_depth); /* control API */ diff --git a/fsliolib/fslio.h b/fsliolib/fslio.h index d979fd48..0e642b59 100644 --- a/fsliolib/fslio.h +++ b/fsliolib/fslio.h @@ -180,6 +180,8 @@ FSL_API void FslWriteHeader(FSLIO *fslio); FSL_API int FslFileExists(const char *filename); FSL_API char *FslMakeBaseName(const char *fname); +FSL_API void FslGetHdrImgNames(const char* filename, const FSLIO* fslio, + char** hdrname, char** imgname); FSL_API int FslCheckForMultipleFileNames(const char* filename); FSL_API int FslGetEnvOutputType(void); @@ -233,6 +235,7 @@ FSL_API void FslGetTimeUnits(FSLIO *fslio, char *units); FSL_API void FslSetDataType(FSLIO *fslio, short t); FSL_API size_t FslGetDataType(FSLIO *fslio, short *t); FSL_API int FslGetIntensityScaling(FSLIO *fslio, float *slope, float *intercept); +FSL_API void FslSetIntensityScaling(FSLIO *fslio, float slope, float intercept); FSL_API void FslSetIntent(FSLIO *fslio, short intent_code, float p1, float p2, float p3); FSL_API short FslGetIntent(FSLIO *fslio, short *intent_code, float *p1, float *p2, float *p3); From 6a1c119efd964d1d8c12e5048fb9e6841c945cfd Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 17 Nov 2026 16:32:43 -0600 Subject: [PATCH 2/6] BUG: Recurse into the children in axml_recur_find_xml The depth-first search passed the current node, not its child, to the recursive call, so it never visited a child. With max_depth <= 0 and any child present it recursed without bound and overflowed the stack; with a depth limit it returned NULL for any match below the root. The new test builds a three-level tree and checks limited, unlimited, and leaf searches. Without the fix it aborts under ASan with a stack overflow. --- cifti/CMakeLists.txt | 7 ++++ cifti/afni_xml.c | 2 +- cifti/test_axml_recur_find_xml.c | 55 ++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 cifti/test_axml_recur_find_xml.c diff --git a/cifti/CMakeLists.txt b/cifti/CMakeLists.txt index d19b7524..e25c5313 100644 --- a/cifti/CMakeLists.txt +++ b/cifti/CMakeLists.txt @@ -26,3 +26,10 @@ if(NIFTI_BUILD_APPLICATIONS) install_nifti_target(${NIFTI_PACKAGE_PREFIX}afni_xml_tool) install_nifti_target(${NIFTI_PACKAGE_PREFIX}cifti_tool) endif() + +if(NIFTI_BUILD_TESTING) + add_executable(${NIFTI_PACKAGE_PREFIX}test_axml_recur_find_xml test_axml_recur_find_xml.c) + target_link_libraries(${NIFTI_PACKAGE_PREFIX}test_axml_recur_find_xml PRIVATE ${NIFTI_CIFTILIB_NAME}) + add_test(NAME ${NIFTI_PACKAGE_PREFIX}axml_recur_find_xml + COMMAND $) +endif() diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 39da4433..b1de9de1 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -570,7 +570,7 @@ afni_xml_t * axml_recur_find_xml(int(*func)(afni_xml_t *, int), afni_xml_t * ax, /* search deeper */ if( ax->nchild > 0 && ax->xchild ) for( ind=0; ind < ax->nchild; ind++ ) { - rv = axml_recur_find_xml(func, ax, depth+1, max_depth); + rv = axml_recur_find_xml(func, ax->xchild[ind], depth+1, max_depth); if( rv ) return rv; } diff --git a/cifti/test_axml_recur_find_xml.c b/cifti/test_axml_recur_find_xml.c new file mode 100644 index 00000000..0f0bae78 --- /dev/null +++ b/cifti/test_axml_recur_find_xml.c @@ -0,0 +1,55 @@ +#include +#include + +#include "afni_xml.h" + +static int is_target(afni_xml_t * ax, int depth) +{ + (void)depth; + return ax->name && strcmp(ax->name, "target") == 0; +} + +static int check(const char * label, afni_xml_t * got, afni_xml_t * want) +{ + if( got == want ) return 0; + fprintf(stderr, "FAIL %s: got %p, want %p\n", label, (void *)got, (void *)want); + return 1; +} + +int main(void) +{ + char root_name[] = "root", mid_name[] = "mid"; + char other_name[] = "other", target_name[] = "target"; + afni_xml_t root, mid, other, target; + afni_xml_t * root_kids[2]; + afni_xml_t * mid_kids[1]; + int nfail = 0; + + memset(&root, 0, sizeof(root)); + memset(&mid, 0, sizeof(mid)); + memset(&other, 0, sizeof(other)); + memset(&target, 0, sizeof(target)); + + root.name = root_name; + other.name = other_name; + mid.name = mid_name; + target.name = target_name; + + root_kids[0] = &other; + root_kids[1] = ∣ + root.nchild = 2; + root.xchild = root_kids; + + mid_kids[0] = ⌖ + mid.nchild = 1; + mid.xchild = mid_kids; + + nfail += check("depth limit reaches target", axml_recur_find_xml(is_target, &root, 0, 2), &target); + nfail += check("depth limit stops short", axml_recur_find_xml(is_target, &root, 0, 1), NULL); + nfail += check("unlimited depth", axml_recur_find_xml(is_target, &root, 0, 0), &target); + nfail += check("leaf without match", axml_recur_find_xml(is_target, &other, 0, 0), NULL); + + if( nfail ) return 1; + printf("axml_recur_find_xml: all checks passed\n"); + return 0; +} From b56a4ca57588e7b8d1cb310936be00a68b5c17c2 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Mon, 21 Sep 2026 16:00:29 -0500 Subject: [PATCH 3/6] COMP: Give the fsliolib internal functions static linkage Nine functions in fslio.c have external linkage and no declaration in any header. Nothing in the tree calls them across a translation unit, AFNI's vendored copy never calls them, and no public source outside a vendored copy of this file references them, so a caller would have had to declare them itself. Seven of them are used within fslio.c and become static, with their prototypes collected in one block so the boundary between internal and published is visible in one place. FslSetVoxUnits and FslGetVoxUnits have no caller at all and are deleted. Removes nine exported symbols from libfslio. --- fsliolib/fslio.c | 54 ++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index 39a9c3ea..2c81fb9b 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -28,6 +28,15 @@ #include "fslio.h" #include "assert.h" +/* Internal: no header declares these and nothing outside this file calls them. */ +static int FslIsValidFileType(int filetype); +static int FslGetFileType2(const FSLIO *fslio, int quiet); +static int FslFileType(const char *fname); +static int FslGetReadFileType(const FSLIO *fslio); +static void FslInit4Write(FSLIO* fslio, const char* filename, int ft); +static int fsl_fileexists(const char* fname); +static int check_for_multiple_filenames(const char* filename); + static int FslIgnoreMFQ=0; static int FslOverrideOutputType=-1; @@ -56,7 +65,7 @@ const char* FslFileTypeString(int filetype) } -int FslIsValidFileType(int filetype) +static int FslIsValidFileType(int filetype) { if ( (filetype!=FSL_TYPE_ANALYZE) && (filetype!=FSL_TYPE_ANALYZE_GZ) && (filetype!=FSL_TYPE_NIFTI) && (filetype!=FSL_TYPE_NIFTI_GZ) && @@ -85,7 +94,7 @@ int FslBaseFileType(int filetype) } -int FslGetFileType2(const FSLIO *fslio, int quiet) +static int FslGetFileType2(const FSLIO *fslio, int quiet) { if (fslio==NULL) FSLIOERR("FslGetFileType: Null pointer passed for FSLIO"); if ( (fslio->file_mode==FSL_TYPE_MINC) || (fslio->file_mode==FSL_TYPE_MINC_GZ) ) { @@ -188,7 +197,7 @@ int FslGetEnvOutputType(void) } -int FslFileType(const char* fname) +static int FslFileType(const char* fname) { /* return type is FSL_TYPE_* or -1 to indicate undetermined */ /* use name as first priority but if that is ambiguous then resolve using environment */ @@ -222,7 +231,7 @@ int FslFileType(const char* fname) /************************************************************ * FslGetReadFileType ************************************************************/ -/*! \fn int FslGetReadFileType(const FSLIO *fslio) +/*! \fn static int FslGetReadFileType(const FSLIO *fslio) \brief return the best estimate of the true file type This function is used to return the best estimate of the true file type once @@ -387,7 +396,7 @@ void FslSetInit(FSLIO* fslio) -void FslInit4Write(FSLIO* fslio, const char* filename, int ft) +static void FslInit4Write(FSLIO* fslio, const char* filename, int ft) { /* ft determines filetype if ft>=0*/ int imgtype; @@ -500,7 +509,7 @@ void FslCloneHeader(FSLIO *dest, const FSLIO *src) } -int fsl_fileexists(const char* fname) +static int fsl_fileexists(const char* fname) { znzFile fp; fp = znzopen( fname , "rb" , 1 ) ; @@ -557,7 +566,7 @@ int FslCheckForMultipleFileNames(const char* filename) -int check_for_multiple_filenames(const char* filename) +static int check_for_multiple_filenames(const char* filename) { char *basename, *tmpname; char *otype; @@ -1362,37 +1371,6 @@ void FslSetAuxFile(FSLIO *fslio,const char *aux_file) } -void FslSetVoxUnits(FSLIO *fslio, const char *units) -{ - int unitcode=0; - if (fslio==NULL) FSLIOERR("FslSetVoxUnits: Null pointer passed for FSLIO"); - if (fslio->niftiptr!=NULL) { - if (strcmp(units,nifti_units_string(NIFTI_UNITS_METER))==0) { - unitcode = NIFTI_UNITS_METER; - } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MM))==0) { - unitcode = NIFTI_UNITS_MM; - } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MICRON))==0) { - unitcode = NIFTI_UNITS_MICRON; - } - fslio->niftiptr->xyz_units = unitcode; - } - if (fslio->mincptr!=NULL) { - fprintf(stderr,"Warning:: Minc is not yet supported\n"); - } -} - - -void FslGetVoxUnits(FSLIO *fslio, char *units) -{ - if (fslio==NULL) FSLIOERR("FslGetVoxUnits: Null pointer passed for FSLIO"); - if (fslio->niftiptr!=NULL) { - strcpy(units,nifti_units_string(fslio->niftiptr->xyz_units)); - } - if (fslio->mincptr!=NULL) { - fprintf(stderr,"Warning:: Minc is not yet supported\n"); - } -} - void FslSetTimeUnits(FSLIO *fslio, const char *units) { int unitcode=0; From 8721faf12e94e5187616987f473518b11ddb914d Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Mon, 21 Sep 2026 16:00:38 -0500 Subject: [PATCH 4/6] COMP: Declare nifti_fileexists where it is defined nifti1_io.c and nifti2_io.c each define nifti_fileexists with external linkage, so libniftiio and libnifti2 export the same name and ELF link order decides which one a caller linking both resolves to. Declaring it locally silences the warning without choosing between publishing it and making it static; that choice needs the duplication settled first, and it covers a hundred names, not this one. The duplicate definition is left for a later change, because removing either copy or making it static alters the exported API. --- nifti2/nifti2_io.c | 3 +++ niftilib/nifti1_io.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 7ec9c086..464d9c6d 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -474,6 +474,9 @@ static const nifti_type_ele nifti_type_list[] = { { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, }; +/* also defined in libniftiio; both export it */ +int nifti_fileexists(const char* fname); + /*---------------------------------------------------------------------------*/ /* prototypes for internal functions - not part of exported library */ diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 9811e715..12b62ba0 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -413,6 +413,9 @@ static const nifti_type_ele nifti_type_list[] = { { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, }; +/* also defined in libnifti2; both export it */ +int nifti_fileexists(const char* fname); + /*---------------------------------------------------------------------------*/ /* prototypes for internal functions - not part of exported library */ From 604baebf3da6da398a2c8bfe7795f355950ec2e7 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Mon, 21 Sep 2026 13:56:40 -0500 Subject: [PATCH 5/6] STYLE: Derive the aux_file bound from sizeof Keeps FslGetAuxFile and FslSetAuxFile deriving the same length so the pair cannot drift if the field width changes. sizeof is 24, so the copy still writes at most 23 bytes. --- fsliolib/fslio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index 2c81fb9b..a0d50221 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -1349,8 +1349,9 @@ void FslGetAuxFile(FSLIO *fslio,char *aux_file) { if (fslio==NULL) FSLIOERR("FslGetAuxFile: Null pointer passed for FSLIO"); if (fslio->niftiptr!=NULL) { - strncpy(aux_file,fslio->niftiptr->aux_file, 24); - aux_file[24-1] = '\0'; + /* the caller's buffer must hold as many bytes as nifti_image aux_file */ + strncpy(aux_file,fslio->niftiptr->aux_file,sizeof(fslio->niftiptr->aux_file)-1); + aux_file[sizeof(fslio->niftiptr->aux_file)-1] = '\0'; } if (fslio->mincptr!=NULL) { fprintf(stderr,"Warning:: Minc is not yet supported\n"); From 5c2f7095aa4f3ba1236d8719c7d06dd732df255f Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 07:44:30 -0500 Subject: [PATCH 6/6] COMP: Give the pigz writers internal linkage doPigz and doPigz2 are called only from the file that defines them and appear in no header, so they are internal. Declaring them so is what lets -Wmissing-declarations be promoted to an error on the FSLSTYLE path, where they were the only offenders. The NIFTI-1 library also carried a doPigz2, taking a nifti_1_header despite its name, byte-identical to doPigz beside it and called from nowhere. It is removed. The exported symbol set of the default build is unchanged: this code compiles only under PIGZ, which FSLSTYLE enables. --- nifti2/nifti2_io.c | 4 ++-- niftilib/nifti1_io.c | 34 +--------------------------------- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 464d9c6d..9a998ee5 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -7870,7 +7870,7 @@ 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) { +static int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); @@ -7902,7 +7902,7 @@ int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list return 0; } -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { +static int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 12b62ba0..35bff9c4 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -5779,39 +5779,7 @@ 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) { +static int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" );