diff --git a/znzlib/CMakeLists.txt b/znzlib/CMakeLists.txt index 10239be6..a703ad94 100644 --- a/znzlib/CMakeLists.txt +++ b/znzlib/CMakeLists.txt @@ -27,3 +27,13 @@ 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 exists only under COMPILE_NIFTIUNUSED_CODE and off Windows, so build a private copy. + 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 + COMMAND $ ${CMAKE_CURRENT_BINARY_DIR}/znzprintf_test.gz) +endif() diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index 52b5665d..6802a967 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -297,19 +297,31 @@ int znzprintf(znzFile stream, const char *format, ...) int retval=0; char *tmpstr; va_list va; - if (stream==NULL) { return 0; } + /* failures return -1 as printf does; 0 is a successful empty write */ + 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; /* znzprintf_test.c mirrors this 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 ){ + /* a partial record reported as complete is worse than none */ + 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); + /* gzprintf writes nothing and returns 0 past its 8192 byte buffer */ + retval=gzwrite(stream->zfptr,tmpstr,(unsigned)written); + if( retval != written ) retval = -1; free(tmpstr); } else #endif diff --git a/znzlib/znzlib.h b/znzlib/znzlib.h index ff031687..c4b5b53f 100644 --- a/znzlib/znzlib.h +++ b/znzlib/znzlib.h @@ -145,12 +145,22 @@ 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) -ZNZ_API int znzprintf(znzFile stream, const char *format, ...); +/* checks callers' formats, so the inner vsnprintf passes -Wformat-nonliteral */ +#if defined(__GNUC__) || defined(__clang__) +#define ZNZ_PRINTF_ATTR __attribute__((format(printf, 2, 3))) +#else +#define ZNZ_PRINTF_ATTR +#endif +ZNZ_API int znzprintf(znzFile stream, const char *format, ...) ZNZ_PRINTF_ATTR; #endif #endif diff --git a/znzlib/znzprintf_test.c b/znzlib/znzprintf_test.c new file mode 100644 index 00000000..23373872 --- /dev/null +++ b/znzlib/znzprintf_test.c @@ -0,0 +1,108 @@ +/* Exercises znzprintf(), which is compiled only under + COMPILE_NIFTIUNUSED_CODE and has no caller inside this tree. */ + +#include +#include +#include + +#include "znzlib.h" + +/* must match the strlen(format) + 1000000 buffer in znzprintf, znzlib.c */ +#define ZNZPRINTF_BOUND 1000000 +/* past zlib's 8192 byte gzprintf buffer, inside ZNZPRINTF_BOUND */ +#define MID_LEN 100000 +#define BIG_LEN (2 * ZNZPRINTF_BOUND) + +static const char expected[] = "abc\n"; + +static char *filled(size_t len) +{ + char *s = (char *)malloc(len + 1); + if( s != NULL ){ memset(s, 'x', len); s[len] = '\0'; } + return s; +} + +int main(int argc, char *argv[]) +{ + const char *path; + znzFile zf; + char *mid; + char *big; + char *readback; + int ret; + int status = 0; + size_t want = sizeof(expected) - 1 + MID_LEN; + size_t got; + + if( argc < 2 ){ fprintf(stderr,"usage: %s OUTFILE.gz\n", argv[0]); return 1; } + path = argv[1]; + + ret = znzprintf(NULL, "x"); + if( ret >= 0 ){ + fprintf(stderr,"** FAIL: NULL stream returned %d, expected a negative value\n", ret); + status = 1; + } + + mid = filled(MID_LEN); + big = filled(BIG_LEN); + readback = (char *)calloc(1, want + 2); + if( mid == NULL || big == NULL || readback == NULL ){ + fprintf(stderr,"** cannot allocate\n"); + free(mid); free(big); free(readback); + return 1; + } + + zf = znzopen(path, "wb", 1); + if( zf == NULL ){ + fprintf(stderr,"** cannot open %s\n", path); + free(mid); free(big); free(readback); + return 1; + } + + ret = znzprintf(zf, "%s", expected); + if( ret != (int)strlen(expected) ){ + fprintf(stderr,"** FAIL: ordinary write returned %d, expected %d\n", + ret, (int)strlen(expected)); + status = 1; + } + + ret = znzprintf(zf, "%s", mid); + if( ret != MID_LEN ){ + fprintf(stderr,"** FAIL: %d byte write returned %d, expected %d\n", + MID_LEN, ret, MID_LEN); + status = 1; + } + + ret = znzprintf(zf, "%s", big); + if( ret >= 0 ){ + fprintf(stderr,"** FAIL: truncating write returned %d, expected a " + "negative value\n", ret); + status = 1; + } + + if( znzclose(zf) != 0 ){ + fprintf(stderr,"** cannot close %s\n", path); + free(mid); free(big); free(readback); + return 1; + } + + zf = znzopen(path, "rb", 1); + if( zf == NULL ){ + fprintf(stderr,"** cannot reopen %s\n", path); + free(mid); free(big); free(readback); + return 1; + } + got = znzread(readback, 1, want + 1, zf); + znzclose(zf); + + if( got != want || strncmp(readback, expected, strlen(expected)) != 0 + || strcmp(readback + strlen(expected), mid) != 0 ){ + fprintf(stderr,"** FAIL: file holds %zu bytes, expected only the %zu " + "bytes of the two complete writes\n", got, want); + status = 1; + } + + free(mid); free(big); free(readback); + if( status == 0 ) printf("znzprintf test passed\n"); + return status; +}