Skip to content

Commit

Permalink
port some fixes from Commons Compress
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed Jul 10, 2021
1 parent 4e5b94a commit 6594a2d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/main/org/apache/tools/tar/TarInputStream.java
Expand Up @@ -438,18 +438,21 @@ Map<String, String> parsePaxHeaders(InputStream i) throws IOException {
String keyword = coll.toString("UTF-8");
// Get rest of entry
final int restLen = len - read;
byte[] rest = new byte[restLen];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int got = 0;
while (got < restLen && (ch = i.read()) != -1) {
rest[got++] = (byte) ch;
bos.write((byte) ch);
got++;
}
bos.close();
if (got != restLen) {
throw new IOException("Failed to read "
+ "Paxheader. Expected "
+ restLen
+ " bytes, read "
+ got);
}
byte[] rest = bos.toByteArray();
// Drop trailing NL
String value = new String(rest, 0,
restLen - 1, "UTF-8");
Expand Down
12 changes: 8 additions & 4 deletions src/main/org/apache/tools/zip/AsiExtraField.java
Expand Up @@ -307,14 +307,18 @@ public void parseFromLocalFileData(byte[] data, int offset, int length)

int newMode = ZipShort.getValue(tmp, 0);
// CheckStyle:MagicNumber OFF
byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
final int linkArrayLength = (int) ZipLong.getValue(tmp, 2);
if (linkArrayLength < 0 || linkArrayLength > tmp.length - 10) {
throw new ZipException("Bad symbolic link name length " + linkArrayLength
+ " in ASI extra field");
}
uid = ZipShort.getValue(tmp, 6);
gid = ZipShort.getValue(tmp, 8);

if (linkArray.length == 0) {
if (linkArrayLength == 0) {
link = "";
} else {
System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
final byte[] linkArray = new byte[linkArrayLength];
System.arraycopy(tmp, 10, linkArray, 0, linkArrayLength);
link = new String(linkArray); // Uses default charset - see class Javadoc
}
// CheckStyle:MagicNumber ON
Expand Down
20 changes: 19 additions & 1 deletion src/main/org/apache/tools/zip/ZipFile.java
Expand Up @@ -538,6 +538,9 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
ze.setExternalAttributes(ZipLong.getValue(CFH_BUF, off));
off += WORD;

if (archive.length() - archive.getFilePointer() < fileNameLen) {
throw new EOFException();
}
final byte[] fileName = new byte[fileNameLen];
archive.readFully(fileName);
ze.setName(entryEncoding.decode(fileName), fileName);
Expand All @@ -547,12 +550,18 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
// data offset will be filled later
entries.add(ze);

if (archive.length() - archive.getFilePointer() < extraLen) {
throw new EOFException();
}
final byte[] cdExtraData = new byte[extraLen];
archive.readFully(cdExtraData);
ze.setCentralDirectoryExtra(cdExtraData);

setSizesAndOffsetFromZip64Extra(ze, offset, diskStart);

if (archive.length() - archive.getFilePointer() < commentLen) {
throw new EOFException();
}
final byte[] comment = new byte[commentLen];
archive.readFully(comment);
ze.setComment(entryEncoding.decode(comment));
Expand Down Expand Up @@ -878,9 +887,18 @@ private void resolveLocalFileHeaderData(final Map<ZipEntry, NameAndComment>
}
lenToSkip -= skipped;
}
if (archive.length() - archive.getFilePointer() < extraFieldLen) {
throw new EOFException();
}
final byte[] localExtraData = new byte[extraFieldLen];
archive.readFully(localExtraData);
ze.setExtra(localExtraData);
try {
ze.setExtra(localExtraData);
} catch (RuntimeException ex) {
final ZipException z = new ZipException("Invalid extra data in entry " + ze.getName());
z.initCause(ex);
throw z;
}
offsetEntry.dataOffset = offset + LFH_OFFSET_FOR_FILENAME_LENGTH
+ SHORT + SHORT + fileNameLen + extraFieldLen;

Expand Down

0 comments on commit 6594a2d

Please sign in to comment.