Skip to content

Check UTF-8 encoding in JNI functions #5991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ static JNIObjectRefType GetObjectRefType(JNIEnvironment env, JNIObjectHandle han
@CEntryPointOptions(prologue = JNIEnvEnterPrologue.class, prologueBailout = ReturnNullHandle.class)
static JNIObjectHandle FindClass(JNIEnvironment env, CCharPointer cname) {
CharSequence name = Utf8.wrapUtf8CString(cname);
if (name == null) {
throw new NoClassDefFoundError("Class name is either null or invalid UTF-8 string");
}

Class<?> clazz = JNIReflectionDictionary.singleton().getClassObjectByName(name);
if (clazz == null) {
throw new NoClassDefFoundError(name.toString());
Expand All @@ -363,7 +367,15 @@ static int RegisterNatives(JNIEnvironment env, JNIObjectHandle hclazz, JNINative
for (int i = 0; i < nmethods; i++) {
JNINativeMethod entry = (JNINativeMethod) p;
CharSequence name = Utf8.wrapUtf8CString(entry.name());
if (name == null) {
throw new NoSuchMethodError("Method name at index " + i + " is either null or invalid UTF-8 string");
}

CharSequence signature = Utf8.wrapUtf8CString(entry.signature());
if (signature == null) {
throw new NoSuchMethodError("Method signature at index " + i + " is either null or invalid UTF-8 string");
}

CFunctionPointer fnPtr = entry.fnPtr();

String declaringClass = MetaUtil.toInternalName(clazz.getName());
Expand Down Expand Up @@ -1243,7 +1255,15 @@ static JNIMethodId getMethodID(JNIObjectHandle hclazz, CCharPointer cname, CChar
DynamicHub.fromClass(clazz).ensureInitialized();

CharSequence name = Utf8.wrapUtf8CString(cname);
if (name == null) {
throw new NoSuchMethodError("Method name is either null or invalid UTF-8 string");
}

CharSequence signature = Utf8.wrapUtf8CString(csig);
if (signature == null) {
throw new NoSuchMethodError("Method signature is either null or invalid UTF-8 string");
}

return getMethodID(clazz, name, signature, isStatic);
}

Expand All @@ -1269,6 +1289,10 @@ static JNIFieldId getFieldID(JNIObjectHandle hclazz, CCharPointer cname, CCharPo
DynamicHub.fromClass(clazz).ensureInitialized();

CharSequence name = Utf8.wrapUtf8CString(cname);
if (name == null) {
throw new NoSuchFieldError("Field name is either null or invalid UTF-8 string");
}

JNIFieldId fieldID = JNIReflectionDictionary.singleton().getFieldID(clazz, name, isStatic);
if (fieldID.isNull()) {
throw new NoSuchFieldError(clazz.getName() + '.' + name);
Expand Down