diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 68197e71..e7250e81 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -196,3 +196,81 @@ jobs: exit 1 fi echo "Exported symbols match the baseline." + + missing-declarations: + # A function defined without a prior declaration is either missing + # from its header or should have been static. Both shapes reached + # master before, because the flag is in the project's clean set but + # nothing makes it fatal, and a warning in a build that passes is + # not read. + # + # Promoting this one flag needs no preparatory cleanup. The whole + # NIFTI_WARNINGS_AS_ERRORS set is still blocked by + # -Wmaybe-uninitialized in fslio.c, and waiting for that would + # leave this uncovered meanwhile. + name: no undeclared external functions (FSLSTYLE=${{ matrix.fslstyle }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + fslstyle: ["OFF", "ON"] + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: sudo apt-get update && sudo apt-get install -y libexpat1-dev zlib1g-dev ninja-build + - name: Configure CMake + run: > + cmake -G Ninja -B ${{ github.workspace }}/build + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_C_FLAGS=-Werror=missing-declarations + -DNIFTI_BUILD_APPLICATIONS=ON + -DUSE_CIFTI_CODE=ON + -DUSE_FSL_CODE=ON + -DFSLSTYLE=${{ matrix.fslstyle }} + -S ${{ github.workspace }} + - name: Build + run: cmake --build ${{ github.workspace }}/build + + windows: + # Twelve source files carry WIN32 or _MSC_VER guards and nothing has + # compiled them for as long as the workflows have existed, so a change + # that breaks the Windows path is invisible until a consumer reports + # it. zlib comes from vcpkg because the project requires it. + name: windows-latest msvc ${{ matrix.linkage }} + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + include: + - linkage: static + shared: "OFF" + triplet: x64-windows-static + - linkage: shared + shared: "ON" + triplet: x64-windows + steps: + - uses: actions/checkout@v4 + - name: Install zlib and expat + run: vcpkg install zlib:${{ matrix.triplet }} expat:${{ matrix.triplet }} + - name: Configure CMake + shell: pwsh + run: | + $toolchain = Join-Path $env:VCPKG_INSTALLATION_ROOT 'scripts/buildsystems/vcpkg.cmake' + Write-Host "vcpkg toolchain: $toolchain" + if (-not (Test-Path $toolchain)) { throw "toolchain file not found: $toolchain" } + cmake -B "${{ github.workspace }}/build" ` + -DCMAKE_TOOLCHAIN_FILE="$toolchain" ` + -DVCPKG_TARGET_TRIPLET=${{ matrix.triplet }} ` + -DBUILD_SHARED_LIBS=${{ matrix.shared }} ` + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY="${{ github.workspace }}/build/bin" ` + -DNIFTI_BUILD_APPLICATIONS=ON ` + -DUSE_NIFTI2_CODE=ON ` + -DUSE_CIFTI_CODE=ON ` + -S "${{ github.workspace }}" + - name: Build + run: cmake --build ${{ github.workspace }}/build --config Release + - name: Test + working-directory: ${{ github.workspace }}/build + # A test that cannot find a DLL can raise a modal dialog and block the + # runner rather than exit, so every test carries a timeout. + run: ctest --build-config Release --output-on-failure --no-tests=ignore --timeout 120 diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 39da4433..25e0d6ee 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -416,7 +416,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; } @@ -461,6 +468,18 @@ 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); + for( c = 0; c <= aind; c++ ) { + free(ax->attrs.name[c]); + free(ax->attrs.value[c]); + } + free(ax->attrs.name); + free(ax->attrs.value); + ax->attrs.name = ax->attrs.value = NULL; + ax->attrs.length = 0; + return 1; + } } return 0; @@ -842,7 +861,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/cifti/afni_xml.h b/cifti/afni_xml.h index ecc4ab3e..16d0ae7c 100644 --- a/cifti/afni_xml.h +++ b/cifti/afni_xml.h @@ -70,38 +70,62 @@ typedef struct { } afni_xml_control; +#ifndef CIF_API + #if defined(_WIN32) || defined(__CYGWIN__) + #if defined(CIFTI_BUILD_SHARED) + #ifdef __GNUC__ + #define CIF_API __attribute__ ((dllexport)) + #else + #define CIF_API __declspec( dllexport ) + #endif + #elif defined(CIFTI_USE_SHARED) + #ifdef __GNUC__ + #define CIF_API __attribute__ ((dllimport)) + #else + #define CIF_API __declspec( dllimport ) + #endif + #else + #define CIF_API + #endif + #elif (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__) + #define CIF_API __attribute__ ((visibility ("default"))) + #else + #define CIF_API + #endif +#endif + /* --------------------------- prototypes --------------------------------- */ /* main interface */ -afni_xml_list axml_read_buf (const char * buf_in, int64_t bin_len); -afni_xml_list axml_read_file(const char * fname, int read_data); +CIF_API afni_xml_list axml_read_buf (const char * buf_in, int64_t bin_len); +CIF_API afni_xml_list axml_read_file(const char * fname, int read_data); -int axml_disp_xlist( const char *mesg, afni_xml_list * axlist, int verb); -int axml_disp_xml_t( const char *mesg, afni_xml_t * ax, int indent, int verb); +CIF_API int axml_disp_xlist( const char *mesg, afni_xml_list * axlist, int verb); +CIF_API int axml_disp_xml_t( const char *mesg, afni_xml_t * ax, int indent, int verb); /* create/free */ -afni_xml_t * new_afni_xml (const char * name); -int axml_add_attrs (afni_xml_t * ax, const char ** attr); -int axml_free_xml_t(afni_xml_t * ax); -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, +CIF_API afni_xml_t * new_afni_xml (const char * name); +CIF_API int axml_add_attrs (afni_xml_t * ax, const char ** attr); +CIF_API int axml_free_xml_t(afni_xml_t * ax); +CIF_API int axml_free_xlist(afni_xml_list * axlist); + +CIF_API char * axml_attr_value(afni_xml_t * ax, const char * name); +CIF_API int axml_recur(int(*func)(FILE*,afni_xml_t*,int), afni_xml_t * ax); +CIF_API afni_xml_t * axml_recur_find_xml(int (*func)(afni_xml_t *, int), afni_xml_t * ax, int depth, int max_depth); /* control API */ -int axml_set_verb ( int val ); -int axml_get_verb ( void ); -int axml_set_dstore ( int val ); -int axml_get_dstore ( void ); -int axml_set_indent ( int val ); -int axml_get_indent ( void ); -int axml_set_buf_size ( int val ); -int axml_get_buf_size ( void ); -int axml_set_wstream ( FILE *fp ); -FILE * axml_get_wstream ( void ); +CIF_API int axml_set_verb ( int val ); +CIF_API int axml_get_verb ( void ); +CIF_API int axml_set_dstore ( int val ); +CIF_API int axml_get_dstore ( void ); +CIF_API int axml_set_indent ( int val ); +CIF_API int axml_get_indent ( void ); +CIF_API int axml_set_buf_size ( int val ); +CIF_API int axml_get_buf_size ( void ); +CIF_API int axml_set_wstream ( FILE *fp ); +CIF_API FILE * axml_get_wstream ( void ); #endif /* AFNI_XML_H */ diff --git a/cifti/afni_xml_io.h b/cifti/afni_xml_io.h index ae8aeb2a..ee9b6a1b 100644 --- a/cifti/afni_xml_io.h +++ b/cifti/afni_xml_io.h @@ -50,29 +50,6 @@ - convert as in SUMA_Create_Fake_CIFTI() * ----------------------------------------------------------------------*/ -#ifndef CIF_API - #if defined(_WIN32) || defined(__CYGWIN__) - #if defined(CIFTI_BUILD_SHARED) - #ifdef __GNUC__ - #define CIF_API __attribute__ ((dllexport)) - #else - #define CIF_API __declspec( dllexport ) - #endif - #elif defined(CIFTI_USE_SHARED) - #ifdef __GNUC__ - #define CIF_API __attribute__ ((dllimport)) - #else - #define CIF_API __declspec( dllimport ) - #endif - #else - #define CIF_API - #endif - #elif (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__) - #define CIF_API __attribute__ ((visibility ("default"))) - #else - #define CIF_API - #endif -#endif /* --------------------------- structures --------------------------------- */ diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index 6630fc79..957c6f00 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -247,6 +247,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 ${PROJECT_SOURCE_DIR}/niftilib/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 5b42cfed..fae0f477 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -5501,8 +5501,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; @@ -7922,7 +7927,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" ); @@ -7954,7 +7959,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 a0b3fd4c..ead6dc26 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -5809,6 +5809,7 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB +#if 0 /* unused here: identical to doPigz below */ int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; @@ -5840,8 +5841,9 @@ int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list free(fp); return 0; } +#endif -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/znzlib/CMakeLists.txt b/znzlib/CMakeLists.txt index 10239be6..5be28397 100644 --- a/znzlib/CMakeLists.txt +++ b/znzlib/CMakeLists.txt @@ -27,3 +27,15 @@ if(BUILD_SHARED_LIBS) target_compile_definitions(${NIFTI_ZNZLIB_NAME} INTERFACE ZNZ_USE_SHARED) endif() install_nifti_target(${NIFTI_ZNZLIB_NAME}) + +if(NIFTI_BUILD_TESTING AND ZLIB_FOUND AND NOT WIN32) + # znzprintf is only compiled under COMPILE_NIFTIUNUSED_CODE, and not at + # all on Windows, so the test builds its own copy of znzlib.c with that + # definition and is registered only where the function exists. + add_executable(znzprintf_test znzprintf_test.c znzlib.c) + target_compile_definitions(znzprintf_test PRIVATE COMPILE_NIFTIUNUSED_CODE) + target_include_directories(znzprintf_test PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${ZLIB_INCLUDE_DIR}) + target_link_libraries(znzprintf_test PRIVATE ${NIFTI_ZLIB_LIBRARIES}) + add_test(NAME znzprintf_truncation + COMMAND $ ${CMAKE_CURRENT_BINARY_DIR}/znzprintf_test.gz) +endif() diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index 3efe2a86..68910f5f 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -298,18 +298,30 @@ int znzprintf(znzFile stream, const char *format, ...) int retval=0; char *tmpstr; va_list va; - if (stream==NULL) { return 0; } + /* the printf family reports failure with a negative value; 0 means an + empty write succeeded, so it cannot be used for the failures below */ + if (stream==NULL) { return -1; } va_start(va, format); #ifdef HAVE_ZLIB if (stream->zfptr!=NULL) { size_t size; /* local to HAVE_ZLIB block */ - size = strlen(format) + 1000000; /* overkill I hope */ + int written; + size = strlen(format) + 1000000; /* still generous, but now a bound */ tmpstr = (char *)calloc(1, size); if( tmpstr == NULL ){ fprintf(stderr,"** ERROR: znzprintf failed to alloc %zu bytes\n", size); - return retval; + va_end(va); + return -1; + } + written = vsnprintf(tmpstr,size,format,va); + if( written < 0 || (size_t)written >= size ){ + /* writing the truncated text would put a partial record in the + file and report it as a complete one, so write nothing */ + fprintf(stderr,"** ERROR: znzprintf output truncated at %zu bytes\n", size-1); + free(tmpstr); + va_end(va); + return -1; } - vsprintf(tmpstr,format,va); retval=gzprintf(stream->zfptr,"%s",tmpstr); free(tmpstr); } else diff --git a/znzlib/znzlib.h b/znzlib/znzlib.h index c0052a40..90d80f1e 100644 --- a/znzlib/znzlib.h +++ b/znzlib/znzlib.h @@ -147,14 +147,25 @@ ZNZ_API int znzputs(const char *str, znzFile file); #ifdef COMPILE_NIFTIUNUSED_CODE ZNZ_API char * znzgets(char* str, int size, znzFile file); +ZNZ_API int znzflush(znzFile file); + +ZNZ_API int znzeof(znzFile file); + ZNZ_API int znzputc(int c, znzFile file); ZNZ_API int znzgetc(znzFile file); #if !defined(WIN32) +/* the attribute lets the caller's format be checked, which is what makes + the internal vsnprintf on it acceptable to -Wformat-nonliteral */ +#if defined(__GNUC__) || defined(__clang__) +ZNZ_API int znzprintf(znzFile stream, const char *format, ...) + __attribute__((format(printf, 2, 3))); +#else ZNZ_API int znzprintf(znzFile stream, const char *format, ...); #endif #endif +#endif /*=================*/ #ifdef __cplusplus diff --git a/znzlib/znzprintf_test.c b/znzlib/znzprintf_test.c new file mode 100644 index 00000000..24b6c321 --- /dev/null +++ b/znzlib/znzprintf_test.c @@ -0,0 +1,67 @@ +/* Exercises znzprintf(), which is compiled only under + COMPILE_NIFTIUNUSED_CODE and has no caller inside this tree. */ + +#include +#include +#include + +#include "znzlib.h" + +#define BIG_LEN 2000000 + +static const char expected[] = "abc\n"; + +int main(int argc, char *argv[]) +{ + const char *path; + znzFile zf; + char *big; + int normal; + int truncated; + int status = 0; + size_t got; + char readback[64]; + + if( argc < 2 ){ fprintf(stderr,"usage: %s OUTFILE.gz\n", argv[0]); return 1; } + path = argv[1]; + + zf = znzopen(path, "wb", 1); + if( zf == NULL ){ fprintf(stderr,"** cannot open %s\n", path); return 1; } + + normal = znzprintf(zf, "%s", expected); + if( normal != (int)strlen(expected) ){ + fprintf(stderr,"** FAIL: ordinary write returned %d, expected %d\n", + normal, (int)strlen(expected)); + status = 1; + } + + big = (char *)malloc(BIG_LEN + 1); + if( big == NULL ){ fprintf(stderr,"** cannot allocate\n"); znzclose(zf); return 1; } + memset(big, 'x', BIG_LEN); + big[BIG_LEN] = '\0'; + + truncated = znzprintf(zf, "%s", big); + if( truncated >= 0 ){ + fprintf(stderr,"** FAIL: truncating write returned %d, expected a " + "negative value\n", truncated); + status = 1; + } + free(big); + + if( znzclose(zf) != 0 ){ fprintf(stderr,"** cannot close %s\n", path); return 1; } + + zf = znzopen(path, "rb", 1); + if( zf == NULL ){ fprintf(stderr,"** cannot reopen %s\n", path); return 1; } + memset(readback, 0, sizeof(readback)); + got = znzread(readback, 1, sizeof(readback) - 1, zf); + znzclose(zf); + + if( got != strlen(expected) || strcmp(readback, expected) != 0 ){ + fprintf(stderr,"** FAIL: file holds %zu bytes, expected only the %d " + "bytes of the ordinary write\n", got, (int)strlen(expected)); + status = 1; + } + + if( status == 0 ) printf("znzprintf test passed\n"); + return status; +}