Skip to content

Commit

Permalink
Fix ZipCentralDirectoryFileHeaderRecord entry comment read offset
Browse files Browse the repository at this point in the history
Update `ZipCentralDirectoryFileHeaderRecord.copyTo` comment read offset
to account for the record position.

Fixes gh-39166
  • Loading branch information
philwebb committed Jan 17, 2024
1 parent fee359f commit eb0040c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void copyTo(DataBlock dataBlock, long pos, ZipEntry zipEntry) throws IOException
dataBlock.readFully(buffer, extraPos);
zipEntry.setExtra(buffer.array());
}
if ((fileCommentLength() & 0xFFFF) > 0) {
long commentPos = MINIMUM_SIZE + fileNameLength + extraLength;
if (commentLength > 0) {
long commentPos = pos + MINIMUM_SIZE + fileNameLength + extraLength;
zipEntry.setComment(ZipString.readString(dataBlock, commentPos, commentLength));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
import java.io.InputStream;
import java.lang.ref.Cleaner.Cleanable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.assertj.core.extractor.Extractors;
Expand Down Expand Up @@ -386,4 +391,42 @@ void versionedStreamStreamsEntries() throws IOException {
}
}

@Test // gh-39166
void getCommentAlignsWithJdkJar() throws Exception {
File file = new File(this.tempDir, "testcomments.jar");
try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) {
jar.putNextEntry(new ZipEntry("BOOT-INF/"));
jar.closeEntry();
jar.putNextEntry(new ZipEntry("BOOT-INF/classes/"));
jar.closeEntry();
for (int i = 0; i < 5; i++) {
ZipEntry entry = new ZipEntry("BOOT-INF/classes/T" + i + ".class");
entry.setComment("T" + i);
jar.putNextEntry(entry);
jar.write(UUID.randomUUID().toString().getBytes());
jar.closeEntry();
}
}
List<String> jdk = collectComments(new JarFile(file));
List<String> nested = collectComments(new NestedJarFile(file, "BOOT-INF/classes/"));
assertThat(nested).isEqualTo(jdk);
}

private List<String> collectComments(JarFile jarFile) throws IOException {
try {
List<String> comments = new ArrayList<>();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
String comment = entries.nextElement().getComment();
if (comment != null) {
comments.add(comment);
}
}
return comments;
}
finally {
jarFile.close();
}
}

}

0 comments on commit eb0040c

Please sign in to comment.