From a3701528aa4be01924c983ce74e2efeaba0e58bc Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 16 Jul 2015 04:46:17 +0200 Subject: Bug 1166: Refine fix of JavaEmitter's JVMUtil_NewDirectByteBufferCopy (2) Further refine code, see commit f6a5ac4473135bbc4bc1a5f537e060df45eb4824. - Perform a NULL check on Buffers.newDirectByteBuffer(..) result. - Only copy memory if capacity > 0, incl fetching direct buffer address --- src/java/com/jogamp/gluegen/JavaEmitter.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index f9a978d..02e56a4 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -2554,6 +2554,8 @@ public class JavaEmitter implements GlueEmitter { "#define JINT_MAX_VALUE ((size_t)0x7fffffffU)\n"+ "static const char * sNewBufferImplNotCalled = \"initializeImpl() not called\";\n"+ "static const char * sNewBufferMAX_INT = \"capacity > MAX_INT\";\n"+ + "static const char * sNewBufferNULL = \"New direct ByteBuffer is NULL\";\n"+ + "\n"+ "static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, void * source_address, size_t capacity) {\n"+ " jobject jbyteBuffer;\n"+ " void * byteBufferPtr;\n"+ @@ -2568,10 +2570,16 @@ public class JavaEmitter implements GlueEmitter { " (*env)->FatalError(env, sNewBufferMAX_INT);\n"+ " return NULL;\n"+ " }\n"+ - " jbyteBuffer = (*env)->CallStaticObjectMethod(env, clazzBuffers, cstrBuffersNew, (jint)capacity);\n"+ - " byteBufferPtr = (*env)->GetDirectBufferAddress(env, jbyteBuffer);\n"+ - " memcpy(byteBufferPtr, source_address, capacity);\n"+ + " if( NULL == jbyteBuffer ) {\n"+ + " fprintf(stderr, \"%s %s: size %lu\\n\", sFatalError, sNewBufferNULL, (unsigned long)capacity);\n"+ + " (*env)->FatalError(env, sNewBufferNULL);\n"+ + " return NULL;\n"+ + " }\n"+ + " if( 0 < capacity ) {\n"+ + " byteBufferPtr = (*env)->GetDirectBufferAddress(env, jbyteBuffer);\n"+ + " memcpy(byteBufferPtr, source_address, capacity);\n"+ + " }\n"+ " return jbyteBuffer;\n"+ "}\n"+ "\n"; -- cgit v1.2.3