Clean up gz* function return values.

In some cases the return values did not match the documentation,
or the documentation did not document all of the return values.
gzprintf() now consistently returns negative values on error,
which matches the behavior of the stdio fprintf() function.
This commit is contained in:
Mark Adler 2016-12-03 08:18:56 -08:00
parent 9dc5a8585f
commit 123f9cfaf7
3 changed files with 37 additions and 34 deletions

View File

@ -615,7 +615,6 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
strcat(state->msg, ": ");
strcat(state->msg, msg);
#endif
return;
}
#ifndef INT_MAX

View File

@ -11,7 +11,8 @@ local int gz_comp OF((gz_statep, int));
local int gz_zero OF((gz_statep, z_off64_t));
/* Initialize state for writing a gzip file. Mark initialization by setting
state->size to non-zero. Return -1 on failure or 0 on success. */
state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
success. */
local int gz_init(state)
gz_statep state;
{
@ -63,11 +64,11 @@ local int gz_init(state)
}
/* Compress whatever is at avail_in and next_in and write to the output file.
Return -1 if there is an error writing to the output file, otherwise 0.
flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
then the deflate() state is reset to start a new gzip stream. If gz->direct
is true, then simply write to the output file without compressing, and
ignore flush. */
Return -1 if there is an error writing to the output file or if gz_init()
fails to allocate memory, otherwise 0. flush is assumed to be a valid
deflate() flush value. If flush is Z_FINISH, then the deflate() state is
reset to start a new gzip stream. If gz->direct is true, then simply write
to the output file without compressing, and ignore flush. */
local int gz_comp(state, flush)
gz_statep state;
int flush;
@ -136,7 +137,8 @@ local int gz_comp(state, flush)
return 0;
}
/* Compress len zeros to output. Return -1 on error, 0 on success. */
/* Compress len zeros to output. Return -1 on a write error or memory
allocation failure by gz_comp(), or 0 on success. */
local int gz_zero(state, len)
gz_statep state;
z_off64_t len;
@ -324,23 +326,23 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
/* get internal structure */
if (file == NULL)
return -1;
return Z_STREAM_ERROR;
state = (gz_statep)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return 0;
return Z_STREAM_ERROR;
/* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1)
return 0;
return state->err;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
return state->err;
}
/* do the printf() into the input buffer, put length in len -- the input
@ -378,7 +380,7 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
left = strm->avail_in - state->size;
strm->avail_in = state->size;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
return state->err;
memcpy(state->in, state->in + state->size, left);
strm->next_in = state->in;
strm->avail_in = left;
@ -414,27 +416,27 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
/* get internal structure */
if (file == NULL)
return -1;
return Z_STREAM_ERROR;
state = (gz_statep)file;
strm = &(state->strm);
/* check that can really pass pointer in ints */
if (sizeof(int) != sizeof(void *))
return 0;
return Z_STREAM_ERROR;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return 0;
return Z_STREAM_ERROR;
/* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1)
return 0;
return state->error;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
return state->error;
}
/* do the printf() into the input buffer, put length in len -- the input
@ -477,7 +479,7 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
left = strm->avail_in - state->size;
strm->avail_in = state->size;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
return state->err;
memcpy(state->in, state->in + state->size, left);
strm->next_in = state->in;
strm->avail_in = left;
@ -496,7 +498,7 @@ int ZEXPORT gzflush(file, flush)
/* get internal structure */
if (file == NULL)
return -1;
return Z_STREAM_ERROR;
state = (gz_statep)file;
/* check that we're writing and that there's no error */
@ -511,7 +513,7 @@ int ZEXPORT gzflush(file, flush)
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
return state->err;
}
/* compress remaining data with requested flush */
@ -546,7 +548,7 @@ int ZEXPORT gzsetparams(file, level, strategy)
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
return state->err;
}
/* change compression parameters for subsequent input */

26
zlib.h
View File

@ -1355,10 +1355,12 @@ ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
/*
Dynamically update the compression level or strategy. See the description
of deflateInit2 for the meaning of these parameters.
of deflateInit2 for the meaning of these parameters. Previously provided
data is flushed before the parameter change.
gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
opened for writing.
gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
opened for writing, Z_ERRNO if there is an error writing the flushed data,
or Z_MEM_ERROR if there is a memory allocation error.
*/
ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
@ -1401,15 +1403,15 @@ ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
/*
Converts, formats, and writes the arguments to the compressed file under
control of the format string, as in fprintf. gzprintf returns the number of
uncompressed bytes actually written, or 0 in case of error. The number of
uncompressed bytes written is limited to 8191, or one less than the buffer
size given to gzbuffer(). The caller should assure that this limit is not
exceeded. If it is exceeded, then gzprintf() will return an error (0) with
nothing written. In this case, there may also be a buffer overflow with
unpredictable consequences, which is possible only if zlib was compiled with
the insecure functions sprintf() or vsprintf() because the secure snprintf()
or vsnprintf() functions were not available. This can be determined using
zlibCompileFlags().
uncompressed bytes actually written, or a negative zlib error code in case
of error. The number of uncompressed bytes written is limited to 8191, or
one less than the buffer size given to gzbuffer(). The caller should assure
that this limit is not exceeded. If it is exceeded, then gzprintf() will
return an error (0) with nothing written. In this case, there may also be a
buffer overflow with unpredictable consequences, which is possible only if
zlib was compiled with the insecure functions sprintf() or vsprintf()
because the secure snprintf() or vsnprintf() functions were not available.
This can be determined using zlibCompileFlags().
*/
ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));