Repository: flink Updated Branches: refs/heads/master 7978a1744 -> 88737cf9f
[FLINK-7516][memory] do not allow copies into a read-only ByteBuffer [FLINK-7516][memory] address PR comments This closes #4593. Project: http://git-wip-us.apache.org/repos/asf/flink/repo Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/fbc12630 Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/fbc12630 Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/fbc12630 Branch: refs/heads/master Commit: fbc12630e6439b02546398b04b60f2c764ccba71 Parents: f1c4eb6 Author: Nico Kruber <[email protected]> Authored: Tue Aug 22 18:33:55 2017 +0200 Committer: Till Rohrmann <[email protected]> Committed: Mon Nov 6 16:19:12 2017 +0100 ---------------------------------------------------------------------- .../flink/core/memory/HybridMemorySegment.java | 5 ++++ .../apache/flink/core/memory/MemorySegment.java | 2 ++ .../core/memory/MemorySegmentTestBase.java | 31 ++++++++++++++++++++ 3 files changed, 38 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flink/blob/fbc12630/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java ---------------------------------------------------------------------- diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java b/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java index a2cf3d3..13a2644 100644 --- a/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java +++ b/flink-core/src/main/java/org/apache/flink/core/memory/HybridMemorySegment.java @@ -27,6 +27,7 @@ import java.lang.reflect.Field; import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; +import java.nio.ReadOnlyBufferException; /** * This class represents a piece of memory managed by Flink. The memory can be on-heap or off-heap, @@ -318,6 +319,10 @@ public final class HybridMemorySegment extends MemorySegment { } if (target.isDirect()) { + if (target.isReadOnly()) { + throw new ReadOnlyBufferException(); + } + // copy to the target memory directly final long targetPointer = getAddress(target) + targetOffset; final long sourcePointer = address + offset; http://git-wip-us.apache.org/repos/asf/flink/blob/fbc12630/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java ---------------------------------------------------------------------- diff --git a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java index 1f3804f..3ad1c63 100644 --- a/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java +++ b/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java @@ -25,6 +25,7 @@ import java.io.DataOutput; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.ReadOnlyBufferException; /** * This class represents a piece of memory managed by Flink. @@ -1177,6 +1178,7 @@ public abstract class MemorySegment { * @throws IndexOutOfBoundsException If the offset is invalid, or this segment does not * contain the given number of bytes (starting from offset), or the target byte buffer does * not have enough space for the bytes. + * @throws ReadOnlyBufferException If the target buffer is read-only. */ public abstract void get(int offset, ByteBuffer target, int numBytes); http://git-wip-us.apache.org/repos/asf/flink/blob/fbc12630/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentTestBase.java ---------------------------------------------------------------------- diff --git a/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentTestBase.java b/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentTestBase.java index 62e0a56..fb28948 100644 --- a/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentTestBase.java +++ b/flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentTestBase.java @@ -32,6 +32,7 @@ import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.ReadOnlyBufferException; import java.util.Arrays; import java.util.Collection; import java.util.Random; @@ -1914,6 +1915,36 @@ public abstract class MemorySegmentTestBase { assertArrayEquals(bytes, result); } + @Test(expected = ReadOnlyBufferException.class) + public void testHeapByteBufferGetReadOnly() { + testByteBufferGetReadOnly(false); + } + + @Test(expected = ReadOnlyBufferException.class) + public void testOffHeapByteBufferGetReadOnly() { + testByteBufferGetReadOnly(true); + } + + /** + * Tries to write into a {@link ByteBuffer} instance which is read-only. This should fail with a + * {@link ReadOnlyBufferException}. + * + * @param directBuffer + * whether the {@link ByteBuffer} instance should be a direct byte buffer or not + * + * @throws ReadOnlyBufferException + * expected exception due to writing to a read-only buffer + */ + private void testByteBufferGetReadOnly(boolean directBuffer) throws ReadOnlyBufferException { + MemorySegment seg = createSegment(pageSize); + + ByteBuffer target = (directBuffer ? + ByteBuffer.allocateDirect(pageSize) : + ByteBuffer.allocate(pageSize)).asReadOnlyBuffer(); + + seg.get(0, target, pageSize); + } + @Test public void testByteBufferPut() { testByteBufferPut(false);
