6763122: ZipFile ctor does not throw exception when file is not a zip file

Reviewed-by: bristor
This commit is contained in:
Alan Bateman 2008-11-11 08:59:43 +00:00
parent 09220bdd58
commit e9a892dd41
2 changed files with 42 additions and 31 deletions

View file

@ -273,8 +273,8 @@ static const jlong END_MAXLEN = 0xFFFF + ENDHDR;
/* /*
* Searches for end of central directory (END) header. The contents of * Searches for end of central directory (END) header. The contents of
* the END header will be read and placed in endbuf. Returns the file * the END header will be read and placed in endbuf. Returns the file
* position of the END header, otherwise returns 0 if the END header * position of the END header, otherwise returns -1 if the END header
* was not found or -1 if an error occurred. * was not found or an error occurred.
*/ */
static jlong static jlong
findEND(jzfile *zip, void *endbuf) findEND(jzfile *zip, void *endbuf)
@ -314,7 +314,7 @@ findEND(jzfile *zip, void *endbuf)
} }
} }
} }
return 0; /* END header not found */ return -1; /* END header not found */
} }
/* /*
@ -460,9 +460,8 @@ if (1) { zip->msg = message; goto Catch; } else ((void)0)
/* /*
* Reads zip file central directory. Returns the file position of first * Reads zip file central directory. Returns the file position of first
* CEN header, otherwise returns 0 if central directory not found or -1 * CEN header, otherwise returns -1 if an error occured. If zip->msg != NULL
* if an error occurred. If zip->msg != NULL then the error was a zip * then the error was a zip format error and zip->msg has the error text.
* format error and zip->msg has the error text.
* Always pass in -1 for knownTotal; it's used for a recursive call. * Always pass in -1 for knownTotal; it's used for a recursive call.
*/ */
static jlong static jlong
@ -488,9 +487,9 @@ readCEN(jzfile *zip, jint knownTotal)
/* Get position of END header */ /* Get position of END header */
if ((endpos = findEND(zip, endbuf)) == -1) if ((endpos = findEND(zip, endbuf)) == -1)
return -1; /* system error */ return -1; /* no END header or system error */
if (endpos == 0) return 0; /* END header not found */ if (endpos == 0) return 0; /* only END header present */
freeCEN(zip); freeCEN(zip);

View file

@ -39,34 +39,23 @@ public class TestEmptyZip {
throw new Exception("failed to delete " + zipName); throw new Exception("failed to delete " + zipName);
} }
// Verify 0-length file cannot be read
f.createNewFile(); f.createNewFile();
ZipFile zf = null;
try { try {
zf = new ZipFile(f); // Verify 0-length file cannot be read
fail(); checkCannotRead(f);
} catch (Exception ex) {
check(ex.getMessage().contains("zip file is empty"));
} finally {
if (zf != null) {
zf.close();
}
}
ZipInputStream zis = null; // Verify non-zip file cannot be read
OutputStream out = new FileOutputStream(f);
try { try {
zis = new ZipInputStream(new FileInputStream(f)); out.write("class Foo { }".getBytes());
ZipEntry ze = zis.getNextEntry();
check(ze == null);
} catch (Exception ex) {
unexpected(ex);
} finally { } finally {
if (zis != null) { out.close();
zis.close();
}
} }
checkCannotRead(f);
} finally {
f.delete(); f.delete();
}
// Verify 0-entries file can be written // Verify 0-entries file can be written
write(f); write(f);
@ -78,6 +67,29 @@ public class TestEmptyZip {
f.delete(); f.delete();
} }
static void checkCannotRead(File f) throws IOException {
try {
new ZipFile(f).close();
fail();
} catch (ZipException ze) {
if (f.length() == 0) {
check(ze.getMessage().contains("zip file is empty"));
} else {
pass();
}
}
ZipInputStream zis = null;
try {
zis = new ZipInputStream(new FileInputStream(f));
ZipEntry ze = zis.getNextEntry();
check(ze == null);
} catch (IOException ex) {
unexpected(ex);
} finally {
if (zis != null) zis.close();
}
}
static void write(File f) throws Exception { static void write(File f) throws Exception {
ZipOutputStream zos = null; ZipOutputStream zos = null;
try { try {