Skip to content
Open
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
10 changes: 10 additions & 0 deletions znzlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Minor CMake consistency nit: prefix the test executable and test name with ${NIFTI_PACKAGE_PREFIX} (${NIFTI_PACKAGE_PREFIX}znzprintf_test / ${NIFTI_PACKAGE_PREFIX}znzprintf) to match niftilib/CMakeLists.txt and nifti2/CMakeLists.txt when NIFTI_clib is vendored (e.g. ITK).

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 $<TARGET_FILE:znzprintf_test> ${CMAKE_CURRENT_BINARY_DIR}/znzprintf_test.gz)
endif()
22 changes: 17 additions & 5 deletions znzlib/znzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Non-blocking suggestion (if znzprintf is kept rather than deleted per @seanm's question): since this codebase requires C11, va_copy lets you format into a small stack buffer (e.g. char stackbuf[512]) first and only malloc((size_t)written + 1) on the rare call that exceeds 512 bytes — avoiding both a 1 MB heap calloc on every tiny znzprintf call and the arbitrary 1 MB truncation ceiling.

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Minor diagnostic nit: when written < 0 (vsnprintf encoding/EOVERFLOW failure rather than (size_t)written >= size), this message still reports "znzprintf output truncated at %zu bytes".

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
Expand Down
12 changes: 11 additions & 1 deletion znzlib/znzlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
108 changes: 108 additions & 0 deletions znzlib/znzprintf_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* Exercises znzprintf(), which is compiled only under
COMPILE_NIFTIUNUSED_CODE and has no caller inside this tree. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;
}
Loading