Skip to content

Commit 40a5373

Browse files
committed
Better handling of corrupt archives
1 parent 43355d9 commit 40a5373

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

src/main/java/org/apache/commons/compress/archivers/lha/LhaArchiveInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Builder setFileSeparatorChar(final char fileSeparatorChar) {
166166
private static final int HEADER_LEVEL_1_OFFSET_FILENAME = 22;
167167

168168
// Header Level 2
169+
private static final int HEADER_LEVEL_2_MINIMUM_HEADER_LENGTH = 26;
169170
private static final int HEADER_LEVEL_2_OFFSET_HEADER_SIZE = 0;
170171
private static final int HEADER_LEVEL_2_OFFSET_COMPRESSED_SIZE = 7;
171172
private static final int HEADER_LEVEL_2_OFFSET_ORIGINAL_SIZE = 11;
@@ -473,7 +474,7 @@ LhaArchiveEntry readHeaderLevel1(ByteBuffer buffer) throws IOException {
473474
*/
474475
LhaArchiveEntry readHeaderLevel2(ByteBuffer buffer) throws IOException {
475476
final int headerSize = Short.toUnsignedInt(buffer.getShort(HEADER_LEVEL_2_OFFSET_HEADER_SIZE));
476-
if (headerSize < HEADER_GENERIC_MINIMUM_HEADER_LENGTH) {
477+
if (headerSize < HEADER_LEVEL_2_MINIMUM_HEADER_LENGTH) {
477478
throw new ArchiveException("Invalid header level 2 length: %d", headerSize);
478479
}
479480

src/test/java/org/apache/commons/compress/archivers/lha/LhaArchiveInputStreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,13 @@ void testParseHeaderLevel2File() throws IOException {
904904
void testInvalidHeaderLevel2Length() throws IOException {
905905
final byte[] data = toByteArray(VALID_HEADER_LEVEL_2_FILE);
906906

907-
data[0] = 0x10; // Change the first byte to an invalid length
907+
data[0] = 25; // Change the first byte to an invalid length
908908

909909
try (LhaArchiveInputStream archive = LhaArchiveInputStream.builder().setInputStream(new ByteArrayInputStream(data)).get()) {
910910
archive.getNextEntry();
911911
fail("Expected ArchiveException for invalid header length");
912912
} catch (ArchiveException e) {
913-
assertEquals("Invalid header level 2 length: 16", e.getMessage());
913+
assertEquals("Invalid header level 2 length: 25", e.getMessage());
914914
}
915915
}
916916

0 commit comments

Comments
 (0)