Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 25 additions & 2 deletions cifti/afni_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
68 changes: 46 additions & 22 deletions cifti/afni_xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
23 changes: 0 additions & 23 deletions cifti/afni_xml_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------- */

Expand Down
4 changes: 4 additions & 0 deletions nifti2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:${TOOL_NAME}> -disp_hdr2 -infiles ${PROJECT_SOURCE_DIR}/niftilib/testdata/dup_attr.nia )

unset(TEST_SUFFIX)
unset(TEST_PREFIX)
unset(TOOL_NAME)
Expand Down
11 changes: 8 additions & 3 deletions nifti2/nifti2_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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" );
Expand Down
4 changes: 3 additions & 1 deletion niftilib/nifti1_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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" );
Expand Down
12 changes: 12 additions & 0 deletions znzlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:znzprintf_test> ${CMAKE_CURRENT_BINARY_DIR}/znzprintf_test.gz)
endif()
20 changes: 16 additions & 4 deletions znzlib/znzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions znzlib/znzlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading