From nobody Thu Nov 19 07:24:04 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jVVMS4kQDz71ylV for ; Thu, 19 Nov 2026 07:24:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jVVMS47sLz4Qgj for ; Thu, 19 Nov 2026 07:24:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795073044; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UauWZR3bGu/DIlyePpsZQ40F/UMb42zIt5yD4zvDdJw=; b=dIu7bf6L/nwECgRcii4fkAx68f0uJLqK8LWSvPboe6TZWixmIRJJk0hdxFnxca2P3DVN0o Mrnrv/OicPsh94Yt5BOm7vLXmVq8CR+3nLCUfArS1NI9FCXdh5akmdas5kXnTT3byJTmIP SjJ7y/6PUqruacaBBqHf2JTb60+zl4V/B6+iCZlcHvtMkM9F/LGwsIQPIr+G/dQImN6iDo OPzHrfA1M9c4PlS+I+fgxctvWUYf7lM0wHkVSKXzXDguUETZqWAlUvhVBV5WC31u1vu1sS 7ji5CXGYMZaVWNAFDLxWLtyczRq6HPDdjheTIcYu3dxyhRkSgJV3H44k55BYzw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795073044; b=qrla3PamoEgesDL6VDF6n1OM5AXXhlqw/6c3FI1I63dJM6hDIqqJf0TXV1ppX7ggZkmT02 6cK6ADmYpNYm+FyPQPPbAp0Jn1upeemMfcBRmU2kbwu926kEi9A8m5haVKr5G/Bx6ycMr9 emGoqRijqeO1Zt2uNQfo8LtrcVmX5QpJhH1b1THYwXblNP1hDIThByfcIdHJwqifcbwjbB 0l871+wYvfseFeI0KGYPUffTQCrz0e3pT3Tyzzg91tQjFIOQSXG+f45kvyta1sU36vb8Lg iWx6bm09iQicU0j5MfllcXBSvQ8HNMPMm848MC0JXFloAv/IyrWDyWv5TjNo1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795073044; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UauWZR3bGu/DIlyePpsZQ40F/UMb42zIt5yD4zvDdJw=; b=GjMS/y499bg16xCZJXKRFX5XSkdMoYu7cPNWnCFoWkqzAMqrJviYcwR9iUma/MBMTz5j0c q8bvwLN0ONlvi1Rrd6G9WnYw9qLE+AbGJTqhaSzcbDY+s3jEEHoWbcIOJctgBIZrVHq2Ng Kze7uU5+urxCntqDPHweR0P4idmv0CRO6hf790MmMxBLnDyHHAsiG0gphAb1/NL5y23x4T ArlUO0FLNIO8yOYO7SH/30fRW8w6mt1bF2JZH1Nt8aWCb0VGReeRMBTBOcMHOIzDm8nJ4i 6lgK+kvv7ufGQp6NbP4ZuGU09NFCdOi5XuKKjWrBdWBy12TI0a0o4drhltMesA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jVVMS3pqFzqwD for ; Thu, 19 Nov 2026 07:24:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 20bd7 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 19 Nov 2026 07:24:04 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Xin LI Subject: git: a0e978291736 - stable/15 - MFC: MFV: zlib 1.4 List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: delphij X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: a0e97829173660733f28a510ea35dbc7aa3d766e Auto-Submitted: auto-generated Date: Thu, 19 Nov 2026 07:24:04 +0000 Message-Id: <6afea414.20bd7.16e06266@gitrepo.freebsd.org> The branch stable/15 has been updated by delphij: URL: https://cgit.FreeBSD.org/src/commit/?id=a0e97829173660733f28a510ea35dbc7aa3d766e commit a0e97829173660733f28a510ea35dbc7aa3d766e Author: Xin LI AuthorDate: 2026-11-08 19:10:01 +0000 Commit: Xin LI CommitDate: 2026-11-19 07:23:13 +0000 MFC: MFV: zlib 1.4 (cherry picked from commit 724bb39e83e369209d8e42a52370684b82e26350) --- sys/contrib/zlib/ChangeLog | 31 +++++++++ sys/contrib/zlib/README | 4 +- sys/contrib/zlib/compress.c | 4 +- sys/contrib/zlib/crc32.c | 2 +- sys/contrib/zlib/deflate.c | 104 +++++++++++++++++++---------- sys/contrib/zlib/gzguts.h | 29 +++++++- sys/contrib/zlib/gzlib.c | 74 ++++++++++---------- sys/contrib/zlib/gzread.c | 141 +++++++++++++++++++++++++++------------ sys/contrib/zlib/gzwrite.c | 48 ++++++++++--- sys/contrib/zlib/infback.c | 2 - sys/contrib/zlib/inflate.c | 56 ++++++++++++---- sys/contrib/zlib/inflate.h | 4 +- sys/contrib/zlib/inftrees.c | 4 +- sys/contrib/zlib/inftrees.h | 10 ++- sys/contrib/zlib/test/infcover.c | 5 +- sys/contrib/zlib/test/minigzip.c | 9 ++- sys/contrib/zlib/trees.c | 2 +- sys/contrib/zlib/uncompr.c | 4 +- sys/contrib/zlib/zconf.h | 11 ++- sys/contrib/zlib/zconf.h.in | 11 ++- sys/contrib/zlib/zlib.3 | 4 +- sys/contrib/zlib/zlib.h | 79 ++++++++++++---------- sys/contrib/zlib/zutil.c | 7 +- 23 files changed, 450 insertions(+), 195 deletions(-) diff --git a/sys/contrib/zlib/ChangeLog b/sys/contrib/zlib/ChangeLog index 312753edadef..228a03afa1d7 100644 --- a/sys/contrib/zlib/ChangeLog +++ b/sys/contrib/zlib/ChangeLog @@ -1,6 +1,37 @@ ChangeLog file for zlib +Changes in 1.4 (4 Nov 2026) +- Change CMake library names on MSVC +- Return z_off_t default to long for portability +- Fail gzopen() with "x" if O_EXCL not available +- Deliver all gzip data before reporting error +- Fix gzwrite() buffer overflow (CVE-2026-85091) +- Fix inflateGetDictionary() write past buffer on invalid window size +- Fix window write overrun after inflateSync() +- Fix read before provided data after repeated inflatePrime()'s +- Fix write past pending buffer after repeated deflatePrime()'s +- Fix copy of uninitialized memory by inflateBack() on invalid data +- Fix copy of uninitialized memory after gzseek() as first write action +- Fix read outside window after a deflateResetKeep() mid-stream +- Avoid out-of-bounds pointer arithmetic in infback9() (CVE-2026-4738) +- Fix several vulnerabilities in contrib/minizip +- Fix truncated stream detection in gzread() +- Fix pending skip handling in gzread() +- Fix loss of zero fill on repeated gzseek()'s when writing +- Fix inaccurate total_in after Z_NEED_DICT returned +- Detect and report bad deflateSetDictionary() calls +- Avoid undefined behaviors in C language usage +- Fix minizip extra field overflows when almost full and need Zip64 +- Don't assume NULL is defined in compress.c and uncompr.c +- Use the generated zconf.h when building test programs out of tree +- Improve portability and functionality of CMake builds +- Provide 64-bit file offsets on Windows with #defines as on Linux +- Improve portability to Android, AIX, PowerPC, s390x, and Windows +- Use a PCG random number generator instead of rand() in minizip +- Clarify documentation +- (See https://github.com/madler/zlib for all 195 commits) + Changes in 1.3.2 (17 Feb 2026) - Continued rewrite of CMake build [Vollstrecker] - Various portability improvements diff --git a/sys/contrib/zlib/README b/sys/contrib/zlib/README index 2b1e6f36fe3e..265c8b47d316 100644 --- a/sys/contrib/zlib/README +++ b/sys/contrib/zlib/README @@ -1,6 +1,6 @@ ZLIB DATA COMPRESSION LIBRARY -zlib 1.3.2 is a general purpose data compression library. All the code is +zlib 1.4 is a general purpose data compression library. All the code is thread safe (though see the FAQ for caveats). The data format used by the zlib library is described by RFCs (Request for Comments) 1950 to 1952 at https://datatracker.ietf.org/doc/html/rfc1950 (zlib format), rfc1951 (deflate @@ -31,7 +31,7 @@ Mark Nelson wrote an article about zlib for the Jan. 1997 issue of Dr. Dobb's Journal; a copy of the article is available at https://zlib.net/nelson/ . -The changes made in version 1.3.2 are documented in the file ChangeLog. +The changes made in version 1.4 are documented in the file ChangeLog. Unsupported third party contributions are provided in directory contrib/ . diff --git a/sys/contrib/zlib/compress.c b/sys/contrib/zlib/compress.c index bd74b9488eb8..2e967145b799 100644 --- a/sys/contrib/zlib/compress.c +++ b/sys/contrib/zlib/compress.c @@ -28,8 +28,8 @@ int ZEXPORT compress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source, const uInt max = (uInt)-1; z_size_t left; - if ((sourceLen > 0 && source == NULL) || - destLen == NULL || (*destLen > 0 && dest == NULL)) + if ((sourceLen > 0 && source == Z_NULL) || + destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) return Z_STREAM_ERROR; left = *destLen; diff --git a/sys/contrib/zlib/crc32.c b/sys/contrib/zlib/crc32.c index d9ade5150239..32b1d0457886 100644 --- a/sys/contrib/zlib/crc32.c +++ b/sys/contrib/zlib/crc32.c @@ -686,7 +686,7 @@ uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { #endif /* Initialize the CRC for each braid. */ - crc0 = crc; + crc0 = (z_crc_t)crc; #if N > 1 crc1 = 0; #if N > 2 diff --git a/sys/contrib/zlib/deflate.c b/sys/contrib/zlib/deflate.c index e782ad519721..0dc8308b70f9 100644 --- a/sys/contrib/zlib/deflate.c +++ b/sys/contrib/zlib/deflate.c @@ -52,7 +52,7 @@ #include "deflate.h" const char deflate_copyright[] = - " deflate 1.3.2 Copyright 1995-2026 Jean-loup Gailly and Mark Adler "; + " deflate 1.4 Copyright 1995-2026 Jean-loup Gailly and Mark Adler "; /* If you use the zlib library in a product, an acknowledgment is welcome in the documentation of your product. If for some reason you cannot @@ -445,7 +445,6 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, s->status = INIT_STATE; /* to pass state test in deflateReset() */ s->wrap = wrap; - s->gzhead = Z_NULL; s->w_bits = (uInt)windowBits; s->w_size = 1 << s->w_bits; s->w_mask = s->w_size - 1; @@ -568,7 +567,8 @@ int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, return Z_STREAM_ERROR; s = strm->state; wrap = s->wrap; - if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) + if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead || + (wrap == 0 && (long)s->strstart != s->block_start)) return Z_STREAM_ERROR; /* when using zlib wrappers, compute Adler-32 for provided dictionary */ @@ -671,7 +671,20 @@ int ZEXPORT deflateResetKeep(z_streamp strm) { adler32(0L, Z_NULL, 0); s->last_flush = -2; - _tr_init(s); + s->gzhead = Z_NULL; + + { + /* Retain the hash work done for level 0. */ + uInt matches = s->level == 0 ? s->matches : 0; + _tr_init(s); + s->matches = matches; + } + + /* Keep the window and the hash, but drop what was left unfinished */ + s->block_start = (long)s->strstart; + s->lookahead = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; return Z_OK; } @@ -683,6 +696,7 @@ local void lm_init(deflate_state *s) { s->window_size = (ulg)2L*s->w_size; CLEAR_HASH(s); + s->matches = 0; /* the hash is clear */ /* Set the default configuration parameters: */ @@ -745,18 +759,23 @@ int ZEXPORT deflateUsed(z_streamp strm, int *bits) { int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) { deflate_state *s; int put; + int need; if (deflateStateCheck(strm)) return Z_STREAM_ERROR; s = strm->state; -#ifdef LIT_MEM - if (bits < 0 || bits > 16 || - (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3)) + if (bits < 0 || bits > 16) return Z_BUF_ERROR; -#else - if (bits < 0 || bits > 16 || - s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3)) + if (s->pending_out != s->pending_buf) { + /* move partially flushed pending output to buffer start */ + Bytef *to = s->pending_buf, *from = s->pending_out; + ulg n = s->pending; + while (n--) + *to++ = *from++; + s->pending_out = s->pending_buf; + } + need = (s->bi_valid + bits) >> 3; + if (s->pending_buf_size - s->pending < (ulg)need) return Z_BUF_ERROR; -#endif do { put = Buf_size - s->bi_valid; if (put > bits) @@ -1316,39 +1335,49 @@ int ZEXPORT deflateEnd(z_streamp strm) { */ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) { #ifdef MAXSEG_64K - (void)dest; (void)source; + if (dest != Z_NULL) + dest->state = Z_NULL; return Z_STREAM_ERROR; #else deflate_state *ds; deflate_state *ss; - - if (deflateStateCheck(source) || dest == Z_NULL) { + if (dest == Z_NULL) + return Z_STREAM_ERROR; + if (deflateStateCheck(source)) { + dest->state = Z_NULL; return Z_STREAM_ERROR; } ss = source->state; - zmemcpy(dest, source, sizeof(z_stream)); - - ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); - if (ds == Z_NULL) return Z_MEM_ERROR; - zmemzero(ds, sizeof(deflate_state)); - dest->state = (struct internal_state FAR *) ds; + ds = (deflate_state *) ZALLOC(source, 1, sizeof(deflate_state)); + if (ds == Z_NULL) { + dest->state = Z_NULL; + return Z_MEM_ERROR; + } zmemcpy(ds, ss, sizeof(deflate_state)); - ds->strm = dest; - - ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); - ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); - ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); - ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS); - + ds->window = (Bytef *) ZALLOC(source, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(source, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(source, ds->hash_size, sizeof(Pos)); + ds->pending_buf = (uchf *) ZALLOC(source, ds->lit_bufsize, LIT_BUFS); if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ds->pending_buf == Z_NULL) { - deflateEnd (dest); + TRY_FREE(source, ds->pending_buf); + TRY_FREE(source, ds->head); + TRY_FREE(source, ds->prev); + TRY_FREE(source, ds->window); + ZFREE(source, ds); + dest->state = Z_NULL; return Z_MEM_ERROR; } + + /* copy the stream and attach the new state */ + zmemcpy(dest, source, sizeof(z_stream)); + dest->state = (struct internal_state FAR *) ds; + ds->strm = dest; + /* following zmemcpy's do not work for 16-bit MSDOS */ zmemcpy(ds->window, ss->window, ss->high_water); zmemcpy(ds->prev, ss->prev, @@ -1641,7 +1670,10 @@ local void check_match(deflate_state *s, IPos start, IPos match, int length) { /* Same but force premature exit if necessary. */ #define FLUSH_BLOCK(s, last) { \ FLUSH_BLOCK_ONLY(s, last); \ - if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ + if (s->strm->avail_out == 0) { \ + if (!(last)) return need_more; \ + if (s->pending != 0 || s->wrap != 0) return finish_started; \ + } \ } /* Maximum stored block length in deflate format (not including header). */ @@ -1678,7 +1710,7 @@ local block_state deflate_stored(deflate_state *s, int flush) { * possible. If flushing, copy the remaining available input to next_out as * stored blocks, if there is enough space. */ - int last = 0; + int last = 0, all; unsigned len, left, have; unsigned used = s->strm->avail_in; do { @@ -1693,11 +1725,14 @@ local block_state deflate_stored(deflate_state *s, int flush) { /* maximum stored block length that will fit in avail_out: */ have = s->strm->avail_out - have; left = (unsigned)(s->strstart - s->block_start); /* window bytes */ - if (len > (ulg)left + s->strm->avail_in) + if (left < len && s->strm->avail_in < len - left) len = left + s->strm->avail_in; /* limit len to the input */ if (len > have) len = have; /* limit len to the output */ + /* all is true if len is all of the available input */ + all = len >= left && len - left == s->strm->avail_in; + /* If the stored block would be less than min_block in length, or if * unable to copy all of the available input when flushing, then try * copying to the window and the pending buffer instead. Also don't @@ -1705,13 +1740,13 @@ local block_state deflate_stored(deflate_state *s, int flush) { */ if (len < min_block && ((len == 0 && flush != Z_FINISH) || flush == Z_NO_FLUSH || - len != left + s->strm->avail_in)) + !all)) break; /* Make a dummy stored block in pending to get the header bytes, * including any pending bits. This also updates the debugging counts. */ - last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; + last = flush == Z_FINISH && all ? 1 : 0; _tr_stored_block(s, (char *)0, 0L, last); /* Replace the lengths in the dummy stored block with len. */ @@ -2016,7 +2051,8 @@ local block_state deflate_slow(deflate_state *s, int flush) { uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; /* Do not insert strings in hash table beyond this. */ - check_match(s, s->strstart - 1, s->prev_match, (int)s->prev_length); + check_match(s, s->strstart - 1, s->prev_match, + (int)s->prev_length); _tr_tally_dist(s, s->strstart - 1 - s->prev_match, s->prev_length - MIN_MATCH, bflush); diff --git a/sys/contrib/zlib/gzguts.h b/sys/contrib/zlib/gzguts.h index 266305debc8b..df4b0d6707e7 100644 --- a/sys/contrib/zlib/gzguts.h +++ b/sys/contrib/zlib/gzguts.h @@ -92,8 +92,10 @@ # define vsnprintf _vsnprintf # endif # endif -# elif !defined(__STDC_VERSION__) || __STDC_VERSION__-0 < 199901L -/* Otherwise if C89/90, assume no C99 snprintf() or vsnprintf() */ +# elif (!defined(__STDC_VERSION__) || __STDC_VERSION__-0 < 199901L) && \ + (!defined(__cplusplus) || __cplusplus < 201103L) +/* Otherwise if C89/90 (and not C++11 or later), assume no C99 snprintf() or + vsnprintf() */ # ifndef NO_snprintf # define NO_snprintf # endif @@ -128,14 +130,28 @@ # include # define zstrerror() gz_strwinerror((DWORD)GetLastError()) #else +# include # ifndef NO_STRERROR -# include # define zstrerror() strerror(errno) # else # define zstrerror() "stdio error (consult errno)" # endif #endif +#ifdef EAGAIN +# ifdef EWOULDBLOCK +# define BLOCKED(e) ((e) == EAGAIN || (e) == EWOULDBLOCK) +# else +# define BLOCKED(e) ((e) == EAGAIN) +# endif +#else +# ifdef EWOULDBLOCK +# define BLOCKED(e) ((e) == EWOULDBLOCK) +# else +# define BLOCKED(e) (0) +# endif +#endif + /* provide prototypes for these when building zlib without LFS */ #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); @@ -214,3 +230,10 @@ char ZLIB_INTERNAL *gz_strwinerror(DWORD error); (possible z_off64_t types off_t, off64_t, and long are all signed) */ unsigned ZLIB_INTERNAL gz_intmax(void); #define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) + +/* largest value representable in z_off64_t */ +#ifndef CHAR_BIT +# define CHAR_BIT 8 +#endif +#define GZ_OFF_MAX \ + ((((z_off64_t)1 << (sizeof(z_off64_t) * CHAR_BIT - 2)) - 1) * 2 + 1) diff --git a/sys/contrib/zlib/gzlib.c b/sys/contrib/zlib/gzlib.c index d81feeac78cf..c775d9044eff 100644 --- a/sys/contrib/zlib/gzlib.c +++ b/sys/contrib/zlib/gzlib.c @@ -70,9 +70,9 @@ char ZLIB_INTERNAL *gz_strwinerror(DWORD error) { /* Reset gzip file state */ local void gz_reset(gz_statep state) { state->x.have = 0; /* no output data available */ + state->past = 0; /* have not read past end yet */ if (state->mode == GZ_READ) { /* for reading ... */ state->eof = 0; /* not at end of file */ - state->past = 0; /* have not read past end yet */ state->how = LOOK; /* look for gzip header */ state->junk = -1; /* mark first member */ } @@ -90,9 +90,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { gz_statep state; z_size_t len; int oflag = 0; -#ifdef O_EXCL int exclusive = 0; -#endif /* check input */ if (path == NULL || mode == NULL) @@ -138,11 +136,9 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { oflag |= O_CLOEXEC; break; #endif -#ifdef O_EXCL case 'x': exclusive = 1; break; -#endif case 'f': state->strategy = Z_FILTERED; break; @@ -198,10 +194,21 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { /* if reading, direct == 1 for auto-detect, -1 for gzip only; if writing or appending, direct == 0 for gzip, 1 for transparent (copy in to out) */ +#ifndef O_EXCL + if (exclusive && state->mode != GZ_READ && fd < 0) { + /* exclusive creation request can't be honored -- fail out */ + free(state); + return NULL; + } +#endif + /* save the path name for error messages */ #ifdef WIDECHAR - if (fd == -2) + if (fd == -2) { len = wcstombs(NULL, path, 0); + if (len == (z_size_t)-1) + len = 0; /* not representable in the current locale */ + } else #endif len = strlen((const char *)path); @@ -212,9 +219,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { } #ifdef WIDECHAR if (fd == -2) { - if (len) - wcstombs(state->path, path, len + 1); - else + if (len == 0 || wcstombs(state->path, path, len + 1) != len) *(state->path) = 0; } else @@ -223,7 +228,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { #if !defined(NO_snprintf) && !defined(NO_vsnprintf) (void)snprintf(state->path, len + 1, "%s", (const char *)path); #else - strcpy(state->path, path); + strcpy(state->path, (const char *)path); #endif } @@ -257,9 +262,9 @@ local gzFile gz_open(const void *path, int fd, const char *mode) { if (oflag & O_NONBLOCK) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); #endif -#ifdef O_CLOEXEC +#if defined(O_CLOEXEC) && defined(FD_CLOEXEC) if (oflag & O_CLOEXEC) - fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | O_CLOEXEC); + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); #endif state->fd = fd; } @@ -301,7 +306,7 @@ gzFile ZEXPORT gzdopen(int fd, const char *mode) { char *path; /* identifier for error messages */ gzFile gz; - if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) + if (fd < 0 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) return NULL; #if !defined(NO_snprintf) && !defined(NO_vsnprintf) (void)snprintf(path, 7 + 3 * sizeof(int), "", fd); @@ -368,7 +373,7 @@ int ZEXPORT gzrewind(gzFile file) { /* -- see zlib.h -- */ z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) { unsigned n; - z_off64_t ret; + z_off64_t ret, pos; gz_statep state; /* get internal structure and check integrity */ @@ -386,18 +391,19 @@ z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) { if (whence != SEEK_SET && whence != SEEK_CUR) return -1; - /* normalize offset to a SEEK_CUR specification */ - if (whence == SEEK_SET) - offset -= state->x.pos; - else { - offset += state->past ? 0 : state->skip; - state->skip = 0; - } + /* compute the absolute position */ + pos = whence == SEEK_SET ? 0 : + state->x.pos + (state->past ? 0 : state->skip); + if (offset > GZ_OFF_MAX - pos) + return -1; /* would overflow z_off64_t */ + if (offset < -pos) + return -1; /* before start of file */ + pos += offset; /* if within raw area while reading, just go there */ - if (state->mode == GZ_READ && state->how == COPY && - state->x.pos + offset >= 0) { - ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR); + if (state->mode == GZ_READ && state->how == COPY) { + ret = LSEEK(state->fd, pos - state->x.pos - (z_off64_t)state->x.have, + SEEK_CUR); if (ret == -1) return -1; state->x.have = 0; @@ -406,20 +412,18 @@ z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) { state->skip = 0; gz_error(state, Z_OK, NULL); state->strm.avail_in = 0; - state->x.pos += offset; - return state->x.pos; + state->x.pos = pos; + return pos; } - /* calculate skip amount, rewinding if needed for back seek when reading */ - if (offset < 0) { + /* rewind if needed for back seek when reading */ + if (pos < state->x.pos) { if (state->mode != GZ_READ) /* writing -- can't go backwards */ return -1; - offset += state->x.pos; - if (offset < 0) /* before start of file! */ - return -1; - if (gzrewind(file) == -1) /* rewind, then skip to offset */ + if (gzrewind(file) == -1) /* rewind, then skip to pos */ return -1; } + offset = pos - state->x.pos; /* if reading, skip what's in output buffer (one less gzgetc() check) */ if (state->mode == GZ_READ) { @@ -433,7 +437,7 @@ z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) { /* request skip (if not zero) */ state->skip = offset; - return state->x.pos + offset; + return pos; } /* -- see zlib.h -- */ @@ -562,10 +566,6 @@ void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) { state->msg = NULL; } - /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ - if (err != Z_OK && err != Z_BUF_ERROR && !state->again) - state->x.have = 0; - /* set error code, and if no message, then done */ state->err = err; if (msg == NULL) diff --git a/sys/contrib/zlib/gzread.c b/sys/contrib/zlib/gzread.c index 26a9b7ee3e56..f616dc7084e0 100644 --- a/sys/contrib/zlib/gzread.c +++ b/sys/contrib/zlib/gzread.c @@ -34,7 +34,7 @@ local int gz_load(gz_statep state, unsigned char *buf, unsigned len, *have += (unsigned)ret; } while (*have < len); if (ret < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (BLOCKED(errno)) { state->again = 1; if (*have != 0) return 0; @@ -126,6 +126,13 @@ local int gz_look(gz_statep state) { if we're looking for a gzip member after the first one, which is not at the start, then proceed directly to look for a gzip member next */ if (state->direct == -1 || state->junk == 0) { + /* wait for input before committing to another gzip member */ + if (state->junk == 0 && strm->avail_in == 0) { + if (gz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) + return 0; + } inflateReset(strm); state->how = GZIP; state->junk = state->junk != -1; @@ -154,7 +161,7 @@ local int gz_look(gz_statep state) { strm->next_in[2] == 8 && strm->next_in[3] < 32) { inflateReset(strm); state->how = GZIP; - state->junk = 1; + state->junk = 0; state->direct = 0; return 0; } @@ -249,6 +256,11 @@ local int gz_decomp(gz_statep state) { local int gz_fetch(gz_statep state) { z_streamp strm = &(state->strm); + /* after a serious error, only what is already in the output buffer is + delivered -- don't try to decompress any more */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + return -1; + do { switch(state->how) { case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ @@ -277,32 +289,46 @@ local int gz_fetch(gz_statep state) { return 0; } +/* Apply as much of a pending skip request as the output already available in + state->x allows. */ +local void gz_skip_have(gz_statep state) { + unsigned n; + + n = GT_OFF(state->x.have) || + (z_off64_t)state->x.have > state->skip ? + (unsigned)state->skip : state->x.have; + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + state->skip -= n; +} + /* Skip state->skip (> 0) uncompressed bytes of output. Return -1 on error, 0 on success. */ local int gz_skip(gz_statep state) { - unsigned n; - /* skip over len bytes or reach end-of-file, whichever comes first */ do { /* skip over whatever is in output buffer */ - if (state->x.have) { - n = GT_OFF(state->x.have) || - (z_off64_t)state->x.have > state->skip ? - (unsigned)state->skip : state->x.have; - state->x.have -= n; - state->x.next += n; - state->x.pos += n; - state->skip -= n; - } + if (state->x.have) + gz_skip_have(state); /* output buffer empty -- return if we're at the end of the input */ - else if (state->eof && state->strm.avail_in == 0) + else if (state->eof && state->strm.avail_in == 0) { + if (state->how == GZIP && state->err == Z_OK) + gz_error(state, Z_BUF_ERROR, "unexpected end of file"); + state->skip = 0; + state->past = 1; break; + } /* need more data to skip -- load up output buffer */ else { - /* get more output, looking for header if required */ - if (gz_fetch(state) == -1) + /* get more output, looking for header if required. If the fetch + stalled but provided some output, consume it before returning + so that x.have > 0 always implies the data is at the current + logical position (x.pos), as assumed by the gzgetc() fast path + in zlib.h and gzread.c */ + if (gz_fetch(state) == -1 && state->x.have == 0) return -1; } } while (state->skip); @@ -350,8 +376,11 @@ local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len) { } /* output buffer empty -- return if we're at the end of the input */ - else if (state->eof && state->strm.avail_in == 0) + else if (state->eof && state->strm.avail_in == 0) { + if (state->how == GZIP && state->err == Z_OK) + gz_error(state, Z_BUF_ERROR, "unexpected end of file"); break; + } /* need output data -- for small len or new stream load up our output buffer, so that gzgetc() can be fast */ @@ -404,15 +433,21 @@ int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { if (state->mode != GZ_READ) return -1; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return -1; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* since an int is returned, make sure len fits in one, otherwise return - with an error (this avoids a flaw in the interface) */ + with an error (this avoids a flaw in the interface) -- keep a pending + serious error, which is the one the caller needs to see */ if ((int)len < 0) { - gz_error(state, Z_STREAM_ERROR, "request does not fit in an int"); + if (state->err == Z_OK || state->err == Z_BUF_ERROR) + gz_error(state, Z_STREAM_ERROR, "request does not fit in an int"); return -1; } @@ -449,15 +484,22 @@ z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems, if (state->mode != GZ_READ) return 0; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return 0; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); - /* compute bytes to read -- error on overflow */ + /* compute bytes to read -- error on overflow, keeping a pending serious + error, which is the one the caller needs to see */ len = nitems * size; if (size && len / size != nitems) { - gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); + if (state->err == Z_OK || state->err == Z_BUF_ERROR) + gz_error(state, Z_STREAM_ERROR, + "request does not fit in a size_t"); return 0; } @@ -482,10 +524,14 @@ int ZEXPORT gzgetc(gzFile file) { if (state->mode != GZ_READ) return -1; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error, as the gzgetc() macro does */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return -1; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* try output buffer (no need to check for skip request) */ if (state->x.have) { @@ -517,10 +563,15 @@ int ZEXPORT gzungetc(int c, gzFile file) { if (state->how == LOOK && state->x.have == 0) (void)gz_look(state); - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- a character can still be + pushed back after one, since it is data that was read before the + error, and it will be returned before the error is signaled -- but only + if there is an output buffer to put it in */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->size == 0) return -1; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* process a skip request */ if (state->skip && gz_skip(state) == -1) @@ -542,7 +593,8 @@ int ZEXPORT gzungetc(int c, gzFile file) { /* if no room, give up (must have already done a gzungetc()) */ if (state->x.have == (state->size << 1)) { - gz_error(state, Z_DATA_ERROR, "out of room to push characters"); + if (state->err == Z_OK || state->err == Z_BUF_ERROR) + gz_error(state, Z_DATA_ERROR, "out of room to push characters"); return -1; } @@ -578,10 +630,14 @@ char * ZEXPORT gzgets(gzFile file, char *buf, int len) { if (state->mode != GZ_READ) return NULL; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return NULL; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* process a skip request */ if (state->skip && gz_skip(state) == -1) @@ -593,8 +649,9 @@ char * ZEXPORT gzgets(gzFile file, char *buf, int len) { left = (unsigned)len - 1; if (left) do { /* assure that something is in the output buffer */ - if (state->x.have == 0 && gz_fetch(state) == -1) - break; /* error */ + if (state->x.have == 0 && gz_fetch(state) == -1 && + state->x.have == 0) + break; /* error, and nothing left to return */ if (state->x.have == 0) { /* end of file */ state->past = 1; /* read past end */ break; /* return what we have */ @@ -618,7 +675,7 @@ char * ZEXPORT gzgets(gzFile file, char *buf, int len) { /* append a terminating zero to the string (we don't check for a zero in the contents, let the user worry about that) -- return the terminated string, or if nothing was read, NULL */ - if (buf == str) + if (buf == str && len > 1) return NULL; buf[0] = 0; return str; @@ -635,8 +692,10 @@ int ZEXPORT gzdirect(gzFile file) { /* if the state is not known, but we can find out, then do so (this is mainly for right after a gzopen() or gzdopen()) */ - if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) + if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) { (void)gz_look(state); + gz_skip_have(state); /* apply skip to current buffer */ + } /* return 1 if transparent, 0 if processing a gzip stream */ return state->direct == 1; diff --git a/sys/contrib/zlib/gzwrite.c b/sys/contrib/zlib/gzwrite.c index f2fefb94346c..aa4bfd8b449f 100644 --- a/sys/contrib/zlib/gzwrite.c +++ b/sys/contrib/zlib/gzwrite.c @@ -42,8 +42,8 @@ local int gz_init(gz_statep state) { gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; } - strm->next_in = NULL; } + strm->next_in = NULL; /* mark state as initialized */ state->size = state->want; @@ -80,7 +80,7 @@ local int gz_comp(gz_statep state, int flush) { put = strm->avail_in > max ? max : strm->avail_in; writ = (int)write(state->fd, strm->next_in, put); if (writ < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (BLOCKED(errno)) state->again = 1; gz_error(state, Z_ERRNO, zstrerror()); return -1; @@ -115,7 +115,7 @@ local int gz_comp(gz_statep state, int flush) { (unsigned)(strm->next_out - state->x.next); writ = (int)write(state->fd, state->x.next, put); if (writ < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (BLOCKED(errno)) state->again = 1; gz_error(state, Z_ERRNO, zstrerror()); return -1; @@ -157,6 +157,11 @@ local int gz_zero(gz_statep state) { unsigned n; z_streamp strm = &(state->strm); + /* allocate memory if this is the first time through -- the input buffer + must exist before it is zeroed below */ + if (state->size == 0 && gz_init(state) == -1) + return -1; + /* consume whatever's left in the input buffer */ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) return -1; @@ -176,8 +181,14 @@ local int gz_zero(gz_statep state) { n -= strm->avail_in; state->x.pos += n; state->skip -= n; - if (ret == -1) + if (ret == -1) { + /* discard any unconsumed zero fill -- it was not yet accounted + for in state->skip, so it will be generated again when the + operation is retried */ + strm->avail_in = 0; + strm->next_in = state->in; return -1; + } } while (state->skip); return 0; } @@ -249,6 +260,7 @@ local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) { return state->again ? put - len : 0; } } while (len); + state->strm.next_in = state->in; } /* input was all buffered or compressed */ @@ -375,9 +387,10 @@ int ZEXPORT gzputs(gzFile file, const char *s) { return len && put == 0 ? -1 : (int)put; } -#if (((!defined(STDC) && !defined(Z_HAVE_STDARG_H)) || !defined(NO_vsnprintf)) && \ - (defined(STDC) || defined(Z_HAVE_STDARG_H) || !defined(NO_snprintf))) || \ - defined(ZLIB_INSECURE) +#if (((!defined(STDC) && !defined(Z_HAVE_STDARG_H)) || \ + !defined(NO_vsnprintf)) && \ + (defined(STDC) || defined(Z_HAVE_STDARG_H) || \ + !defined(NO_snprintf))) || defined(ZLIB_INSECURE) /* If the second half of the input buffer is occupied, write out the contents. If there is any input remaining due to a non-blocking stall on write, move it to the start of the buffer. Return true if this did not open up the @@ -387,7 +400,8 @@ local int gz_vacate(gz_statep state) { z_streamp strm; strm = &(state->strm); - if (strm->next_in + strm->avail_in <= state->in + state->size) + if (strm->next_in == NULL || + strm->next_in + strm->avail_in <= state->in + state->size) return 0; (void)gz_comp(state, Z_NO_FLUSH); if (strm->avail_in == 0) { @@ -447,6 +461,7 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { a Z_BUF_ERROR to let the application know that this gzprintf() needs to be retried. */ gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf"); + return state->err; } if (!state->again) return state->err; @@ -482,6 +497,14 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { /* write out buffer if more than half is occupied */ ret = gz_vacate(state); + if (ret) { + /* discard unwritten formatted output */ + strm->avail_in -= (unsigned)len; + state->x.pos -= len; + if (state->again) + gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf"); + return state->err; + } if (state->err && !state->again) return state->err; return len; *** 634 LINES SKIPPED ***