]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Fix switchlevels calling deflateParams() with a stale buffer
authorIlya Leoshkevich <iii@linux.ibm.com>
Mon, 28 Sep 2020 19:54:54 +0000 (21:54 +0200)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 9 Oct 2020 09:37:53 +0000 (11:37 +0200)
Fixes: #776
test/switchlevels.c

index 11635755e8bf10a2e23c3bfccc58c84df8653783..0f850113e27d769fd43ed70d7c9c64de31149faa 100644 (file)
@@ -66,12 +66,6 @@ static int compress_chunk(PREFIX3(stream) *strm, int level, int size, int last)
         goto done;
     }
 
-    err = PREFIX(deflateParams)(strm, level, Z_DEFAULT_STRATEGY);
-    if (err != Z_OK) {
-        fprintf(stderr, "deflateParams() failed with code %d\n", err);
-        goto done;
-    }
-
     compsize = 100 + 2 * PREFIX(deflateBound)(strm, size);
     buf = malloc(size + compsize);
     if (buf == NULL) {
@@ -82,11 +76,21 @@ static int compress_chunk(PREFIX3(stream) *strm, int level, int size, int last)
         goto free_buf;
     }
 
-    strm->next_in = buf;
-    strm->avail_in = size;
+    /* Provide only output buffer to deflateParams(). It might need some space to flush the leftovers from the last
+     * deflate(), but we don't want it to compress anything new. */
+    strm->next_in = NULL;
+    strm->avail_in = 0;
     strm->next_out = buf + size;
     strm->avail_out = compsize;
+    err = PREFIX(deflateParams)(strm, level, Z_DEFAULT_STRATEGY);
+    if (err != Z_OK) {
+        fprintf(stderr, "deflateParams() failed with code %d\n", err);
+        goto free_buf;
+    }
 
+    /* Provide input buffer to deflate(). */
+    strm->next_in = buf;
+    strm->avail_in = size;
     err = PREFIX(deflate)(strm, last ? Z_FINISH : Z_SYNC_FLUSH);
     if ((!last && err != Z_OK) || (last && err != Z_STREAM_END)) {
         fprintf(stderr, "deflate() failed with code %d\n", err);