You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2021/04/09 06:03:50 UTC

svn commit: r1888543 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/io/RandomAccessReadBuffer.java test/java/org/apache/pdfbox/io/RandomAccessReadBufferTest.java

Author: tilman
Date: Fri Apr  9 06:03:49 2021
New Revision: 1888543

URL: http://svn.apache.org/viewvc?rev=1888543&view=rev
Log:
PDFBOX-5158: avoid endless loop on FileInputStreams that have a 4096 multiple length + test

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadBuffer.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferTest.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadBuffer.java?rev=1888543&r1=1888542&r2=1888543&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadBuffer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadBuffer.java Fri Apr  9 06:03:49 2021
@@ -105,7 +105,8 @@ public class RandomAccessReadBuffer impl
         int remainingBytes = chunkSize;
         int offset = 0;
         byte[] eofCheck = new byte[1];
-        while ((bytesRead = input.read(currentBuffer.array(), offset, remainingBytes)) > -1)
+        while (remainingBytes > 0 &&
+                (bytesRead = input.read(currentBuffer.array(), offset, remainingBytes)) > -1)
         {
             remainingBytes -= bytesRead;
             offset += bytesRead;

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferTest.java?rev=1888543&r1=1888542&r2=1888543&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferTest.java Fri Apr  9 06:03:49 2021
@@ -24,7 +24,10 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 import org.junit.jupiter.api.Assertions;
 
@@ -209,4 +212,27 @@ class RandomAccessReadBufferTest
             randomAccessSource.close();
         }
     }
+
+    /**
+     * PDFBOX-5158: endless loop reading a stream of a multiple of 4096 bytes from a
+     * FileInputStream. Test does not fail with a ByteArrayInputStream, so we need to create a temp
+     * file.
+     *
+     * @throws IOException
+     */
+    @Test
+    void testPDFBOX5158() throws IOException
+    {
+        Path path = Files.createTempFile("len4096", ".pdf");
+        try (OutputStream os = Files.newOutputStream(path))
+        {
+            os.write(new byte[4096]);
+        }
+        assertEquals(4096, path.toFile().length());
+        try (RandomAccessRead rar = new RandomAccessReadBuffer(Files.newInputStream(path)))
+        {
+            assertEquals(0, rar.read());
+        }
+        Files.delete(path);
+    }
 }