Author: mturk Date: Thu Apr 16 07:57:02 2009 New Revision: 765497 URL: http://svn.apache.org/viewvc?rev=765497&view=rev Log: Fix array indexes. That's why we have testsuite to find such a bugs ;)
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java Modified: commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c?rev=765497&r1=765496&r2=765497&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c Thu Apr 16 07:57:02 2009 @@ -118,17 +118,16 @@ /* Failed creating ByteBuffer[] array */ goto cleanup; } - for (i = off; i < len; i++) { + for (i = 0; i < len; i++) { /* calculate overall size using the default alignment */ - sz += ACR_ALIGN_DEFAULT(ia[i]); + sz += ACR_ALIGN_DEFAULT(ia[i + off]); } if ((mem = malloc(sz)) != NULL) { size_t offset = 0; - jsize aindex = 0; - for (i = off; i < len; i++) { - size_t n = ACR_ALIGN_DEFAULT(ia[i]); + for (i = 0; i < len; i++) { + size_t n = ACR_ALIGN_DEFAULT(ia[i + off]); jobject bb = (*_E)->NewDirectByteBuffer(_E, mem + offset, - (jlong)ia[i]); + (jlong)ia[i + off]); if (bb == NULL) { /* One of the constructors failed. * The entire ByteBuffer[] array is invalid. @@ -137,7 +136,7 @@ rv = NULL; goto cleanup; } - (*_E)->SetObjectArrayElement(_E, rv, aindex++, bb); + (*_E)->SetObjectArrayElement(_E, rv, (jsize)i, bb); if ((*_E)->ExceptionCheck(_E)) { /* One of the setters failed. * The entire ByteBuffer[] array is invalid. @@ -200,17 +199,16 @@ /* Failed creating ByteBuffer[] array */ goto cleanup; } - for (i = off; i < len; i++) { + for (i = 0; i < len; i++) { /* calculate overall size using the default alignment */ - sz += ACR_ALIGN_DEFAULT(ia[i]); + sz += ACR_ALIGN_DEFAULT(ia[i + off]); } if ((mem = calloc(1, sz)) != NULL) { size_t offset = 0; - jsize aindex = 0; - for (i = off; i < len; i++) { - size_t n = ACR_ALIGN_DEFAULT(ia[i]); + for (i = 0; i < len; i++) { + size_t n = ACR_ALIGN_DEFAULT(ia[i + off]); jobject bb = (*_E)->NewDirectByteBuffer(_E, mem + offset, - (jlong)ia[i]); + (jlong)ia[i + off]); if (bb == NULL) { /* One of the constructors failed. * The entire ByteBuffer[] array is invalid. @@ -219,7 +217,7 @@ rv = NULL; goto cleanup; } - (*_E)->SetObjectArrayElement(_E, rv, aindex++, bb); + (*_E)->SetObjectArrayElement(_E, rv, (jsize)i, bb); if ((*_E)->ExceptionCheck(_E)) { /* One of the setters failed. * The entire ByteBuffer[] array is invalid. Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java?rev=765497&r1=765496&r2=765497&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java Thu Apr 16 07:57:02 2009 @@ -126,6 +126,24 @@ } + public void testArrayCreateIdx() + throws Exception + { + int [] sizes = { 100, 10, 100, 1000 }; + ByteBuffer[] a = DirectByteBuffer.allocate(sizes, 2, 2); + assertNotNull("Array", a); + + for (int i = 0; i < 2; i++) { + assertEquals("Capacity" + i, sizes[i+2], a[i].capacity()); + } + + /* WARNING: Free only the first array element + * because they all share the same memory + */ + DirectByteBuffer.free(a[0]); + + } + public void testArrayCreateAndClear() throws Exception {