Skip to content

Commit c55bfb0

Browse files
committed
Merge branch '1.4.x' into 1.5.x
2 parents 94e9c94 + 4563272 commit c55bfb0

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/JarFileArchive.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,8 +100,14 @@ protected Archive getNestedArchive(Entry entry) throws IOException {
100100
if (jarEntry.getComment().startsWith(UNPACK_MARKER)) {
101101
return getUnpackedNestedArchive(jarEntry);
102102
}
103-
JarFile jarFile = this.jarFile.getNestedJarFile(jarEntry);
104-
return new JarFileArchive(jarFile);
103+
try {
104+
JarFile jarFile = this.jarFile.getNestedJarFile(jarEntry);
105+
return new JarFileArchive(jarFile);
106+
}
107+
catch (Exception ex) {
108+
throw new IllegalStateException(
109+
"Failed to get nested archive for entry " + entry.getName(), ex);
110+
}
105111
}
106112

107113
private Archive getUnpackedNestedArchive(JarEntry jarEntry) throws IOException {

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/CentralDirectoryEndRecord.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
* A ZIP File "End of central directory record" (EOCD).
2525
*
2626
* @author Phillip Webb
27+
* @author Andy Wilkinson
2728
* @see <a href="http://en.wikipedia.org/wiki/Zip_%28file_format%29">Zip File Format</a>
2829
*/
2930
class CentralDirectoryEndRecord {
@@ -118,7 +119,11 @@ public RandomAccessData getCentralDirectory(RandomAccessData data) {
118119
* @return the number of records in the zip
119120
*/
120121
public int getNumberOfRecords() {
121-
return (int) Bytes.littleEndianValue(this.block, this.offset + 10, 2);
122+
long numberOfRecords = Bytes.littleEndianValue(this.block, this.offset + 10, 2);
123+
if (numberOfRecords == 0xFFFF) {
124+
throw new IllegalStateException("Zip64 archives are not supported");
125+
}
126+
return (int) numberOfRecords;
122127
}
123128

124129
}

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,20 +16,30 @@
1616

1717
package org.springframework.boot.loader.archive;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.io.File;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
2023
import java.net.URL;
2124
import java.util.HashMap;
2225
import java.util.Map;
26+
import java.util.jar.JarEntry;
27+
import java.util.jar.JarOutputStream;
28+
import java.util.zip.CRC32;
29+
import java.util.zip.ZipEntry;
2330

2431
import org.junit.Before;
2532
import org.junit.Rule;
2633
import org.junit.Test;
34+
import org.junit.rules.ExpectedException;
2735
import org.junit.rules.TemporaryFolder;
2836

2937
import org.springframework.boot.loader.TestJarCreator;
3038
import org.springframework.boot.loader.archive.Archive.Entry;
39+
import org.springframework.util.FileCopyUtils;
3140

3241
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.hamcrest.CoreMatchers.equalTo;
3343

3444
/**
3545
* Tests for {@link JarFileArchive}.
@@ -42,6 +52,9 @@ public class JarFileArchiveTests {
4252
@Rule
4353
public TemporaryFolder temporaryFolder = new TemporaryFolder();
4454

55+
@Rule
56+
public ExpectedException thrown = ExpectedException.none();
57+
4558
private File rootJarFile;
4659

4760
private JarFileArchive archive;
@@ -120,6 +133,48 @@ public void unpackedLocationsFromSameArchiveShareSameParent() throws Exception {
120133
assertThat(nested.getParent()).isEqualTo(anotherNested.getParent());
121134
}
122135

136+
@Test
137+
public void zip64ArchivesAreHandledGracefully() throws IOException {
138+
File file = this.temporaryFolder.newFile("test.jar");
139+
FileCopyUtils.copy(writeZip64Jar(), file);
140+
this.thrown.expectMessage(equalTo("Zip64 archives are not supported"));
141+
new JarFileArchive(file);
142+
}
143+
144+
@Test
145+
public void nestedZip64ArchivesAreHandledGracefully() throws IOException {
146+
File file = this.temporaryFolder.newFile("test.jar");
147+
JarOutputStream output = new JarOutputStream(new FileOutputStream(file));
148+
JarEntry zip64JarEntry = new JarEntry("nested/zip64.jar");
149+
output.putNextEntry(zip64JarEntry);
150+
byte[] zip64JarData = writeZip64Jar();
151+
zip64JarEntry.setSize(zip64JarData.length);
152+
zip64JarEntry.setCompressedSize(zip64JarData.length);
153+
zip64JarEntry.setMethod(ZipEntry.STORED);
154+
CRC32 crc32 = new CRC32();
155+
crc32.update(zip64JarData);
156+
zip64JarEntry.setCrc(crc32.getValue());
157+
output.write(zip64JarData);
158+
output.closeEntry();
159+
output.close();
160+
JarFileArchive jarFileArchive = new JarFileArchive(file);
161+
this.thrown.expectMessage(
162+
equalTo("Failed to get nested archive for entry nested/zip64.jar"));
163+
jarFileArchive
164+
.getNestedArchive(getEntriesMap(jarFileArchive).get("nested/zip64.jar"));
165+
}
166+
167+
private byte[] writeZip64Jar() throws IOException {
168+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
169+
JarOutputStream jarOutput = new JarOutputStream(bytes);
170+
for (int i = 0; i < 65537; i++) {
171+
jarOutput.putNextEntry(new JarEntry(i + ".dat"));
172+
jarOutput.closeEntry();
173+
}
174+
jarOutput.close();
175+
return bytes.toByteArray();
176+
}
177+
123178
private Map<String, Archive.Entry> getEntriesMap(Archive archive) {
124179
Map<String, Archive.Entry> entries = new HashMap<String, Archive.Entry>();
125180
for (Archive.Entry entry : archive) {

0 commit comments

Comments
 (0)