From cca27e95cf2bf057b2bbea93702135da3ca7be45 Mon Sep 17 00:00:00 2001 From: Mark Adler Date: Sat, 31 Dec 2016 10:03:09 -0800 Subject: [PATCH] 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. --- configure | 22 +--------------------- gzread.c | 8 ++++++-- gzwrite.c | 23 ++++++++++++----------- zconf.h | 11 ----------- zconf.h.cmakein | 11 ----------- zconf.h.in | 11 ----------- 6 files changed, 19 insertions(+), 67 deletions(-) diff --git a/configure b/configure index e0c07b7..e974d1f 100755 --- a/configure +++ b/configure @@ -465,23 +465,8 @@ fi echo >> configure.log -# check for ssize_t -cat > $test.c < -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 -if test $need_sizet -eq 1 -o $need_ssizet -eq 1; then +if test $need_sizet -eq 1; then cat > $test.c <> configure.log # check for large file support, and if none, check for fseeko() diff --git a/gzread.c b/gzread.c index 3811157..deea79b 100644 --- a/gzread.c +++ b/gzread.c @@ -24,11 +24,15 @@ local int gz_load(state, buf, len, have) unsigned len; unsigned *have; { - z_ssize_t ret; + int ret; + unsigned get, max = ((unsigned)-1 >> 2) + 1; *have = 0; 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) break; *have += (unsigned)ret; diff --git a/gzwrite.c b/gzwrite.c index c29308b..b866251 100644 --- a/gzwrite.c +++ b/gzwrite.c @@ -74,9 +74,8 @@ local int gz_comp(state, flush) gz_statep state; int flush; { - int ret; - z_ssize_t got; - unsigned have; + int ret, writ; + unsigned have, put, max = ((unsigned)-1 >> 2) + 1; z_streamp strm = &(state->strm); /* allocate memory if this is the first time through */ @@ -86,13 +85,14 @@ local int gz_comp(state, flush) /* write directly if requested */ if (state->direct) { while (strm->avail_in) { - got = write(state->fd, strm->next_in, strm->avail_in); - if (got < 0) { + put = strm->avail_in > max ? max : strm->avail_in; + writ = write(state->fd, strm->next_in, put); + if (writ < 0) { gz_error(state, Z_ERRNO, zstrerror()); return -1; } - strm->avail_in -= (unsigned)got; - strm->next_in += got; + strm->avail_in -= (unsigned)writ; + strm->next_in += writ; } return 0; } @@ -105,13 +105,14 @@ local int gz_comp(state, flush) if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && (flush != Z_FINISH || ret == Z_STREAM_END))) { while (strm->next_out > state->x.next) { - got = write(state->fd, state->x.next, - (unsigned long)(strm->next_out - state->x.next)); - if (got < 0) { + put = strm->next_out - state->x.next > (int)max ? max : + (unsigned)(strm->next_out - state->x.next); + writ = write(state->fd, state->x.next, put); + if (writ < 0) { gz_error(state, Z_ERRNO, zstrerror()); return -1; } - state->x.next += got; + state->x.next += writ; } if (strm->avail_out == 0) { strm->avail_out = state->size; diff --git a/zconf.h b/zconf.h index f6481d1..dc7209a 100644 --- a/zconf.h +++ b/zconf.h @@ -237,17 +237,6 @@ # include typedef size_t z_size_t; # endif -# ifdef NO_SSIZE_T - typedef NO_SSIZE_T z_ssize_t; -# else -# include -# include -# ifdef _MSC_VER - typedef intptr_t z_ssize_t; -# else - typedef ssize_t z_ssize_t; -# endif -# endif # undef z_longlong #endif diff --git a/zconf.h.cmakein b/zconf.h.cmakein index 843aeb4..31619f3 100644 --- a/zconf.h.cmakein +++ b/zconf.h.cmakein @@ -239,17 +239,6 @@ # include typedef size_t z_size_t; # endif -# ifdef NO_SSIZE_T - typedef NO_SSIZE_T z_ssize_t; -# else -# include -# include -# ifdef _MSC_VER - typedef intptr_t z_ssize_t; -# else - typedef ssize_t z_ssize_t; -# endif -# endif # undef z_longlong #endif diff --git a/zconf.h.in b/zconf.h.in index f6481d1..dc7209a 100644 --- a/zconf.h.in +++ b/zconf.h.in @@ -237,17 +237,6 @@ # include typedef size_t z_size_t; # endif -# ifdef NO_SSIZE_T - typedef NO_SSIZE_T z_ssize_t; -# else -# include -# include -# ifdef _MSC_VER - typedef intptr_t z_ssize_t; -# else - typedef ssize_t z_ssize_t; -# endif -# endif # undef z_longlong #endif