mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 20:14:43 +02:00
6763122: ZipFile ctor does not throw exception when file is not a zip file
Reviewed-by: bristor
This commit is contained in:
parent
09220bdd58
commit
e9a892dd41
2 changed files with 42 additions and 31 deletions
|
@ -273,8 +273,8 @@ static const jlong END_MAXLEN = 0xFFFF + ENDHDR;
|
|||
/*
|
||||
* Searches for end of central directory (END) header. The contents of
|
||||
* 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
|
||||
* was not found or -1 if an error occurred.
|
||||
* position of the END header, otherwise returns -1 if the END header
|
||||
* was not found or an error occurred.
|
||||
*/
|
||||
static jlong
|
||||
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
|
||||
* CEN header, otherwise returns 0 if central directory not found or -1
|
||||
* if an error occurred. If zip->msg != NULL then the error was a zip
|
||||
* format error and zip->msg has the error text.
|
||||
* CEN header, otherwise returns -1 if an error occured. If zip->msg != NULL
|
||||
* then the error was a zip format error and zip->msg has the error text.
|
||||
* Always pass in -1 for knownTotal; it's used for a recursive call.
|
||||
*/
|
||||
static jlong
|
||||
|
@ -488,9 +487,9 @@ readCEN(jzfile *zip, jint knownTotal)
|
|||
|
||||
/* Get position of END header */
|
||||
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);
|
||||
|
||||
|
|
|
@ -39,34 +39,23 @@ public class TestEmptyZip {
|
|||
throw new Exception("failed to delete " + zipName);
|
||||
}
|
||||
|
||||
// Verify 0-length file cannot be read
|
||||
f.createNewFile();
|
||||
ZipFile zf = null;
|
||||
try {
|
||||
zf = new ZipFile(f);
|
||||
fail();
|
||||
} catch (Exception ex) {
|
||||
check(ex.getMessage().contains("zip file is empty"));
|
||||
} finally {
|
||||
if (zf != null) {
|
||||
zf.close();
|
||||
}
|
||||
}
|
||||
// Verify 0-length file cannot be read
|
||||
checkCannotRead(f);
|
||||
|
||||
ZipInputStream zis = null;
|
||||
try {
|
||||
zis = new ZipInputStream(new FileInputStream(f));
|
||||
ZipEntry ze = zis.getNextEntry();
|
||||
check(ze == null);
|
||||
} catch (Exception ex) {
|
||||
unexpected(ex);
|
||||
} finally {
|
||||
if (zis != null) {
|
||||
zis.close();
|
||||
// Verify non-zip file cannot be read
|
||||
OutputStream out = new FileOutputStream(f);
|
||||
try {
|
||||
out.write("class Foo { }".getBytes());
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
checkCannotRead(f);
|
||||
|
||||
f.delete();
|
||||
} finally {
|
||||
f.delete();
|
||||
}
|
||||
|
||||
// Verify 0-entries file can be written
|
||||
write(f);
|
||||
|
@ -78,6 +67,29 @@ public class TestEmptyZip {
|
|||
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 {
|
||||
ZipOutputStream zos = null;
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue