Author: lehmi
Date: Sun Sep 29 09:33:56 2024
New Revision: 1921019

URL: http://svn.apache.org/viewvc?rev=1921019&view=rev
Log:
PDFBOX-5880: don't restore invalid stream length values <= 0, mark stream 
length values <= as invalid

Modified:
    
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java

Modified: 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1921019&r1=1921018&r2=1921019&view=diff
==============================================================================
--- 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
 (original)
+++ 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
 Sun Sep 29 09:33:56 2024
@@ -1160,8 +1160,8 @@ public class COSParser extends BaseParse
             finally
             {
                 out.close();
-                // restore original (possibly incorrect) length
-                if (streamLengthObj != null)
+                // restore original (possibly incorrect) positive length value
+                if (streamLengthObj != null && streamLengthObj.longValue() > 0)
                 {
                     stream.setItem(COSName.LENGTH, streamLengthObj);
                 }
@@ -1320,30 +1320,34 @@ public class COSParser extends BaseParse
 
     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: " + originOffset + ", length: " 
+ streamLength
                     + ", expected end position: " + expectedEndOfStream);
+            return false;
         }
-        else
-        {
-            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: " + originOffset + ", 
length: " + streamLength
-                        + ", expected end position: " + expectedEndOfStream);
-            }
-            source.seek(originOffset);
+        source.seek(expectedEndOfStream);
+        skipSpaces();
+        boolean endStreamFound = isString(ENDSTREAM);
+        source.seek(originOffset);
+        if (!endStreamFound)
+        {
+            LOG.warn(
+                    "The end of the stream doesn't point to the correct 
offset, using workaround to read the stream, "
+                            + "stream start position: " + originOffset + ", 
length: " + streamLength
+                            + ", expected end position: " + 
expectedEndOfStream);
+            return false;
         }
-        return streamLengthIsValid;
+        return true;
     }
 
     /**


Reply via email to