Skip to content

Commit 6594a2d

Browse files
committed
port some fixes from Commons Compress
1 parent 4e5b94a commit 6594a2d

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/main/org/apache/tools/tar/TarInputStream.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,18 +438,21 @@ Map<String, String> parsePaxHeaders(InputStream i) throws IOException {
438438
String keyword = coll.toString("UTF-8");
439439
// Get rest of entry
440440
final int restLen = len - read;
441-
byte[] rest = new byte[restLen];
441+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
442442
int got = 0;
443443
while (got < restLen && (ch = i.read()) != -1) {
444-
rest[got++] = (byte) ch;
444+
bos.write((byte) ch);
445+
got++;
445446
}
447+
bos.close();
446448
if (got != restLen) {
447449
throw new IOException("Failed to read "
448450
+ "Paxheader. Expected "
449451
+ restLen
450452
+ " bytes, read "
451453
+ got);
452454
}
455+
byte[] rest = bos.toByteArray();
453456
// Drop trailing NL
454457
String value = new String(rest, 0,
455458
restLen - 1, "UTF-8");

src/main/org/apache/tools/zip/AsiExtraField.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,18 @@ public void parseFromLocalFileData(byte[] data, int offset, int length)
307307

308308
int newMode = ZipShort.getValue(tmp, 0);
309309
// CheckStyle:MagicNumber OFF
310-
byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
310+
final int linkArrayLength = (int) ZipLong.getValue(tmp, 2);
311+
if (linkArrayLength < 0 || linkArrayLength > tmp.length - 10) {
312+
throw new ZipException("Bad symbolic link name length " + linkArrayLength
313+
+ " in ASI extra field");
314+
}
311315
uid = ZipShort.getValue(tmp, 6);
312316
gid = ZipShort.getValue(tmp, 8);
313-
314-
if (linkArray.length == 0) {
317+
if (linkArrayLength == 0) {
315318
link = "";
316319
} else {
317-
System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
320+
final byte[] linkArray = new byte[linkArrayLength];
321+
System.arraycopy(tmp, 10, linkArray, 0, linkArrayLength);
318322
link = new String(linkArray); // Uses default charset - see class Javadoc
319323
}
320324
// CheckStyle:MagicNumber ON

src/main/org/apache/tools/zip/ZipFile.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
538538
ze.setExternalAttributes(ZipLong.getValue(CFH_BUF, off));
539539
off += WORD;
540540

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

553+
if (archive.length() - archive.getFilePointer() < extraLen) {
554+
throw new EOFException();
555+
}
550556
final byte[] cdExtraData = new byte[extraLen];
551557
archive.readFully(cdExtraData);
552558
ze.setCentralDirectoryExtra(cdExtraData);
553559

554560
setSizesAndOffsetFromZip64Extra(ze, offset, diskStart);
555561

562+
if (archive.length() - archive.getFilePointer() < commentLen) {
563+
throw new EOFException();
564+
}
556565
final byte[] comment = new byte[commentLen];
557566
archive.readFully(comment);
558567
ze.setComment(entryEncoding.decode(comment));
@@ -878,9 +887,18 @@ private void resolveLocalFileHeaderData(final Map<ZipEntry, NameAndComment>
878887
}
879888
lenToSkip -= skipped;
880889
}
890+
if (archive.length() - archive.getFilePointer() < extraFieldLen) {
891+
throw new EOFException();
892+
}
881893
final byte[] localExtraData = new byte[extraFieldLen];
882894
archive.readFully(localExtraData);
883-
ze.setExtra(localExtraData);
895+
try {
896+
ze.setExtra(localExtraData);
897+
} catch (RuntimeException ex) {
898+
final ZipException z = new ZipException("Invalid extra data in entry " + ze.getName());
899+
z.initCause(ex);
900+
throw z;
901+
}
884902
offsetEntry.dataOffset = offset + LFH_OFFSET_FOR_FILENAME_LENGTH
885903
+ SHORT + SHORT + fileNameLen + extraFieldLen;
886904

0 commit comments

Comments
 (0)