Skip to content

Commit 0f0801c

Browse files
committed
Reject "negative" 'total entries' values
1 parent 11a275d commit 0f0801c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ private void initCEN(int knownTotal) throws IOException {
17391739
if (end.cenlen > MAX_CEN_SIZE) {
17401740
zerror("invalid END header (central directory size too large)");
17411741
}
1742-
if (end.centot > end.cenlen / CENHDR) {
1742+
if (end.centot < 0 || end.centot > end.cenlen / CENHDR) {
17431743
zerror("invalid END header (total entries count too large)");
17441744
}
17451745
cen = this.cen = new byte[(int)end.cenlen];

test/jdk/java/util/zip/ZipFile/EndOfCenValidation.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.junit.jupiter.api.BeforeEach;
3333
import org.junit.jupiter.api.AfterEach;
3434
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.ValueSource;
3537

3638
import java.io.*;
3739
import java.nio.ByteBuffer;
@@ -71,7 +73,7 @@ public class EndOfCenValidation {
7173
private static final int ENDSIZ = ZipFile.ENDSIZ; // Offset of CEN size field within ENDHDR
7274
private static final int ENDOFF = ZipFile.ENDOFF; // Offset of CEN offset field within ENDHDR
7375
// Maximum allowed CEN size allowed by ZipFile
74-
private static int MAX_CEN_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
76+
private static final int MAX_CEN_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
7577

7678
// Expected message when CEN size does not match file size
7779
private static final String INVALID_CEN_BAD_SIZE = "invalid END header (bad central directory size)";
@@ -170,8 +172,15 @@ public void shouldRejectInvalidCenOffset() throws IOException {
170172
*
171173
* @throws IOException if an error occurs
172174
*/
173-
@Test
174-
public void shouldRejectBadTotalEntries() throws IOException {
175+
@ParameterizedTest
176+
@ValueSource(longs = {
177+
-1, // Negative
178+
Long.MIN_VALUE, // Very negative
179+
0x3B / 3L - 1, // Cannot fit in test ZIP's CEN
180+
MAX_CEN_SIZE / 3 + 1, // Too large to allocate int[] entries array
181+
Long.MAX_VALUE // Unreasonably large
182+
})
183+
public void shouldRejectBadTotalEntries(long totalEntries) throws IOException {
175184
/**
176185
* A small ZIP using the ZIP64 format.
177186
*
@@ -267,7 +276,7 @@ public void shouldRejectBadTotalEntries() throws IOException {
267276
ByteBuffer buf = ByteBuffer.wrap(zipBytes).order(ByteOrder.LITTLE_ENDIAN);
268277
// Offset of the 'total entries' in the 'ZIP64 END CENTRAL DIR' record
269278
// Update ZIP64 entry count to a value which cannot possibly fit in the small CEN
270-
buf.putLong(0x94, MAX_CEN_SIZE / 3);
279+
buf.putLong(0x94, totalEntries);
271280
// The corresponding END field needs the ZIP64 magic value
272281
buf.putShort(0xCA, (short) 0xFFFF);
273282
// Write the ZIP to disk

0 commit comments

Comments
 (0)