Avoid the need for ssize_t.

Limit read() and write() requests to sizes that fit in an int.
This allows storing the return value in an int, and avoiding the
need to use or construct an ssize_t type. This is required for
Microsoft C, whose _read and _write functions take an unsigned
request and return an int.
This commit is contained in:
Mark Adler 2016-12-31 10:03:09 -08:00
parent b7fbee2156
commit cca27e95cf
6 changed files with 19 additions and 67 deletions

22
configure vendored
View File

@ -465,23 +465,8 @@ fi
echo >> configure.log echo >> configure.log
# check for ssize_t
cat > $test.c <<EOF
#include <sys/types.h>
ssize_t dummy = 0;
EOF
if try $CC -c $CFLAGS $test.c; then
echo "Checking for ssize_t... Yes." | tee -a configure.log
need_ssizet=0
else
echo "Checking for ssize_t... No." | tee -a configure.log
need_ssizet=1
fi
echo >> configure.log
# find the size_t integer type, if needed # find the size_t integer type, if needed
if test $need_sizet -eq 1 -o $need_ssizet -eq 1; then if test $need_sizet -eq 1; then
cat > $test.c <<EOF cat > $test.c <<EOF
long long dummy = 0; long long dummy = 0;
EOF EOF
@ -521,11 +506,6 @@ if test $need_sizet -eq 1; then
SFLAGS="${SFLAGS} -DNO_SIZE_T=${sizet}" SFLAGS="${SFLAGS} -DNO_SIZE_T=${sizet}"
fi fi
if test $need_ssizet -eq 1; then
CFLAGS="${CFLAGS} -DNO_SSIZE_T=${sizet}"
SFLAGS="${SFLAGS} -DNO_SSIZE_T=${sizet}"
fi
echo >> configure.log echo >> configure.log
# check for large file support, and if none, check for fseeko() # check for large file support, and if none, check for fseeko()

View File

@ -24,11 +24,15 @@ local int gz_load(state, buf, len, have)
unsigned len; unsigned len;
unsigned *have; unsigned *have;
{ {
z_ssize_t ret; int ret;
unsigned get, max = ((unsigned)-1 >> 2) + 1;
*have = 0; *have = 0;
do { do {
ret = read(state->fd, buf + *have, len - *have); get = len - *have;
if (get > max)
get = max;
ret = read(state->fd, buf + *have, get);
if (ret <= 0) if (ret <= 0)
break; break;
*have += (unsigned)ret; *have += (unsigned)ret;

View File

@ -74,9 +74,8 @@ local int gz_comp(state, flush)
gz_statep state; gz_statep state;
int flush; int flush;
{ {
int ret; int ret, writ;
z_ssize_t got; unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
unsigned have;
z_streamp strm = &(state->strm); z_streamp strm = &(state->strm);
/* allocate memory if this is the first time through */ /* allocate memory if this is the first time through */
@ -86,13 +85,14 @@ local int gz_comp(state, flush)
/* write directly if requested */ /* write directly if requested */
if (state->direct) { if (state->direct) {
while (strm->avail_in) { while (strm->avail_in) {
got = write(state->fd, strm->next_in, strm->avail_in); put = strm->avail_in > max ? max : strm->avail_in;
if (got < 0) { writ = write(state->fd, strm->next_in, put);
if (writ < 0) {
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
} }
strm->avail_in -= (unsigned)got; strm->avail_in -= (unsigned)writ;
strm->next_in += got; strm->next_in += writ;
} }
return 0; return 0;
} }
@ -105,13 +105,14 @@ local int gz_comp(state, flush)
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
(flush != Z_FINISH || ret == Z_STREAM_END))) { (flush != Z_FINISH || ret == Z_STREAM_END))) {
while (strm->next_out > state->x.next) { while (strm->next_out > state->x.next) {
got = write(state->fd, state->x.next, put = strm->next_out - state->x.next > (int)max ? max :
(unsigned long)(strm->next_out - state->x.next)); (unsigned)(strm->next_out - state->x.next);
if (got < 0) { writ = write(state->fd, state->x.next, put);
if (writ < 0) {
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
} }
state->x.next += got; state->x.next += writ;
} }
if (strm->avail_out == 0) { if (strm->avail_out == 0) {
strm->avail_out = state->size; strm->avail_out = state->size;

11
zconf.h
View File

@ -237,17 +237,6 @@
# include <stddef.h> # include <stddef.h>
typedef size_t z_size_t; typedef size_t z_size_t;
# endif # endif
# ifdef NO_SSIZE_T
typedef NO_SSIZE_T z_ssize_t;
# else
# include <stddef.h>
# include <sys/types.h>
# ifdef _MSC_VER
typedef intptr_t z_ssize_t;
# else
typedef ssize_t z_ssize_t;
# endif
# endif
# undef z_longlong # undef z_longlong
#endif #endif

View File

@ -239,17 +239,6 @@
# include <stddef.h> # include <stddef.h>
typedef size_t z_size_t; typedef size_t z_size_t;
# endif # endif
# ifdef NO_SSIZE_T
typedef NO_SSIZE_T z_ssize_t;
# else
# include <stddef.h>
# include <sys/types.h>
# ifdef _MSC_VER
typedef intptr_t z_ssize_t;
# else
typedef ssize_t z_ssize_t;
# endif
# endif
# undef z_longlong # undef z_longlong
#endif #endif

View File

@ -237,17 +237,6 @@
# include <stddef.h> # include <stddef.h>
typedef size_t z_size_t; typedef size_t z_size_t;
# endif # endif
# ifdef NO_SSIZE_T
typedef NO_SSIZE_T z_ssize_t;
# else
# include <stddef.h>
# include <sys/types.h>
# ifdef _MSC_VER
typedef intptr_t z_ssize_t;
# else
typedef ssize_t z_ssize_t;
# endif
# endif
# undef z_longlong # undef z_longlong
#endif #endif