Skip to content

Commit b5cfd76

Browse files
committed
8358456: ZipFile.getInputStream(ZipEntry) throws unspecified IllegalArgumentException
Reviewed-by: lancea
1 parent 683319f commit b5cfd76

2 files changed

Lines changed: 132 additions & 8 deletions

File tree

src/java.base/share/classes/java/util/zip/ZipFile.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ public InputStream getInputStream(ZipEntry entry) throws IOException {
351351
case DEFLATED:
352352
// Inflater likes a bit of slack
353353
// MORE: Compute good size for inflater stream:
354-
long size = CENSIZ(zsrc.cen, pos);
355-
if (size > 65536) {
356-
size = 8192;
354+
long inputBufSize = CENSIZ(zsrc.cen, pos);
355+
if (inputBufSize > 65536 || inputBufSize <= 0) {
356+
inputBufSize = 8192;
357357
}
358-
InputStream is = new ZipFileInflaterInputStream(in, res, (int) size);
358+
InputStream is = new ZipFileInflaterInputStream(in, res, (int) inputBufSize);
359359
synchronized (istreams) {
360360
istreams.add(is);
361361
}
@@ -416,14 +416,14 @@ private class ZipFileInflaterInputStream extends InflaterInputStream {
416416
private final Cleanable cleanable;
417417

418418
ZipFileInflaterInputStream(ZipFileInputStream zfin,
419-
CleanableResource res, int size) {
420-
this(zfin, res, res.getInflater(), size);
419+
CleanableResource res, int inputBufSize) {
420+
this(zfin, res, res.getInflater(), inputBufSize);
421421
}
422422

423423
private ZipFileInflaterInputStream(ZipFileInputStream zfin,
424424
CleanableResource res,
425-
Inflater inf, int size) {
426-
super(zfin, inf, size);
425+
Inflater inf, int inputBufSize) {
426+
super(zfin, inf, inputBufSize);
427427
this.cleanable = CleanerFactory.cleaner().register(this,
428428
new InflaterCleanupAction(inf, res));
429429
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.nio.ByteBuffer;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.util.HexFormat;
32+
import java.util.zip.ZipEntry;
33+
import java.util.zip.ZipFile;
34+
import java.util.zip.ZipOutputStream;
35+
36+
import org.junit.jupiter.api.Test;
37+
import static java.nio.ByteOrder.LITTLE_ENDIAN;
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertNotNull;
40+
41+
/*
42+
* @test
43+
* @bug 8358456
44+
* @summary verify that ZipFile.getInputStream(ZipFile) doesn't throw an unspecified exception
45+
* for invalid compressed size of an entry
46+
* @run junit InvalidCompressedSizeTest
47+
*/
48+
class InvalidCompressedSizeTest {
49+
50+
private static final String ENTRY_NAME = "foo-bar";
51+
private static final byte[] ENTRY_CONTENT = new byte[]{0x42, 0x42};
52+
53+
// created through a call to createZIPContent()
54+
private static final String ZIP_CONTENT_HEX = """
55+
504b03041400080808005053c35a00000000000000000000000007000000666f6f2d6261727
56+
3720200504b0708c41f441b0400000002000000504b010214001400080808005053c35ac41f
57+
441b0400000002000000070000000000000000000000000000000000666f6f2d626172504b0
58+
506000000000100010035000000390000000000
59+
""";
60+
61+
62+
// 0039 CENTRAL HEADER #1 02014B50
63+
// ...
64+
// 0043 Compression Method 0008 'Deflated'
65+
// ...
66+
// 004D Compressed Length 00000004
67+
// 0051 Uncompressed Length 00000002
68+
// ...
69+
// 0067 Filename 'foo-bar'
70+
// this is the offset in the ZIP content stream for the compressed size field
71+
// for the entry of interest
72+
private static final int COMP_SIZE_OFFSET = 0x004D;
73+
74+
// intentionally unused but left here to allow for constructing newer/updated
75+
// ZIP_CONTENT_HEX, when necessary
76+
private static String createZIPContent() throws IOException {
77+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
78+
try (final ZipOutputStream zos = new ZipOutputStream(baos)) {
79+
final ZipEntry ze = new ZipEntry(ENTRY_NAME);
80+
zos.putNextEntry(ze);
81+
zos.write(ENTRY_CONTENT);
82+
zos.closeEntry();
83+
}
84+
return HexFormat.of().formatHex(baos.toByteArray());
85+
}
86+
87+
/*
88+
* Calls ZipFile.getInputStream(ZipEntry) on a ZIP entry whose compressed size is
89+
* intentionally set to 0. The test then verifies that the call to getInputStream()
90+
* doesn't throw an unspecified exception.
91+
*/
92+
@Test
93+
void testInvalidCompressedSize() throws Exception {
94+
final byte[] originalZIPContent = HexFormat.of().parseHex(ZIP_CONTENT_HEX.replace("\n", ""));
95+
final ByteBuffer zipContent = ByteBuffer.wrap(originalZIPContent).order(LITTLE_ENDIAN);
96+
97+
// overwrite the compressed size value in the entry's CEN to an invalid value of 0
98+
zipContent.position(COMP_SIZE_OFFSET);
99+
final int invalidCompressedSize = 0;
100+
zipContent.putInt(invalidCompressedSize);
101+
zipContent.rewind();
102+
103+
// write out the ZIP content so that it can be read through ZipFile
104+
final Path zip = Files.createTempFile(Path.of("."), "8358456-", ".zip");
105+
Files.write(zip, zipContent.array());
106+
System.out.println("created ZIP " + zip + " with an invalid compressed size for entry");
107+
108+
try (final ZipFile zf = new ZipFile(zip.toFile())) {
109+
final ZipEntry entry = zf.getEntry(ENTRY_NAME);
110+
assertNotNull(entry, "missing entry " + ENTRY_NAME + " in ZIP file " + zip);
111+
// verify that we are indeed testing a ZIP file with an invalid
112+
// compressed size for the entry
113+
assertEquals(0, entry.getCompressedSize(), "unexpected compressed size");
114+
// merely open and close the InputStream to exercise the code which
115+
// would incorrectly raise an exception. we don't read the contents
116+
// of the stream because we have (intentionally) corrupted the metadata
117+
// of the ZIP and that will cause the reading to fail.
118+
try (final InputStream is = zf.getInputStream(entry)) {
119+
System.out.println("successfully opened input stream " + is
120+
+ " for entry " + entry.getName());
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)