Wcast-qual was useuful to discover those errors in extra(): the returned ByteBuffer is fully writable, but the backing memory isn't really intended to be written to (and you never should modify the descriptors in memory).

So fixed by getting a read-only ByteBuffer and passing that back to Java instead.
This commit is contained in:
Luca Longinotti 2013-06-13 13:43:27 +02:00
parent 0f6031ab88
commit 266ab299cc
5 changed files with 22 additions and 6 deletions

View File

@ -162,8 +162,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(ConfigDescriptor, extra)
struct libusb_config_descriptor *config = unwrapConfigDescriptor(env, this);
if (!config) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) config->extra,
config->extra_length);
return NewDirectReadOnlyByteBuffer(env, config->extra, config->extra_length);
}
/**

View File

@ -156,8 +156,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(EndpointDescriptor, extra)
struct libusb_endpoint_descriptor *ep = unwrapEndpointDescriptor(env, this);
if (!ep) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) ep->extra,
ep->extra_length);
return NewDirectReadOnlyByteBuffer(env, ep->extra, ep->extra_length);
}
/**

View File

@ -198,7 +198,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(InterfaceDescriptor, extra)
unwrapInterfaceDescriptor(env, this);
if (!interface) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) interface->extra,
return NewDirectReadOnlyByteBuffer(env, interface->extra,
interface->extra_length);
}

View File

@ -19,7 +19,23 @@ jint illegalState(JNIEnv *env, const char *message)
return (*env)->ThrowNew(env, cls, message);
}
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
jobject NewDirectReadOnlyByteBuffer(JNIEnv *env, const void *mem,
int mem_length)
{
jobject buffer = (*env)->NewDirectByteBuffer(env, (void *) mem, mem_length);
// Get a read-only buffer from this buffer.
jclass cls = (*env)->GetObjectClass(env, buffer);
jmethodID method = (*env)->GetMethodID(env, cls, "asReadOnlyBuffer",
"()Ljava/nio/ByteBuffer;");
return (*env)->CallObjectMethod(env, buffer, method);
}
#pragma GCC diagnostic pop
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
jvm = vm;
return JNI_VERSION_1_4;
}

View File

@ -88,5 +88,7 @@ extern JavaVM *jvm;
jint illegalArgument(JNIEnv *env, const char *message);
jint illegalState(JNIEnv *env, const char *message);
jobject NewDirectReadOnlyByteBuffer(JNIEnv *env, const void *mem,
int mem_length);
#endif