Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the upper limit of WARC content length to half of Integer.MAX_VALUE #496

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static String getWarcResponseMimeType(final byte[] contents) {
* @throws IOException if there is an issue
*/
public static byte[] getContent(final WARCRecord record) throws IOException {
int len = (int) record.getHeader().getContentLength();
int len = Math.toIntExact(record.getHeader().getContentLength());

// If we have a corrupt record, quit and move on.
if (len < 0) {
Expand Down
24 changes: 18 additions & 6 deletions src/main/scala/io/archivesunleashed/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ package object archivesunleashed {
/** Loads records from either WARCs or ARCs. */
object RecordLoader {

/**
* Used to skip warc records with content length longer than 1073741823 bytes (~1.07 GB).
* This is done such that when the content of the record is massaged into a String, it would not
* overflow the String's internal byte array, which is double the length of the content byte array.
*
* @see ArchiveRecord#getContentString
*/
private val MAX_ALLOWABLE_WARC_CONTENT_LENGTH = (Int.MaxValue >> 1).toLong

/** Gets all non-empty archive files.
*
* @param dir the path to the directory containing archive files
Expand Down Expand Up @@ -107,12 +116,15 @@ package object archivesunleashed {
classOf[LongWritable],
classOf[ArchiveRecordWritable]
)
.filter(r =>
(r._2.getFormat == ArchiveFormat.ARC) ||
((r._2.getFormat == ArchiveFormat.WARC) && r._2.getRecord.getHeader
.getHeaderValue("WARC-Type")
.equals("response"))
)
.filter { case (_, arw) =>
arw.getFormat match {
case ArchiveFormat.ARC => true
case ArchiveFormat.WARC =>
arw.getRecord.getHeader.getHeaderValue("WARC-Type").equals("response") &&
arw.getRecord.getHeader.getContentLength <= MAX_ALLOWABLE_WARC_CONTENT_LENGTH
case _ => false
}
}
.map(r => new ArchiveRecordImpl(new SerializableWritable(r._2)))
}
}
Expand Down