Skip to content

Commit

Permalink
8278794: Infinite loop in DeflaterOutputStream.finish()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Yuri Nesterenko committed Aug 26, 2024
1 parent 89697c3 commit 4942516
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 154 deletions.
12 changes: 11 additions & 1 deletion jdk/src/share/classes/java/util/zip/Deflater.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -559,6 +559,16 @@ private void ensureOpen() {
throw new NullPointerException("Deflater has been closed");
}

/**
* Returns the value of 'finish' flag.
* 'finish' will be set to true if def.finish() method is called.
*/
boolean shouldFinish() {
synchronized (zsRef) {
return finish;
}
}

private static native void initIDs();
private native static long init(int level, int strategy, boolean nowrap);
private native static void setDictionary(long addr, byte[] b, int off, int len);
Expand Down
14 changes: 10 additions & 4 deletions jdk/src/share/classes/java/util/zip/DeflaterOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -221,9 +221,15 @@ public void write(byte[] b, int off, int len) throws IOException {
*/
public void finish() throws IOException {
if (!def.finished()) {
def.finish();
while (!def.finished()) {
deflate();
try{
def.finish();
while (!def.finished()) {
deflate();
}
} catch(IOException e) {
if (usesDefaultDeflater)
def.end();
throw e;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions jdk/src/share/classes/java/util/zip/ZipOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -304,7 +304,7 @@ public void closeEntry() throws IOException {
crc.reset();
current = null;
} catch (IOException e) {
if (usesDefaultDeflater && !(e instanceof ZipException))
if (def.shouldFinish() && usesDefaultDeflater && !(e instanceof ZipException))
def.end();
throw e;
}
Expand Down
147 changes: 0 additions & 147 deletions jdk/test/java/util/zip/CloseDeflaterTest.java

This file was deleted.

Loading

1 comment on commit 4942516

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.