You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ra...@apache.org on 2019/02/25 06:08:30 UTC

[arrow] branch master updated: ARROW-4669: [Java] Add validity checks to slice

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

ravindra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 9ea4d3c  ARROW-4669: [Java] Add validity checks to slice
9ea4d3c is described below

commit 9ea4d3c17c171c7ca66cee8d5c2e0e271b25685c
Author: Micah Kornfield <em...@gmail.com>
AuthorDate: Mon Feb 25 11:38:15 2019 +0530

    ARROW-4669: [Java] Add validity checks to slice
    
    Author: Micah Kornfield <em...@gmail.com>
    
    Closes #3740 from emkornfield/fix_index_out_of_bounds and squashes the following commits:
    
    1fb4f91f <Micah Kornfield> fix formatting issues
    d6f822d7 <Micah Kornfield> some small cleanup
    1b73657e <Micah Kornfield> ARROW-4669:  Add validity checks to slice
---
 .../src/main/java/io/netty/buffer/ArrowBuf.java    |  3 ++
 .../test/java/io/netty/buffer/TestArrowBuf.java    | 61 ++++++++++++++++++----
 2 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/java/memory/src/main/java/io/netty/buffer/ArrowBuf.java b/java/memory/src/main/java/io/netty/buffer/ArrowBuf.java
index 5d37df5..607f8ca 100644
--- a/java/memory/src/main/java/io/netty/buffer/ArrowBuf.java
+++ b/java/memory/src/main/java/io/netty/buffer/ArrowBuf.java
@@ -366,6 +366,9 @@ public final class ArrowBuf extends AbstractByteBuf implements AutoCloseable {
       return this;
     }
 
+    Preconditions.checkPositionIndex(index, this.length);
+    Preconditions.checkPositionIndex(index + length, this.length);
+
     /*
      * Re the behavior of reference counting, see http://netty.io/wiki/reference-counted-objects
      * .html#wiki-h3-5, which
diff --git a/java/memory/src/test/java/io/netty/buffer/TestArrowBuf.java b/java/memory/src/test/java/io/netty/buffer/TestArrowBuf.java
index 3171a22..feb7c44 100644
--- a/java/memory/src/test/java/io/netty/buffer/TestArrowBuf.java
+++ b/java/memory/src/test/java/io/netty/buffer/TestArrowBuf.java
@@ -17,11 +17,13 @@
 
 package io.netty.buffer;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
 
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 
+import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -31,19 +33,60 @@ public class TestArrowBuf {
 
   private static final int MAX_ALLOCATION = 8 * 1024;
   private static RootAllocator allocator;
-  
+
   @BeforeClass
   public static void beforeClass() {
     allocator = new RootAllocator(MAX_ALLOCATION);
   }
-  
+
   @AfterClass
   public static void afterClass() {
     if (allocator != null) {
       allocator.close();
     }
   }
-  
+
+  @Test(expected = IndexOutOfBoundsException.class)
+  public void testSliceOutOfBoundsLength_RaisesIndexOutOfBoundsException() {
+    try (BufferAllocator allocator = new RootAllocator(128);
+         ArrowBuf buf = allocator.buffer(2)
+    ) {
+      assertEquals(2, buf.capacity());
+      buf.slice(0, 3);
+    }
+  }
+
+  @Test(expected = IndexOutOfBoundsException.class)
+  public void testSliceOutOfBoundsIndexPlusLength_RaisesIndexOutOfBoundsException() {
+    try (BufferAllocator allocator = new RootAllocator(128);
+         ArrowBuf buf = allocator.buffer(2)
+    ) {
+      assertEquals(2, buf.capacity());
+      buf.slice(1, 2);
+    }
+  }
+
+  @Test(expected = IndexOutOfBoundsException.class)
+  public void testSliceOutOfBoundsIndex_RaisesIndexOutOfBoundsException() {
+    try (BufferAllocator allocator = new RootAllocator(128);
+         ArrowBuf buf = allocator.buffer(2)
+    ) {
+      assertEquals(2, buf.capacity());
+      buf.slice(3, 0);
+    }
+  }
+
+  @Test
+  public void testSliceWithinBoundsLength_ReturnsSlice() {
+    try (BufferAllocator allocator = new RootAllocator(128);
+         ArrowBuf buf = allocator.buffer(2)
+    ) {
+      assertEquals(2, buf.capacity());
+      assertEquals(1, buf.slice(1, 1).capacity());
+      assertEquals(2, buf.slice(0, 2).capacity());
+    }
+  }
+
   @Test
   public void testSetBytesSliced() {
     int arrLength = 64;
@@ -54,13 +97,13 @@ public class TestArrowBuf {
     ByteBuffer data = ByteBuffer.wrap(expecteds);
     try (ArrowBuf buf = allocator.buffer(expecteds.length)) {
       buf.setBytes(0, data, 0, data.capacity());
-      
+
       byte[] actuals = new byte[expecteds.length];
       buf.getBytes(0, actuals);
       assertArrayEquals(expecteds, actuals);
     }
   }
-  
+
   @Test
   public void testSetBytesUnsliced() {
     int arrLength = 64;
@@ -69,17 +112,17 @@ public class TestArrowBuf {
       arr[i] = (byte) i;
     }
     ByteBuffer data = ByteBuffer.wrap(arr);
-    
+
     int from = 10;
     int to = arrLength;
     byte[] expecteds = Arrays.copyOfRange(arr, from, to);
     try (ArrowBuf buf = allocator.buffer(expecteds.length)) {
       buf.setBytes(0, data, from, to - from);
-      
+
       byte[] actuals = new byte[expecteds.length];
       buf.getBytes(0, actuals);
       assertArrayEquals(expecteds, actuals);
     }
   }
-  
+
 }