OSDN Git Service

Fix an issue where we're adding 4x the intended offset.
authorJesse Wilson <jessewilson@google.com>
Tue, 28 Jul 2009 23:49:40 +0000 (16:49 -0700)
committerJesse Wilson <jessewilson@google.com>
Wed, 29 Jul 2009 22:00:09 +0000 (15:00 -0700)
We were doing pointer arithmetic of mixed types (jint* and jint),
and the type conversion ended up causing the offset to be converted
an extra time.

libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp
libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectIntBufferTest.java
libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectShortBufferTest.java

index 5bd7907..c5e92bb 100644 (file)
@@ -184,7 +184,7 @@ static void harmony_nio_putShortsImpl(JNIEnv *_env, jobject _this,
     if (swap) {
         swapShorts(src_ + offset, length);
     }
-    memcpy((jbyte *)pointer, src_ + offset, length);
+    memcpy((jbyte *)pointer, (jbyte *)src_ + offset, length);
     if (swap) {
         swapShorts(src_ + offset, length);
     }
@@ -198,7 +198,7 @@ static void harmony_nio_putShortsImpl(JNIEnv *_env, jobject _this,
  */
 static void harmony_nio_putIntsImpl(JNIEnv *_env, jobject _this,
        jint pointer, jintArray src, jint offset, jint length, jboolean swap) {
-       
+
     offset = offset << 2;
     length = length << 2;
        
@@ -207,7 +207,7 @@ static void harmony_nio_putIntsImpl(JNIEnv *_env, jobject _this,
     if (swap) {
         swapInts(src_ + offset, length);
     }
-    memcpy((jbyte *)pointer, src_ + offset, length);
+    memcpy((jbyte *)pointer, (jbyte *)src_ + offset, length);
     if (swap) {
         swapInts(src_ + offset, length);
     }
index 489d265..5227d47 100644 (file)
@@ -22,6 +22,7 @@ import dalvik.annotation.TestTargetClass;
 import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
+import java.nio.IntBuffer;
 
 @TestTargetClass(java.nio.IntBuffer.class)
 public class DirectIntBufferTest extends IntBufferTest {
@@ -37,6 +38,30 @@ public class DirectIntBufferTest extends IntBufferTest {
         baseBuf = null;
     }
 
+    /**
+     * Regression for http://code.google.com/p/android/issues/detail?id=3279
+     */
+    @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "",
+            method = "put",
+            args = {int[].class, int.class, int.class}
+    )
+    public void testPutWhenOffsetIsNonZero() {
+        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(40);
+        byteBuffer.order(ByteOrder.nativeOrder());
+        IntBuffer intBuffer = byteBuffer.asIntBuffer();
+
+        int[] source = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+
+        intBuffer.put(source, 2, 2);
+        intBuffer.put(source, 4, 2);
+        assertEquals(4, intBuffer.get(0));
+        assertEquals(5, intBuffer.get(1));
+        assertEquals(6, intBuffer.get(2));
+        assertEquals(7, intBuffer.get(3));
+    }
+
     @TestTargetNew(
         level = TestLevel.PARTIAL_COMPLETE,
         notes = "Verifies hasArray method for direct IntBuffer.",
index a6e922b..91762a7 100644 (file)
@@ -23,6 +23,7 @@ import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.ShortBuffer;
+import java.nio.IntBuffer;
 
 @TestTargetClass(java.nio.ShortBuffer.class)
 public class DirectShortBufferTest extends ShortBufferTest {
@@ -38,6 +39,30 @@ public class DirectShortBufferTest extends ShortBufferTest {
         baseBuf = null;
     }
 
+    /**
+     * Regression for http://code.google.com/p/android/issues/detail?id=3279 
+     */
+    @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "",
+            method = "put",
+            args = {short[].class, int.class, int.class}
+    )
+    public void testPutWhenOffsetIsNonZero() {
+        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(40);
+        byteBuffer.order(ByteOrder.nativeOrder());
+        ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
+
+        short[] source = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+
+        shortBuffer.put(source, 2, 2);
+        shortBuffer.put(source, 4, 2);
+        assertEquals(4, shortBuffer.get(0));
+        assertEquals(5, shortBuffer.get(1));
+        assertEquals(6, shortBuffer.get(2));
+        assertEquals(7, shortBuffer.get(3));
+    }
+
     @TestTargetNew(
         level = TestLevel.PARTIAL_COMPLETE,
         notes = "Verifies hasArray method for direct ShortBuffer.",