JDK-8246729 : MappedByteBuffer.force() throws IndexOutOfBoundsException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 15
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2020-06-07
  • Updated: 2024-10-17
  • Resolved: 2020-06-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15 b27Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
openjdk version "15-ea" 2020-09-15
OpenJDK Runtime Environment (build 15-ea+26-1287)
OpenJDK 64-Bit Server VM (build 15-ea+26-1287, mixed mode, sharing)

A DESCRIPTION OF THE PROBLEM :
When a MappedByteBuffer limit is less than the capacity, calling force() throws an IndexOutOfBoundsException.

REGRESSION : Last worked in version 14.0.1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Map a file, write something to it, set a limit lower than the buffer capacity, and then call force().

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result is no exception.
ACTUAL -
An exception is thrown:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [0, 0 + 1000) out of bounds for length 4
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82)
        at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343)
        at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411)
        at java.base/java.nio.MappedByteBuffer.force(MappedByteBuffer.java:283)
        at java.base/java.nio.MappedByteBuffer.force(MappedByteBuffer.java:230)
        at test.MappedFileBug.main(MappedFileBug.java:15)

---------- BEGIN SOURCE ----------
package test;

import java.io.File;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import static java.nio.file.StandardOpenOption.*;

public class MappedFileBug {
    public static void main(String[] args) throws Exception {
        var file = File.createTempFile("test", null);
        var channel = FileChannel.open(file.toPath(), READ, WRITE);
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
        buffer.putInt(1234);
        buffer.limit(4);
        buffer.force();
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Set the limit to the capacity, call force, and then set the limit back. This might cause more data to be forced than desired.

FREQUENCY : always



Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/b87b8ca419cb User: bpb Date: 2020-06-08 20:16:33 +0000
08-06-2020

Yes, this a regressions since the integration of JEP 383. The no-arg force method should be calling the 2-arg force with the limit rather than the capacity as the length.
08-06-2020

The observation on Windows 10: JDK 15: Fail ILW=HLM=P3
08-06-2020