Skip to content

Commit

Permalink
8278794: Infinite loop in DeflaterOutputStream.finish()
Browse files Browse the repository at this point in the history
Reviewed-by: coffeys, lancea
  • Loading branch information
Ravi Reddy authored and Lance Andersen committed Mar 18, 2022
1 parent b2aa085 commit ff0b092
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 154 deletions.
12 changes: 11 additions & 1 deletion src/java.base/share/classes/java/util/zip/Deflater.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, 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 @@ -895,6 +895,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 long init(int level, int strategy, boolean nowrap);
private static native void setDictionary(long addr, byte[] b, int off,
int len);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, 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 @@ -220,9 +220,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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, 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 @@ -314,7 +314,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 test/jdk/java/util/zip/CloseDeflaterTest.java

This file was deleted.

1 comment on commit ff0b092

@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.