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:
parent
0f6031ab88
commit
266ab299cc
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user