diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java b/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java index 679f25288f8..ac2f86e70e4 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java @@ -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) @@ -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