|
| 1 | +From 1fa58fb3a05631cb019340ff429de2c85a214b29 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Stefan Bodewig <bodewig@apache.org> |
| 3 | +Date: Sat, 10 Jul 2021 11:10:12 +0200 |
| 4 | +Subject: [PATCH] port some fixes from Commons Compress |
| 5 | + |
| 6 | +--- |
| 7 | + .../org/apache/tools/tar/TarInputStream.java | 7 +++++-- |
| 8 | + .../org/apache/tools/zip/AsiExtraField.java | 12 +++++++---- |
| 9 | + src/main/org/apache/tools/zip/ZipFile.java | 20 ++++++++++++++++++- |
| 10 | + 3 files changed, 32 insertions(+), 7 deletions(-) |
| 11 | + |
| 12 | +diff --git a/src/main/org/apache/tools/tar/TarInputStream.java b/src/main/org/apache/tools/tar/TarInputStream.java |
| 13 | +index 0477d5c..71e4cc0 100644 |
| 14 | +--- a/src/main/org/apache/tools/tar/TarInputStream.java |
| 15 | ++++ b/src/main/org/apache/tools/tar/TarInputStream.java |
| 16 | +@@ -436,11 +436,13 @@ public class TarInputStream extends FilterInputStream { |
| 17 | + String keyword = coll.toString("UTF-8"); |
| 18 | + // Get rest of entry |
| 19 | + final int restLen = len - read; |
| 20 | +- byte[] rest = new byte[restLen]; |
| 21 | ++ ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 22 | + int got = 0; |
| 23 | + while (got < restLen && (ch = i.read()) != -1) { |
| 24 | +- rest[got++] = (byte) ch; |
| 25 | ++ bos.write((byte) ch); |
| 26 | ++ got++; |
| 27 | + } |
| 28 | ++ bos.close(); |
| 29 | + if (got != restLen) { |
| 30 | + throw new IOException("Failed to read " |
| 31 | + + "Paxheader. Expected " |
| 32 | +@@ -448,6 +450,7 @@ public class TarInputStream extends FilterInputStream { |
| 33 | + + " bytes, read " |
| 34 | + + got); |
| 35 | + } |
| 36 | ++ byte[] rest = bos.toByteArray(); |
| 37 | + // Drop trailing NL |
| 38 | + String value = new String(rest, 0, |
| 39 | + restLen - 1, StandardCharsets.UTF_8); |
| 40 | +diff --git a/src/main/org/apache/tools/zip/AsiExtraField.java b/src/main/org/apache/tools/zip/AsiExtraField.java |
| 41 | +index 8afddb5..fdd81c6 100644 |
| 42 | +--- a/src/main/org/apache/tools/zip/AsiExtraField.java |
| 43 | ++++ b/src/main/org/apache/tools/zip/AsiExtraField.java |
| 44 | +@@ -307,14 +307,18 @@ public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable { |
| 45 | + |
| 46 | + int newMode = ZipShort.getValue(tmp, 0); |
| 47 | + // CheckStyle:MagicNumber OFF |
| 48 | +- byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)]; |
| 49 | ++ final int linkArrayLength = (int) ZipLong.getValue(tmp, 2); |
| 50 | ++ if (linkArrayLength < 0 || linkArrayLength > tmp.length - 10) { |
| 51 | ++ throw new ZipException("Bad symbolic link name length " + linkArrayLength |
| 52 | ++ + " in ASI extra field"); |
| 53 | ++ } |
| 54 | + uid = ZipShort.getValue(tmp, 6); |
| 55 | + gid = ZipShort.getValue(tmp, 8); |
| 56 | +- |
| 57 | +- if (linkArray.length == 0) { |
| 58 | ++ if (linkArrayLength == 0) { |
| 59 | + link = ""; |
| 60 | + } else { |
| 61 | +- System.arraycopy(tmp, 10, linkArray, 0, linkArray.length); |
| 62 | ++ final byte[] linkArray = new byte[linkArrayLength]; |
| 63 | ++ System.arraycopy(tmp, 10, linkArray, 0, linkArrayLength); |
| 64 | + link = new String(linkArray); // Uses default charset - see class Javadoc |
| 65 | + } |
| 66 | + // CheckStyle:MagicNumber ON |
| 67 | +diff --git a/src/main/org/apache/tools/zip/ZipFile.java b/src/main/org/apache/tools/zip/ZipFile.java |
| 68 | +index dfb6bcf..8806ae7 100644 |
| 69 | +--- a/src/main/org/apache/tools/zip/ZipFile.java |
| 70 | ++++ b/src/main/org/apache/tools/zip/ZipFile.java |
| 71 | +@@ -541,6 +541,9 @@ public class ZipFile implements Closeable { |
| 72 | + ze.setExternalAttributes(ZipLong.getValue(CFH_BUF, off)); |
| 73 | + off += WORD; |
| 74 | + |
| 75 | ++ if (archive.length() - archive.getFilePointer() < fileNameLen) { |
| 76 | ++ throw new EOFException(); |
| 77 | ++ } |
| 78 | + final byte[] fileName = new byte[fileNameLen]; |
| 79 | + archive.readFully(fileName); |
| 80 | + ze.setName(entryEncoding.decode(fileName), fileName); |
| 81 | +@@ -550,12 +553,18 @@ public class ZipFile implements Closeable { |
| 82 | + // data offset will be filled later |
| 83 | + entries.add(ze); |
| 84 | + |
| 85 | ++ if (archive.length() - archive.getFilePointer() < extraLen) { |
| 86 | ++ throw new EOFException(); |
| 87 | ++ } |
| 88 | + final byte[] cdExtraData = new byte[extraLen]; |
| 89 | + archive.readFully(cdExtraData); |
| 90 | + ze.setCentralDirectoryExtra(cdExtraData); |
| 91 | + |
| 92 | + setSizesAndOffsetFromZip64Extra(ze, offset, diskStart); |
| 93 | + |
| 94 | ++ if (archive.length() - archive.getFilePointer() < commentLen) { |
| 95 | ++ throw new EOFException(); |
| 96 | ++ } |
| 97 | + final byte[] comment = new byte[commentLen]; |
| 98 | + archive.readFully(comment); |
| 99 | + ze.setComment(entryEncoding.decode(comment)); |
| 100 | +@@ -881,9 +890,18 @@ public class ZipFile implements Closeable { |
| 101 | + } |
| 102 | + lenToSkip -= skipped; |
| 103 | + } |
| 104 | ++ if (archive.length() - archive.getFilePointer() < extraFieldLen) { |
| 105 | ++ throw new EOFException(); |
| 106 | ++ } |
| 107 | + final byte[] localExtraData = new byte[extraFieldLen]; |
| 108 | + archive.readFully(localExtraData); |
| 109 | +- ze.setExtra(localExtraData); |
| 110 | ++ try { |
| 111 | ++ ze.setExtra(localExtraData); |
| 112 | ++ } catch (RuntimeException ex) { |
| 113 | ++ final ZipException z = new ZipException("Invalid extra data in entry " + ze.getName()); |
| 114 | ++ z.initCause(ex); |
| 115 | ++ throw z; |
| 116 | ++ } |
| 117 | + offsetEntry.dataOffset = offset + LFH_OFFSET_FOR_FILENAME_LENGTH |
| 118 | + + SHORT + SHORT + fileNameLen + extraFieldLen; |
| 119 | + |
| 120 | +-- |
| 121 | +2.34.1 |
| 122 | + |
0 commit comments