Repository navigation
BUG: Bound the formatted write in znzprintf, and end the va_list on failure #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3276516
b07e851
1adac61
d64146e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Google Carbon] Non-blocking suggestion (if |
||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Google Carbon] Minor diagnostic nit: when |
||
| 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 | ||
|
|
||
| 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; | ||
| } |
There was a problem hiding this comment.
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 matchniftilib/CMakeLists.txtandnifti2/CMakeLists.txtwhenNIFTI_clibis vendored (e.g. ITK).