From c27639c4d2e7ef125d7da9b0a819d3cc02b5cacb Mon Sep 17 00:00:00 2001 From: Xueming Shen Date: Tue, 19 May 2009 16:21:48 -0700 Subject: [PATCH] 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails 6735255: ZipFile.close() does not close ZipFileInputStreams, contrary to the API document Throws OOM when malloc failed. Closes all outstanding streams when closing Reviewed-by: alanb --- .../share/classes/java/util/zip/ZipFile.java | 17 +++++++++++++++-- jdk/src/share/native/java/util/zip/Deflater.c | 4 ++++ jdk/src/share/native/java/util/zip/Inflater.c | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/jdk/src/share/classes/java/util/zip/ZipFile.java b/jdk/src/share/classes/java/util/zip/ZipFile.java index f37121c5256..4f725fc1f53 100644 --- a/jdk/src/share/classes/java/util/zip/ZipFile.java +++ b/jdk/src/share/classes/java/util/zip/ZipFile.java @@ -32,6 +32,8 @@ import java.io.File; import java.nio.charset.Charset; import java.util.Vector; import java.util.Enumeration; +import java.util.Set; +import java.util.HashSet; import java.util.NoSuchElementException; import static java.util.zip.ZipConstants64.*; @@ -277,6 +279,9 @@ class ZipFile implements ZipConstants { // freeEntry releases the C jzentry struct. private static native void freeEntry(long jzfile, long jzentry); + // the outstanding inputstreams that need to be closed. + private Set streams = new HashSet(); + /** * Returns an input stream for reading the contents of the specified * zip file entry. @@ -308,6 +313,7 @@ class ZipFile implements ZipConstants { return null; } in = new ZipFileInputStream(jzentry); + streams.add(in); } final ZipFileInputStream zfin = in; switch (getEntryMethod(jzentry)) { @@ -323,7 +329,7 @@ class ZipFile implements ZipConstants { public void close() throws IOException { if (!isClosed) { - releaseInflater(inf); + releaseInflater(inf); this.in.close(); isClosed = true; } @@ -497,6 +503,13 @@ class ZipFile implements ZipConstants { synchronized (this) { closeRequested = true; + if (streams.size() !=0) { + Set copy = streams; + streams = new HashSet(); + for (ZipFileInputStream is: copy) + is.close(); + } + if (jzfile != 0) { // Close the zip file long zf = this.jzfile; @@ -631,9 +644,9 @@ class ZipFile implements ZipConstants { freeEntry(ZipFile.this.jzfile, jzentry); jzentry = 0; } + streams.remove(this); } } - } diff --git a/jdk/src/share/native/java/util/zip/Deflater.c b/jdk/src/share/native/java/util/zip/Deflater.c index 98216b581ac..1cc7cf79af4 100644 --- a/jdk/src/share/native/java/util/zip/Deflater.c +++ b/jdk/src/share/native/java/util/zip/Deflater.c @@ -138,6 +138,7 @@ Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, in_buf = (jbyte *) malloc(this_len); if (in_buf == 0) { + JNU_ThrowOutOfMemoryError(env, 0); return 0; } (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf); @@ -145,6 +146,7 @@ Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, out_buf = (jbyte *) malloc(len); if (out_buf == 0) { free(in_buf); + JNU_ThrowOutOfMemoryError(env, 0); return 0; } @@ -179,6 +181,7 @@ Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, in_buf = (jbyte *) malloc(this_len); if (in_buf == 0) { + JNU_ThrowOutOfMemoryError(env, 0); return 0; } (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf); @@ -186,6 +189,7 @@ Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, out_buf = (jbyte *) malloc(len); if (out_buf == 0) { free(in_buf); + JNU_ThrowOutOfMemoryError(env, 0); return 0; } diff --git a/jdk/src/share/native/java/util/zip/Inflater.c b/jdk/src/share/native/java/util/zip/Inflater.c index 4d9c30ee0e6..b26ab6fe7ef 100644 --- a/jdk/src/share/native/java/util/zip/Inflater.c +++ b/jdk/src/share/native/java/util/zip/Inflater.c @@ -125,6 +125,7 @@ Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, in_buf = (jbyte *) malloc(this_len); if (in_buf == 0) { + JNU_ThrowOutOfMemoryError(env, 0); return 0; } (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf); @@ -132,6 +133,7 @@ Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, out_buf = (jbyte *) malloc(len); if (out_buf == 0) { free(in_buf); + JNU_ThrowOutOfMemoryError(env, 0); return 0; }