[PATCH 18/21] lib/zlib: update deflate to zlib 1.2.2.2 release

From: Greg Kroah-Hartman

Date: Thu Dec 03 2026 - 02:32:18 EST


Sync the kernel deflate code with the zlib 1.2.2.2 release.

This involves:
- add the Z_FIXED strategy, which prevents the use of dynamic Huffman
codes so decoding can start earlier with a simpler decoder:
_tr_flush_block() picks the static trees unconditionally for it, and
the match finders treat it like the default strategy
- rewrite the strategy dispatch in deflate_fast()/deflate_slow() as
explicit != Z_HUFFMAN_ONLY && != Z_RLE tests so Z_FIXED (numerically
above Z_RLE) uses longest_match()
- expand the longest_match() comment to document the intentional reads
of uninitialized window bytes beyond the lookahead
- emit OS_CODE instead of 255 in the (compiled-out) gzip header path

There was one change that was not relevant to the kernel copy:
- deflateCopy()'s zmemcpy conversion is not needed

The kernel is now in sync with zlib commit 0484693e1723 ("zlib 1.2.2.2").

Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
include/linux/zlib.h | 3 ++-
lib/zlib_deflate/deflate.c | 15 ++++++++++-----
lib/zlib_deflate/deftree.c | 2 +-
3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/include/linux/zlib.h b/include/linux/zlib.h
index 9d203a8f419e..95d90b51970c 100644
--- a/include/linux/zlib.h
+++ b/include/linux/zlib.h
@@ -32,7 +32,7 @@

#include <linux/zconf.h>

-/* zlib deflate based on ZLIB_VERSION "1.2.2.1" */
+/* zlib deflate based on ZLIB_VERSION "1.2.2.2" */
/* zlib inflate based on ZLIB_VERSION "1.2.3" */

/*
@@ -167,6 +167,7 @@ typedef z_stream *z_streamp;
#define Z_FILTERED 1
#define Z_HUFFMAN_ONLY 2
#define Z_RLE 3
+#define Z_FIXED 4
#define Z_DEFAULT_STRATEGY 0
/* compression strategy; see deflateInit2() below for details */

diff --git a/lib/zlib_deflate/deflate.c b/lib/zlib_deflate/deflate.c
index d6d29f63b572..f7ff13a15434 100644
--- a/lib/zlib_deflate/deflate.c
+++ b/lib/zlib_deflate/deflate.c
@@ -222,7 +222,7 @@ int zlib_deflateInit2(
#endif
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
- strategy < 0 || strategy > Z_RLE) {
+ strategy < 0 || strategy > Z_FIXED) {
return Z_STREAM_ERROR;
}
if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
@@ -374,7 +374,7 @@ int zlib_deflate(
put_byte(s, s->level == 9 ? 2 :
(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
4 : 0));
- put_byte(s, 255);
+ put_byte(s, OS_CODE);
s->status = BUSY_STATE;
strm->adler = crc32(0L, NULL, 0);
}
@@ -664,7 +664,12 @@ static uInt longest_match(
match = s->window + cur_match;

/* Skip to next match if the match length cannot increase
- * or if the match length is less than 2:
+ * or if the match length is less than 2. Note that the checks below
+ * for insufficient lookahead only occur occasionally for performance
+ * reasons. Therefore uninitialized memory will be accessed, and
+ * conditional jumps will be made that depend on those values.
+ * However the length of the match is limited to the lookahead, so
+ * the output of deflate is not affected by the uninitialized values.
*/
#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
/* This code assumes sizeof(unsigned short) == 2. Do not use
@@ -1065,7 +1070,7 @@ static block_state deflate_fast(
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
- if (s->strategy < Z_HUFFMAN_ONLY) {
+ if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
s->match_length = longest_match (s, hash_head);
} else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
s->match_length = longest_match_fast (s, hash_head);
@@ -1165,7 +1170,7 @@ static block_state deflate_slow(
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
- if (s->strategy < Z_HUFFMAN_ONLY) {
+ if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
s->match_length = longest_match (s, hash_head);
} else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
s->match_length = longest_match_fast (s, hash_head);
diff --git a/lib/zlib_deflate/deftree.c b/lib/zlib_deflate/deftree.c
index baa8d37fd094..b98feac2d498 100644
--- a/lib/zlib_deflate/deftree.c
+++ b/lib/zlib_deflate/deftree.c
@@ -872,7 +872,7 @@ void zlib_tr_flush_block(
#ifdef FORCE_STATIC
} else if (static_lenb >= 0) { /* force static trees */
#else
- } else if (static_lenb == opt_lenb) {
+ } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
#endif
send_bits(s, (STATIC_TREES<<1)+eof, 3);
compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);

--
2.56.1