You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2026/05/01 07:26:37 UTC

(jena) 01/02: GH-3877: Add checks on bounds BinaryDataFileRandomAccess

This is an automated email from the ASF dual-hosted git repository.

afs pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git

commit bc37c7622f0b700c45b9cff62b066d359ccb6688
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Thu Apr 30 11:21:48 2026 +0100

    GH-3877: Add checks on bounds BinaryDataFileRandomAccess
---
 .../java/org/apache/jena/dboe/base/file/BinaryDataFile.java |  7 ++++---
 .../jena/dboe/base/file/BinaryDataFileRandomAccess.java     | 13 +++++++++----
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFile.java b/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFile.java
index 91e05018f1..d95408d137 100644
--- a/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFile.java
+++ b/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFile.java
@@ -26,9 +26,10 @@ import java.io.RandomAccessFile;
 import org.apache.jena.atlas.lib.Closeable;
 import org.apache.jena.atlas.lib.Sync;
 
-/** An append-only, read-anywhere, binary file.
- * A {@code BinaryDataFile} does not record the length and assumes the
- * entries are self-defining.
+/**
+ * An append-only, read-anywhere, binary file.
+ * A {@code BinaryDataFile} does not record the length of items in the file
+ * and assumes the entries are self-defining.
  *
  *  @see RandomAccessFile
  */
diff --git a/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFileRandomAccess.java b/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFileRandomAccess.java
index b3e2473a4e..87ab24a3b5 100644
--- a/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFileRandomAccess.java
+++ b/jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BinaryDataFileRandomAccess.java
@@ -42,7 +42,7 @@ public class BinaryDataFileRandomAccess implements BinaryDataFile {
     protected RandomAccessFile file;
     protected boolean readMode;
     protected long readPosition;
-    protected long writePosition;
+    protected long writePosition;       // This is the position of the end of the written area.
     private final String filename;
 
     public BinaryDataFileRandomAccess(String filename) {
@@ -93,10 +93,13 @@ public class BinaryDataFileRandomAccess implements BinaryDataFile {
         return x;
     }
 
-    // Move the RandomAccess file pointer.
+    // Move the RandomAccess file pointer
     private void seek(long posn) {
-        try { file.seek(posn); }
-        catch (IOException ex) { IO.exception(ex); }
+        try {
+            if ( posn > writePosition )
+                throw new RuntimeIOException("Seek beyond the end of the allocated file area [seek="+posn+",writePosition="+writePosition+"]");
+            file.seek(posn);
+        } catch (IOException ex) { IO.exception(ex); }
     }
 
     @Override
@@ -104,6 +107,8 @@ public class BinaryDataFileRandomAccess implements BinaryDataFile {
         checkOpen();
         switchToWriteMode();
         try {
+            if ( length > writePosition )
+                throw new RuntimeIOException("Truncate beyond the end of the allocated file area [length="+length+",writePosition="+writePosition+"]");
             file.setLength(length);
             writePosition = length;
         }