diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index d7cc9a4f..45b72e52 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; @@ -838,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; } 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) 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;