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
This commit is contained in:
Xueming Shen 2009-05-19 16:21:48 -07:00
parent a1958b22ef
commit c27639c4d2
3 changed files with 21 additions and 2 deletions

View File

@ -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<ZipFileInputStream> streams = new HashSet<ZipFileInputStream>();
/**
* 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<ZipFileInputStream> copy = streams;
streams = new HashSet<ZipFileInputStream>();
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);
}
}
}

View File

@ -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;
}

View File

@ -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;
}