From bd79e4498465381117f2cbc6399a256061f1d144 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Tue, 28 Jul 2009 16:49:40 -0700 Subject: [PATCH] Fix an issue where we're adding 4x the intended offset. 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. --- .../org_apache_harmony_luni_platform_OSMemory.cpp | 6 +++--- .../nio/tests/java/nio/DirectIntBufferTest.java | 25 ++++++++++++++++++++++ .../nio/tests/java/nio/DirectShortBufferTest.java | 25 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp b/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp index 5bd79078d..c5e92bbd8 100644 --- a/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp +++ b/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp @@ -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); } diff --git a/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectIntBufferTest.java b/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectIntBufferTest.java index 489d265de..5227d4747 100644 --- a/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectIntBufferTest.java +++ b/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectIntBufferTest.java @@ -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.", diff --git a/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectShortBufferTest.java b/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectShortBufferTest.java index a6e922b93..91762a75e 100644 --- a/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectShortBufferTest.java +++ b/libcore/nio/src/test/java/org/apache/harmony/nio/tests/java/nio/DirectShortBufferTest.java @@ -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.", -- 2.11.0