Skip to content

Commit aa9ec68

Browse files
committed
Merge branch '1.9.x'
2 parents 7f9c5dd + 6594a2d commit aa9ec68

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
@@ -436,18 +436,21 @@ Map<String, String> parsePaxHeaders(InputStream i) throws IOException {
436436
String keyword = coll.toString("UTF-8");
437437
// Get rest of entry
438438
final int restLen = len - read;
439-
byte[] rest = new byte[restLen];
439+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
440440
int got = 0;
441441
while (got < restLen && (ch = i.read()) != -1) {
442-
rest[got++] = (byte) ch;
442+
bos.write((byte) ch);
443+
got++;
443444
}
445+
bos.close();
444446
if (got != restLen) {
445447
throw new IOException("Failed to read "
446448
+ "Paxheader. Expected "
447449
+ restLen
448450
+ " bytes, read "
449451
+ got);
450452
}
453+
byte[] rest = bos.toByteArray();
451454
// Drop trailing NL
452455
String value = new String(rest, 0,
453456
restLen - 1, StandardCharsets.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
@@ -541,6 +541,9 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
541541
ze.setExternalAttributes(ZipLong.getValue(CFH_BUF, off));
542542
off += WORD;
543543

544+
if (archive.length() - archive.getFilePointer() < fileNameLen) {
545+
throw new EOFException();
546+
}
544547
final byte[] fileName = new byte[fileNameLen];
545548
archive.readFully(fileName);
546549
ze.setName(entryEncoding.decode(fileName), fileName);
@@ -550,12 +553,18 @@ private Map<ZipEntry, NameAndComment> populateFromCentralDirectory()
550553
// data offset will be filled later
551554
entries.add(ze);
552555

556+
if (archive.length() - archive.getFilePointer() < extraLen) {
557+
throw new EOFException();
558+
}
553559
final byte[] cdExtraData = new byte[extraLen];
554560
archive.readFully(cdExtraData);
555561
ze.setCentralDirectoryExtra(cdExtraData);
556562

557563
setSizesAndOffsetFromZip64Extra(ze, offset, diskStart);
558564

565+
if (archive.length() - archive.getFilePointer() < commentLen) {
566+
throw new EOFException();
567+
}
559568
final byte[] comment = new byte[commentLen];
560569
archive.readFully(comment);
561570
ze.setComment(entryEncoding.decode(comment));
@@ -881,9 +890,18 @@ private void resolveLocalFileHeaderData(final Map<ZipEntry, NameAndComment>
881890
}
882891
lenToSkip -= skipped;
883892
}
893+
if (archive.length() - archive.getFilePointer() < extraFieldLen) {
894+
throw new EOFException();
895+
}
884896
final byte[] localExtraData = new byte[extraFieldLen];
885897
archive.readFully(localExtraData);
886-
ze.setExtra(localExtraData);
898+
try {
899+
ze.setExtra(localExtraData);
900+
} catch (RuntimeException ex) {
901+
final ZipException z = new ZipException("Invalid extra data in entry " + ze.getName());
902+
z.initCause(ex);
903+
throw z;
904+
}
887905
offsetEntry.dataOffset = offset + LFH_OFFSET_FOR_FILENAME_LENGTH
888906
+ SHORT + SHORT + fileNameLen + extraFieldLen;
889907

0 commit comments

Comments
 (0)