Skip to content

Commit 4942516

Browse files
author
Yuri Nesterenko
committed
8278794: Infinite loop in DeflaterOutputStream.finish()
8284771: java/util/zip/CloseInflaterDeflaterTest.java failed with "AssertionError: Expected IOException to be thrown, but nothing was thrown" Reviewed-by: phh, sgehwolf Backport-of: ff0b0927a2df8b36f8fd6ed41bd4e20e71a5b653
1 parent 89697c3 commit 4942516

File tree

5 files changed

+232
-154
lines changed

5 files changed

+232
-154
lines changed

jdk/src/share/classes/java/util/zip/Deflater.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -559,6 +559,16 @@ private void ensureOpen() {
559559
throw new NullPointerException("Deflater has been closed");
560560
}
561561

562+
/**
563+
* Returns the value of 'finish' flag.
564+
* 'finish' will be set to true if def.finish() method is called.
565+
*/
566+
boolean shouldFinish() {
567+
synchronized (zsRef) {
568+
return finish;
569+
}
570+
}
571+
562572
private static native void initIDs();
563573
private native static long init(int level, int strategy, boolean nowrap);
564574
private native static void setDictionary(long addr, byte[] b, int off, int len);

jdk/src/share/classes/java/util/zip/DeflaterOutputStream.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -221,9 +221,15 @@ public void write(byte[] b, int off, int len) throws IOException {
221221
*/
222222
public void finish() throws IOException {
223223
if (!def.finished()) {
224-
def.finish();
225-
while (!def.finished()) {
226-
deflate();
224+
try{
225+
def.finish();
226+
while (!def.finished()) {
227+
deflate();
228+
}
229+
} catch(IOException e) {
230+
if (usesDefaultDeflater)
231+
def.end();
232+
throw e;
227233
}
228234
}
229235
}

jdk/src/share/classes/java/util/zip/ZipOutputStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -304,7 +304,7 @@ public void closeEntry() throws IOException {
304304
crc.reset();
305305
current = null;
306306
} catch (IOException e) {
307-
if (usesDefaultDeflater && !(e instanceof ZipException))
307+
if (def.shouldFinish() && usesDefaultDeflater && !(e instanceof ZipException))
308308
def.end();
309309
throw e;
310310
}

jdk/test/java/util/zip/CloseDeflaterTest.java

Lines changed: 0 additions & 147 deletions
This file was deleted.

0 commit comments

Comments
 (0)