Skip to content

Commit 354b548

Browse files
authored
Throw ArchiveException instead of EOFException when CPIO namesize <= 0 (#771)
1 parent bd48936 commit 354b548

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ private CpioArchiveEntry readNewEntry(final boolean hasCrc) throws IOException {
463463
newEntry.setRemoteDeviceMaj(readAsciiLong(8, 16));
464464
newEntry.setRemoteDeviceMin(readAsciiLong(8, 16));
465465
final long namesize = readAsciiLong(8, 16);
466-
if (namesize < 0) {
466+
if (namesize <= 0) {
467467
throw new ArchiveException("Found illegal entry with negative name length");
468468
}
469469
newEntry.setChksum(readAsciiLong(8, 16));

src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ void testCrcVerification() throws Exception {
9393
}
9494
}
9595

96+
@Test
97+
void testEndOfFileInEntry_c_namesize_0x00000000() throws Exception {
98+
// CPIO header with c_namesize = 0x00000000
99+
// @formatter:off
100+
final String header =
101+
"070701" + // c_magic
102+
"00000000" + // c_ino
103+
"000081A4" + // c_mode
104+
"00000000" + // c_uid
105+
"00000000" + // c_gid
106+
"00000001" + // c_nlink
107+
"00000000" + // c_mtime
108+
"00000000" + // c_filesize
109+
"00000000" + // c_devmajor
110+
"00000000" + // c_devminor
111+
"00000000" + // c_rdevmajor
112+
"00000000" + // c_rdevminor
113+
"00000000" + // c_namesize
114+
"00000000"; // c_check
115+
// @formatter:on
116+
final byte[] data = new byte[header.getBytes(StandardCharsets.US_ASCII).length + 1];
117+
System.arraycopy(header.getBytes(), 0, data, 0, header.getBytes().length);
118+
try (CpioArchiveInputStream cpio = CpioArchiveInputStream.builder().setByteArray(data).get()) {
119+
assertThrows(ArchiveException.class, () -> cpio.getNextEntry());
120+
}
121+
}
122+
96123
@Test
97124
void testEndOfFileInEntry_c_namesize_0xFFFFFFFF() throws Exception {
98125
// CPIO header with c_namesize = 0xFFFFFFFF

0 commit comments

Comments
 (0)