From 091067bf0d26795fd5126544a4f708c06b4d4289 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 00:00:00 -0400 Subject: [PATCH 1/3] BUG: Check the allocations whose result is used immediately Three allocations had their result used without a NULL check, in every case within a line or two, so an allocation failure is a null dereference rather than an error. nifti2_io.c nifti_read_n2_hdr() malloc(nifti_2_header), then nifti_convert_nim2n2hdr() fills it afni_xml.c new_afni_xml(), strdup results stored straight axml_add_attrs() into the structure Each returns an error the way the failure paths beside it do. One line further on, nifti_read_n2_hdr() released the image it had just converted with free(nim) rather than nifti_image_free(), leaking the filename strings nifti_read_ascii_image() had allocated. That is corrected in the same place. The four fslio.c allocations in the same class are left out: the idiom that file uses to report an allocation failure, FSLIOERR, calls exit(), and whether the library may end its host process is for the maintainers to decide. --- cifti/afni_xml.c | 14 +++++++++++++- nifti2/nifti2_io.c | 7 ++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index d7cc9a4f..54467434 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -412,7 +412,14 @@ afni_xml_t * new_afni_xml(const char * name) newp->xparent = NULL; newp->xchild = NULL; - if( name ) newp->name = strdup(name); + if( name ) { + newp->name = strdup(name); + if( ! newp->name ) { + fprintf(stderr,"** new_afni_xml: failed to copy name '%s'\n", name); + free(newp); + return NULL; + } + } return newp; } @@ -457,6 +464,11 @@ int axml_add_attrs(afni_xml_t * ax, const char ** attr) for(c = 0, aind = 0; attr[c]; c += 2, aind++) { ax->attrs.name[aind] = strdup(strip_whitespace(attr[c],0)); ax->attrs.value[aind] = strdup(strip_whitespace(attr[c+1],0)); + if( ! ax->attrs.name[aind] || ! ax->attrs.value[aind] ) { + fprintf(stderr,"** NAX: failed to copy attribute %d\n", aind); + ax->attrs.length = aind+1; /* so the partial pair is still freed */ + return 1; + } } return 0; diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index ae0d8070..58bc2bfb 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -5492,8 +5492,13 @@ nifti_2_header * nifti_read_n2_hdr(const char * hname, int * swapped, if( ! nim ) return NULL; hptr = (nifti_2_header *)malloc(sizeof(nifti_2_header)); + if( ! hptr ){ + fprintf(stderr,"** nifti_read_n2_hdr: failed to alloc nifti_2_header\n"); + nifti_image_free(nim); + return NULL; + } rv = nifti_convert_nim2n2hdr(nim, hptr); - free(nim); + nifti_image_free(nim); /* free(nim) leaked nim's filename strings */ if( rv ) { free(hptr); return NULL; } return hptr; From f557402f3c6979f4ad45f5f97231fe7d24682397 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:52:16 -0400 Subject: [PATCH 2/3] BUG: Report the attribute failure instead of discarding it axml_add_attrs() returns 1 when it cannot copy an attribute, but make_afni_xml() ignored the result, so the element was returned with a partial attribute list and nothing said so. Every failure the function reports is an allocation failure, so there is no case where continuing is right. Propagate it: epush() already treats a NULL from make_afni_xml() by entering a skip block, which is what the allocation-failure paths beside it do. axml_free_xml_t() releases what was built. It walks the attributes with attrs.length, which axml_add_attrs() lowers to the pair it failed on, so the half of that pair that was copied is freed and the entries past it are never read. That matters because the two arrays come from malloc(), not calloc(), so those entries hold uninitialized pointers. --- cifti/afni_xml.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 54467434..45b72e52 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -850,7 +850,11 @@ static afni_xml_t * make_afni_xml(const char * ename, const char ** attr) newp = new_afni_xml(ename); if( ! newp ) return NULL; - axml_add_attrs(newp, attr); + /* a failure here is an allocation failure; epush() skips on NULL */ + if( axml_add_attrs(newp, attr) ) { + axml_free_xml_t(newp); + return NULL; + } return newp; } From a1e0d8cce598d026fab091b5a15d5ae7f9c91dbf Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 10:20:02 -0500 Subject: [PATCH 3/3] ENH: Cover the ASCII path through the NIFTI-2 header reader nifti_read_n2_hdr() builds a nifti_image to convert an ASCII header and then released it with free(), which leaves its filename strings behind. No test reached that path, so the suite reported 362 of 362 either way. The fixture is the one the ASCII attribute test already uses, read through -disp_hdr2 rather than -disp_nim so the NIFTI-2 reader is the one exercised. The leak is visible to the memcheck and sanitizer legs; an ordinary build stays green with or without the fix. --- nifti2/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index d369220c..4564ed76 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -238,6 +238,10 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_nii_cmp_selftest COMMAND sh ${NIFTI_TEST_SCRIPT_DIR}/nii_cmp_selftest.sh ${NIFTI_TEST_SCRIPT_DIR} ) endif() + # Reading an ASCII header through the NIFTI-2 reader releases the image + # it built; the leak is what the memcheck and sanitizer legs observe. + add_test( NAME ${TEST_PREFIX}_tool_disp_hdr2_ascii COMMAND $ -disp_hdr2 -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/dup_attr.nia ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME)