Skip to content

Commit

Permalink
PDFBOX-5880: set missing/replace invalid stream length
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1921020 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed Sep 29, 2024
1 parent dca31d2 commit 72e2bb3
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,10 @@ protected COSStream parseCOSStream(COSDictionary dic) throws IOException
else
{
streamLength = readUntilEndStream(new EndstreamFilterStream());
if (streamLengthObj == null || streamLengthObj.longValue() != streamLength)
{
dic.setLong(COSName.LENGTH, streamLength);
}
}
String endStream = readString();
if (endStream.equals("endobj") && isLenient)
Expand Down Expand Up @@ -882,30 +886,33 @@ private long readUntilEndStream(final EndstreamFilterStream out) throws IOExcept

private boolean validateStreamLength(long streamLength) throws IOException
{
boolean streamLengthIsValid = true;
long originOffset = source.getPosition();
if (streamLength <= 0)
{
LOG.warn("Invalid stream length: " + streamLength + ", stream start position: "
+ originOffset);
return false;
}
long expectedEndOfStream = originOffset + streamLength;
if (expectedEndOfStream > fileLen)
{
streamLengthIsValid = false;
LOG.warn(
"The end of the stream is out of range, using workaround to read the stream, stream start position: {}, length: {}, expected end position: {}",
originOffset, streamLength, expectedEndOfStream);
return false;
}
else
source.seek(expectedEndOfStream);
skipSpaces();
boolean endStreamFound = isString(ENDSTREAM);
source.seek(originOffset);
if (!endStreamFound)
{
source.seek(expectedEndOfStream);
skipSpaces();
if (!isString(ENDSTREAM))
{
streamLengthIsValid = false;
LOG.warn(
"The end of the stream doesn't point to the correct offset, using workaround to read the stream, stream start position: {}, length: {}, expected end position: {}",
originOffset, streamLength, expectedEndOfStream);
}
source.seek(originOffset);
LOG.warn(
"The end of the stream doesn't point to the correct offset, using workaround to read the stream, stream start position: {}, length: {}, expected end position: {}",
originOffset, streamLength, expectedEndOfStream);
return false;
}
return streamLengthIsValid;
return true;
}

protected BruteForceParser getBruteForceParser() throws IOException
Expand Down

0 comments on commit 72e2bb3

Please sign in to comment.