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 2020/02/08 19:48:35 UTC

svn commit: r1873806 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/io/ScratchFileBuffer.java test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java

Author: tilman
Date: Sat Feb  8 19:48:35 2020
New Revision: 1873806

URL: http://svn.apache.org/viewvc?rev=1873806&view=rev
Log:
PDFBOX-4756: fix bug in seek() (wrong position and EOF), by Petr Slaby

Added:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java?rev=1873806&r1=1873805&r2=1873806&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java Sat Feb  8 19:48:35 2020
@@ -324,6 +324,10 @@ class ScratchFileBuffer implements Rando
             }
             
             int newPagePosition = (int) (seekToPosition / pageSize);
+            if (seekToPosition % pageSize == 0 && seekToPosition == size)
+            {
+                newPagePosition--; // PDFBOX-4756: Prevent seeking a non-yet-existent page...
+            }
             
             currentPage = pageHandler.readPage(pageIndexes[newPagePosition]);
             currentPagePositionInPageIndexes = newPagePosition;

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java?rev=1873806&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java Sat Feb  8 19:48:35 2020
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.io;
+
+import java.io.IOException;
+import org.junit.Assert;
+
+import org.junit.Test;
+
+/**
+ * Regression test to check the known bugs in {@link ScratchFileBuffer}.
+ *
+ * @author Kühn & Weyh Software GmbH
+ */
+public class ScratchFileBufferTest
+{
+
+    private static final int PAGE_SIZE = 4096;
+    private static final int NUM_ITERATIONS = 3;
+
+    /**
+     * PDFBOX-4756: test positions are correct when seeking and that no EOFException is thrown in
+     * ScratchFileBuffer.seek() beyond last page.
+     *
+     * @throws IOException
+     */
+    @Test
+    public void testEOFBugInSeek() throws IOException
+    {
+        try (ScratchFile scratchFile = new ScratchFile(MemoryUsageSetting.setupTempFileOnly()))
+        {
+            ScratchFileBuffer scratchFileBuffer = new ScratchFileBuffer(scratchFile);
+            byte[] bytes = new byte[PAGE_SIZE];
+            for (int i = 0; i < NUM_ITERATIONS; i++)
+            {
+                long p0 = scratchFileBuffer.getPosition();
+                scratchFileBuffer.write(bytes);
+                long p1 = scratchFileBuffer.getPosition();
+                Assert.assertEquals(PAGE_SIZE, p1 - p0);
+                scratchFileBuffer.write(bytes);
+                long p2 = scratchFileBuffer.getPosition();
+                Assert.assertEquals(PAGE_SIZE, p2 - p1);
+                scratchFileBuffer.seek(0);
+                scratchFileBuffer.seek(i * 2 * PAGE_SIZE);
+            }
+        }
+    }
+}

Propchange: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/ScratchFileBufferTest.java
------------------------------------------------------------------------------
    svn:eol-style = native