Skip to content

Commit d2e3daf

Browse files
[COMPRESS-718] Adjust size validation for OLD_ASCII format (#759)
* Adjust size validation for OLD_ASCII format Support files up to 8GiB in size for a single entry when using the OLD_ASCII format. * Use camel-case --------- Co-authored-by: Gary Gregory <[email protected]>
1 parent 6518f80 commit d2e3daf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,9 @@ public void setRemoteDeviceMin(final long rmin) {
911911
* @param size The file size to set.
912912
*/
913913
public void setSize(final long size) {
914-
if (size < 0 || size > 0xFFFFFFFFL) {
914+
// When using the OLD_ASCII format, files can be up to 8GiB in size.
915+
final long maxSize = this.fileFormat == FORMAT_OLD_ASCII ? 0x1FFFFFFFFL : 0xFFFFFFFFL;
916+
if (size < 0 || size > maxSize) {
915917
throw new IllegalArgumentException("Invalid entry size <" + size + ">");
916918
}
917919
this.fileSize = size;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,24 @@
2323

2424
import org.apache.commons.compress.archivers.ArchiveException;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
2628

2729
class CpioArchiveEntryTest {
2830

31+
@ParameterizedTest
32+
@ValueSource(shorts = {CpioConstants.FORMAT_NEW, CpioConstants.FORMAT_NEW_CRC, CpioConstants.FORMAT_OLD_BINARY})
33+
void testCpioEntrySizeUnder4GiBNotOldAsciiFormat(short format) {
34+
final CpioArchiveEntry entry = new CpioArchiveEntry(format);
35+
assertThrows(IllegalArgumentException.class, () -> entry.setSize(0x1FFFFFFFFL));
36+
}
37+
38+
@Test
39+
void testCpioEntrySizeOldAsciiFormatOver4GiB() {
40+
final CpioArchiveEntry entry = new CpioArchiveEntry(CpioConstants.FORMAT_OLD_ASCII);
41+
entry.setSize(0x1FFFFFFFFL);
42+
}
43+
2944
@Test
3045
void testGetHeaderPadCountOverflow() throws Exception {
3146
final CpioArchiveEntry entry = new CpioArchiveEntry(CpioConstants.FORMAT_NEW);

0 commit comments

Comments
 (0)