Skip to content

Commit 05a7fb2

Browse files
authored
[COMPRESS-712] Unsanitized read causes IndexOutOfBoundsException in (apache#749)
DumpArchiveInputStream.java:359 - Throw a DumpArchiveException instead of a IndexOutOfBoundsException - Also fix formatting in org.apache.commons.compress.archivers.dump.DumpArchiveUtil.decode(ZipEncoding, byte[], int, int)
1 parent e78e02c commit 05a7fb2

3 files changed

Lines changed: 188 additions & 8 deletions

File tree

src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.util.Arrays;
25-
import java.util.BitSet;
2625
import java.util.HashMap;
26+
import java.util.HashSet;
2727
import java.util.Map;
2828
import java.util.PriorityQueue;
2929
import java.util.Queue;
30+
import java.util.Set;
3031
import java.util.Stack;
3132

3233
import org.apache.commons.compress.MemoryLimitException;
@@ -353,18 +354,19 @@ private String getPath(final DumpArchiveEntry entry) throws DumpArchiveException
353354
// build the stack of elements. It's possible that we're
354355
// still missing an intermediate value and if so we
355356
final Stack<String> elements = new Stack<>();
356-
final BitSet visited = new BitSet();
357+
// INO entries are unsigned (uint32_t)
358+
final Set<Integer> visited = new HashSet<>();
357359
Dirent dirent = null;
358360
for (int i = entry.getIno();; i = dirent.getParentIno()) {
359361
if (!names.containsKey(i)) {
360362
elements.clear();
361363
break;
362364
}
363-
if (visited.get(i)) {
365+
if (visited.contains(i)) {
364366
throw new DumpArchiveException("Duplicate node " + i);
365367
}
366368
dirent = names.get(i);
367-
visited.set(i);
369+
visited.add(i);
368370
elements.push(dirent.getName());
369371
if (dirent.getIno() == dirent.getParentIno()) {
370372
break;

src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public static long convert64(final byte[] buffer, final int offset) {
8181
* Decodes a byte array to a string.
8282
*/
8383
static String decode(final ZipEncoding encoding, final byte[] b, final int offset, final int len) throws IOException {
84-
if (offset > offset + len) {
85-
throw new ArchiveException("Invalid offset/length combination");
86-
}
87-
return encoding.decode(Arrays.copyOfRange(b, offset, offset + len));
84+
if (offset > offset + len) {
85+
throw new ArchiveException("Invalid offset/length combination");
86+
}
87+
return encoding.decode(Arrays.copyOfRange(b, offset, offset + len));
8888
}
8989

9090
/**

0 commit comments

Comments
 (0)