Skip to content

Commit 406dbc2

Browse files
authored
[COMPRESS-714] Internal IllegalArgumentException in ZipFile and TarFile creation is not caught (#754)
* [COMPRESS-714] Internal IllegalArgumentException in ZipFile and TarFile creation is not caught * The bug fix for [COMPRESS-714] is better done in Commons IO * TEMP in PR until IO is released. * The bug fix for [COMPRESS-714] is better done in Commons IO * The bug fix for [COMPRESS-714] is better done in Commons IO * Use IOUtils.close[Quietly]() * Pick up Commons IO 2.22.0 * Removing temporary Maven repository and fix tag Removed temporary repository configuration and cleaned up dependency closing tag.
1 parent 9eca118 commit 406dbc2

5 files changed

Lines changed: 99 additions & 11 deletions

File tree

src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,7 @@ private SevenZFile(final Builder builder) throws IOException {
636636
archive = readHeaders(password);
637637
this.password = password != null ? Arrays.copyOf(password, password.length) : null;
638638
} catch (final ArithmeticException | IllegalArgumentException e) {
639-
final ArchiveException archiveException = new ArchiveException(e);
640-
IOUtils.close(channel, archiveException::addSuppressed);
641-
throw archiveException;
639+
throw IOUtils.closeQuietlySuppress(channel, new ArchiveException(e));
642640
}
643641
}
644642

src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,8 @@ private TarFile(final Builder builder) throws IOException {
193193
while ((entry = getNextTarEntry()) != null) {
194194
entries.add(entry);
195195
}
196-
} catch (final IOException ex) {
197-
IOUtils.close(archive, ex::addSuppressed);
198-
throw ex;
196+
} catch (final IOException e) {
197+
throw IOUtils.closeQuietlySuppress(archive, e);
199198
}
200199
}
201200

src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -756,12 +756,8 @@ private ZipFile(final Builder builder) throws IOException {
756756
}
757757
fillNameMap();
758758
} catch (final IOException e) {
759-
final ArchiveException archiveException = e instanceof ArchiveException
760-
? (ArchiveException) e
761-
: new ArchiveException("Error reading Zip content from " + builder.getName(), (Throwable) e);
762759
this.closed = true;
763-
IOUtils.close(archive, archiveException::addSuppressed);
764-
throw archiveException;
760+
throw IOUtils.closeQuietlySuppress(archive, e);
765761
}
766762
}
767763

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.commons.compress.archivers.tar;
21+
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.io.EOFException;
25+
import java.io.IOException;
26+
27+
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* Tests https://issues.apache.org/jira/browse/COMPRESS-714
32+
*/
33+
public class TarCompress714Test {
34+
35+
@Test
36+
public void testIllegalPosition() throws IOException {
37+
final byte[] data = { 46, 101, 97, 115, 97, 47, 120, -64, 72, 98, -18, 2, 53, 101, 112, 0, 0, 115, 8, 0, 0, 0, 112, 115, 40, 1, 0, 36, 2, 108, 0, -1,
38+
-1, 0, 0, 0, 74, 0, 0, 0, 0, -1, 0, 1, 67, -2, 8, 0, 0, 0, 0, -64, -1, -1, -34, 9, 0, -120, -120, -120, -120, -120, -120, -120, -120, -120,
39+
-120, -120, -120, -120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, -65, -1, -126, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128,
40+
-1, -1, -1, -1, 0, -122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128, 0, 0, 0, 0, 35, 0, 53, 0, 0, 0, 0, 0, 0, 32, 56, 0, 0, 0, 0, 0, 0, -120, 55, 55,
41+
55, 55, 55, 55, 55, 55, 55, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -105, 0, 0, 0, 49, 36, -1, -1, -1, -1, -1, -1, -126, 0, 0, 0, 0, 0,
42+
0, 0, 53, 0, 0, 0, -128, 0, 0, 0, 0, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 46, 101, 97, 115, 97, 47, 120, -64, 72, 98, -18, 2, 53,
43+
101, 112, 0, 0, 115, 8, 0, 0, 0, 112, 115, 40, 1, 0, 2, 108, 0, -1, -1, 0, 0, 0, 74, 0, 0, 0, 0, -1, 0, 1, 67, -2, 8, 0, 0, 0, 0, -64, -1, -1,
44+
-34, 9, 0, -120, -120, -120, -120, -120, -120, -120, -120, -120, -120, -120, -120, -120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 63, 0, 0,
45+
0, 0, 0, 0, -65, -1, -126, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128, -1, -1, -1, -1, 0, -122, 0, 0, 0, 0, 0, 0, -128, 0, 0, 0, 0, 35, 0, 53, 0,
46+
0, 0, 0, 0, 0, 32, 56, 0, 0, 0, 0, 0, 0, -120, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -105, 0, 0,
47+
0, 49, 36, -1, -1, -1, -1, -1, -1, -126, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, -128, 0, 0, 0, 0, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -65,
49+
-1, -126, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128, -1, -1, -1, -1, 0, -122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128, 0, 0, 0, 0, 35, 0, 53, 0, 0, 0,
50+
0, 0, 0, 32, 56, 0, 0, 0, 0, 0, 0, 0 };
51+
assertThrows(EOFException.class, () -> TarFile.builder().setChannel(new SeekableInMemoryByteChannel(data)).get());
52+
assertThrows(EOFException.class, () -> TarFile.builder().setByteArray(data).get());
53+
}
54+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.commons.compress.archivers.zip;
21+
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.io.EOFException;
25+
import java.io.IOException;
26+
27+
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* Tests https://issues.apache.org/jira/browse/COMPRESS-714
32+
*/
33+
public class ZipCompress714Test {
34+
35+
@Test
36+
public void testIllegalPosition() throws IOException {
37+
final byte[] data = { 80, 75, 5, 6, -127, 80, 75, 5, 6, 7, -127, -127, -127, 80, 74, 7, 8, -127, -127, -127, -127, -127 };
38+
assertThrows(EOFException.class, () -> ZipFile.builder().setChannel(new SeekableInMemoryByteChannel(data)).get());
39+
assertThrows(EOFException.class, () -> ZipFile.builder().setByteArray(data).get());
40+
}
41+
}

0 commit comments

Comments
 (0)