8184306: zlib 1.2.11 upgrade triggers j.u.zip.Deflater regression

Reviewed-by: alanb, bpb
This commit is contained in:
Xueming Shen 2017-07-17 14:48:43 -07:00
parent b507a80b2b
commit d98cecb4c0
3 changed files with 92 additions and 29 deletions

View File

@ -505,6 +505,8 @@ int ZEXPORT deflateResetKeep (strm)
s->pending = 0; s->pending = 0;
s->pending_out = s->pending_buf; s->pending_out = s->pending_buf;
s->high_water = 0; /* reset to its inital value 0 */
if (s->wrap < 0) { if (s->wrap < 0) {
s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
} }

View File

@ -76,6 +76,21 @@
> n = len; > n = len;
-------------------------- --------------------------
(6) deflate.c #8184306
*** 503,512 ****
--- 503,514 ----
s = (deflate_state *)strm->state;
s->pending = 0;
s->pending_out = s->pending_buf;
+ s->high_water = 0; /* reset to its inital value 0 */
+
if (s->wrap < 0) {
s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
}
s->status =
#ifdef GZIP

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/** /**
* @test * @test
* @bug 7110149 * @bug 7110149 8184306
* @summary Test basic deflater & inflater functionality * @summary Test basic deflater & inflater functionality
* @key randomness * @key randomness
*/ */
@ -34,64 +34,110 @@ import java.util.zip.*;
public class DeInflate { public class DeInflate {
static void check(Deflater compresser, byte[] in, int len, static void checkStream(Deflater def, byte[] in, int len,
byte[] out1, byte[] out2, boolean nowrap)
throws Throwable
{
Arrays.fill(out1, (byte)0);
Arrays.fill(out2, (byte)0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DeflaterOutputStream defos = new DeflaterOutputStream(baos, def)) {
defos.write(in, 0, len);
}
out1 = baos.toByteArray();
int m = out1.length;
Inflater inf = new Inflater(nowrap);
inf.setInput(out1, 0, m);
int n = inf.inflate(out2);
if (n != len ||
!Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
inf.inflate(out2) != 0) {
System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
m, n, len, Arrays.equals(in, out2));
throw new RuntimeException("De/inflater failed:" + def);
}
}
static void check(Deflater def, byte[] in, int len,
byte[] out1, byte[] out2, boolean nowrap) byte[] out1, byte[] out2, boolean nowrap)
throws Throwable throws Throwable
{ {
Arrays.fill(out1, (byte)0); Arrays.fill(out1, (byte)0);
Arrays.fill(out2, (byte)0); Arrays.fill(out2, (byte)0);
compresser.setInput(in, 0, len); def.setInput(in, 0, len);
compresser.finish(); def.finish();
int m = compresser.deflate(out1); int m = def.deflate(out1);
Inflater decompresser = new Inflater(nowrap); Inflater inf = new Inflater(nowrap);
decompresser.setInput(out1, 0, m); inf.setInput(out1, 0, m);
int n = decompresser.inflate(out2); int n = inf.inflate(out2);
if (n != len || if (n != len ||
!Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) || !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
decompresser.inflate(out2) != 0) { inf.inflate(out2) != 0) {
System.out.printf("m=%d, n=%d, len=%d, eq=%b%n", System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
m, n, len, Arrays.equals(in, out2)); m, n, len, Arrays.equals(in, out2));
throw new RuntimeException("De/inflater failed:" + compresser); throw new RuntimeException("De/inflater failed:" + def);
} }
} }
private static Deflater newDeflater(int level, int strategy, boolean dowrap, byte[] tmp) {
Deflater def = new Deflater(level, dowrap);
if (strategy != Deflater.DEFAULT_STRATEGY) {
def.setStrategy(strategy);
// The first invocation after setLevel/Strategy()
// with a different level/stragety returns 0, if
// there is no need to flush out anything for the
// previous setting/"data", this is tricky and
// appears un-documented.
def.deflate(tmp);
}
return def;
}
private static Deflater resetDeflater(Deflater def, int level, int strategy) {
def.setLevel(level);
def.setStrategy(strategy);
def.reset();
return def;
}
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
byte[] dataIn = new byte[1024 * 512]; byte[] dataIn = new byte[1024 * 512];
new Random().nextBytes(dataIn); new Random().nextBytes(dataIn);
byte[] dataOut1 = new byte[dataIn.length + 1024]; byte[] dataOut1 = new byte[dataIn.length + 1024];
byte[] dataOut2 = new byte[dataIn.length]; byte[] dataOut2 = new byte[dataIn.length];
boolean wrap[] = new boolean[] { false, true };
Deflater defNotWrap = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
Deflater defWrap = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
for (int level = Deflater.DEFAULT_COMPRESSION; for (int level = Deflater.DEFAULT_COMPRESSION;
level <= Deflater.BEST_COMPRESSION; level++) { level <= Deflater.BEST_COMPRESSION; level++) {
System.out.print("level=" + level + ", strategy= ");
for (int strategy = Deflater.DEFAULT_STRATEGY; for (int strategy = Deflater.DEFAULT_STRATEGY;
strategy <= Deflater.HUFFMAN_ONLY; strategy++) { strategy <= Deflater.HUFFMAN_ONLY; strategy++) {
System.out.print(" " + strategy + " nowrap["); for (boolean dowrap : new boolean[] { false, true }) {
for (int dowrap = 0; dowrap <= 1; dowrap++) { System.out.println("level:" + level +
System.out.print(" " + wrap[dowrap]); ", strategy: " + strategy +
", dowrap: " + dowrap);
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
Deflater def = new Deflater(level, wrap[dowrap]);
if (strategy != Deflater.DEFAULT_STRATEGY) {
def.setStrategy(strategy);
// The first invocation after setLevel/Strategy()
// with a different level/stragety returns 0, if
// there is no need to flush out anything for the
// previous setting/"data", this is tricky and
// appears un-documented.
def.deflate(dataOut2);
}
int len = (i == 0)? dataIn.length int len = (i == 0)? dataIn.length
: new Random().nextInt(dataIn.length); : new Random().nextInt(dataIn.length);
check(def, dataIn, len, dataOut1, dataOut2, wrap[dowrap]); // use a new deflater
Deflater def = newDeflater(level, strategy, dowrap, dataOut2);
check(def, dataIn, len, dataOut1, dataOut2, dowrap);
// reuse the deflater (with reset) and test on stream, which
// uses a "smaller" buffer (smaller than the overall data)
def = resetDeflater(dowrap ? defWrap : defNotWrap, level, strategy);
checkStream(def, dataIn, len, dataOut1, dataOut2, dowrap);
} }
} }
System.out.print("] ");
} }
System.out.println();
} }
} }
} }