From f6a5ac4473135bbc4bc1a5f537e060df45eb4824 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 16 Jul 2015 03:55:25 +0200 Subject: Bug 1166: Refine fix of JavaEmitter's JVMUtil_NewDirectByteBufferCopy See commit e424c28f869269f5a22c22ef017230346b22847a (first patch) Since JVMUtil_NewDirectByteBufferCopy is being called w/ 'size_t' values, e.g. 'count * sizeof(Structure)', we shall validate whether 'capacity' is valid, i.e. <= MAX_INT. After validation, 'capacity' is being cast to 'jint' before being passed to the java method. --- src/java/com/jogamp/gluegen/JavaEmitter.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/java/com') diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index 1952a43..98958bf 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -2500,8 +2500,9 @@ public class JavaEmitter implements GlueEmitter { if (getConfig().emitImpl()) { cWriter.println("#include "); + cWriter.println("#include "); cWriter.println(); - cWriter.println("static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, void * source_address, jint capacity); /* forward decl. */"); + cWriter.println("static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, void * source_address, size_t capacity); /* forward decl. */"); cWriter.println(); } for (final String code : cfg.customCCode()) { @@ -2549,7 +2550,8 @@ public class JavaEmitter implements GlueEmitter { " return JNI_TRUE;\n"+ "}\n"+ "\n"+ - "static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, void * source_address, jint capacity) {\n"+ + "#define JINT_MAX_VALUE ((size_t)0x7fffffffU)\n"+ + "static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, void * source_address, size_t capacity) {\n"+ " jobject jbyteBuffer;\n"+ " void * byteBufferPtr;\n"+ "\n"+ @@ -2558,7 +2560,13 @@ public class JavaEmitter implements GlueEmitter { " (*env)->FatalError(env, \"initializeImpl() not called\");\n"+ " return NULL;\n"+ " }\n"+ - " jbyteBuffer = (*env)->CallStaticObjectMethod(env, clazzBuffers, cstrBuffersNew, capacity);\n"+ + " if( JINT_MAX_VALUE < capacity ) {\n"+ + " fprintf(stderr, \"FatalError: capacity > MAX_INT: %lu\\n\", (unsigned long)capacity);\n"+ + " (*env)->FatalError(env, \"capacity > MAX_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"+ " return jbyteBuffer;\n"+ -- cgit v1.2.3