Skip to content

Commit eb0040c

Browse files
committed
Fix ZipCentralDirectoryFileHeaderRecord entry comment read offset
Update `ZipCentralDirectoryFileHeaderRecord.copyTo` comment read offset to account for the record position. Fixes gh-39166
1 parent fee359f commit eb0040c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/zip/ZipCentralDirectoryFileHeaderRecord.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ void copyTo(DataBlock dataBlock, long pos, ZipEntry zipEntry) throws IOException
9797
dataBlock.readFully(buffer, extraPos);
9898
zipEntry.setExtra(buffer.array());
9999
}
100-
if ((fileCommentLength() & 0xFFFF) > 0) {
101-
long commentPos = MINIMUM_SIZE + fileNameLength + extraLength;
100+
if (commentLength > 0) {
101+
long commentPos = pos + MINIMUM_SIZE + fileNameLength + extraLength;
102102
zipEntry.setComment(ZipString.readString(dataBlock, commentPos, commentLength));
103103
}
104104
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/NestedJarFileTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
import java.io.InputStream;
2424
import java.lang.ref.Cleaner.Cleanable;
2525
import java.nio.charset.Charset;
26+
import java.util.ArrayList;
2627
import java.util.Enumeration;
28+
import java.util.List;
29+
import java.util.UUID;
2730
import java.util.jar.JarEntry;
2831
import java.util.jar.JarFile;
32+
import java.util.jar.JarOutputStream;
2933
import java.util.jar.Manifest;
34+
import java.util.zip.ZipEntry;
3035
import java.util.zip.ZipFile;
3136

3237
import org.assertj.core.extractor.Extractors;
@@ -386,4 +391,42 @@ void versionedStreamStreamsEntries() throws IOException {
386391
}
387392
}
388393

394+
@Test // gh-39166
395+
void getCommentAlignsWithJdkJar() throws Exception {
396+
File file = new File(this.tempDir, "testcomments.jar");
397+
try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) {
398+
jar.putNextEntry(new ZipEntry("BOOT-INF/"));
399+
jar.closeEntry();
400+
jar.putNextEntry(new ZipEntry("BOOT-INF/classes/"));
401+
jar.closeEntry();
402+
for (int i = 0; i < 5; i++) {
403+
ZipEntry entry = new ZipEntry("BOOT-INF/classes/T" + i + ".class");
404+
entry.setComment("T" + i);
405+
jar.putNextEntry(entry);
406+
jar.write(UUID.randomUUID().toString().getBytes());
407+
jar.closeEntry();
408+
}
409+
}
410+
List<String> jdk = collectComments(new JarFile(file));
411+
List<String> nested = collectComments(new NestedJarFile(file, "BOOT-INF/classes/"));
412+
assertThat(nested).isEqualTo(jdk);
413+
}
414+
415+
private List<String> collectComments(JarFile jarFile) throws IOException {
416+
try {
417+
List<String> comments = new ArrayList<>();
418+
Enumeration<JarEntry> entries = jarFile.entries();
419+
while (entries.hasMoreElements()) {
420+
String comment = entries.nextElement().getComment();
421+
if (comment != null) {
422+
comments.add(comment);
423+
}
424+
}
425+
return comments;
426+
}
427+
finally {
428+
jarFile.close();
429+
}
430+
}
431+
389432
}

0 commit comments

Comments
 (0)