8242882: opening jar file with large manifest might throw NegativeArraySizeException

Reviewed-by: bchristi, lancea
This commit is contained in:
Jaikiran Pai 2020-10-08 10:46:37 +00:00 committed by Lance Andersen
parent f86037207c
commit 782d45bdec
2 changed files with 85 additions and 1 deletions

View file

@ -152,6 +152,8 @@ public class JarFile extends ZipFile {
private static final boolean MULTI_RELEASE_ENABLED;
private static final boolean MULTI_RELEASE_FORCED;
private static final ThreadLocal<Boolean> isInitializing = new ThreadLocal<>();
// The maximum size of array to allocate. Some VMs reserve some header words in an array.
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private SoftReference<Manifest> manRef;
private JarEntry manEntry;
@ -788,7 +790,11 @@ public class JarFile extends ZipFile {
*/
private byte[] getBytes(ZipEntry ze) throws IOException {
try (InputStream is = super.getInputStream(ze)) {
int len = (int)ze.getSize();
long uncompressedSize = ze.getSize();
if (uncompressedSize > MAX_ARRAY_SIZE) {
throw new OutOfMemoryError("Required array size too large");
}
int len = (int)uncompressedSize;
int bytesRead;
byte[] b;
// trust specified entry sizes when reasonably small