Skip to content
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

Proper null check to allow nullptr though #97

Closed
tlf30 opened this issue Dec 8, 2021 · 2 comments
Closed

Proper null check to allow nullptr though #97

tlf30 opened this issue Dec 8, 2021 · 2 comments

Comments

@tlf30
Copy link
Contributor

tlf30 commented Dec 8, 2021

Hello @SpaiR,
I would like to perform a null check to allow passing a null pointer through to native code.
Example:

public static native void openDialog(String vKey, String vTitle, String vFilters, String vPath, String vFileName,
                                         int vCountSelectionMax, long vUserDatas, int vFlags); /*
       if (env->IsSameObject(obj_vFilters, NULL)) {
            ImGuiFileDialog::Instance()->OpenDialog(vKey, vTitle, nullptr, vPath, vFileName, vCountSelectionMax, reinterpret_cast<void*>(vUserDatas), vFlags);
        } else {
            char* filters = (char*)env->GetStringUTFChars(obj_vFilters, JNI_FALSE);
            ImGuiFileDialog::Instance()->OpenDialog(vKey, vTitle, filters, vPath, vFileName, vCountSelectionMax, reinterpret_cast<void*>(vUserDatas), vFlags);
            env->ReleaseStringUTFChars(obj_vFilters, filters);
        }
    */

But the build system is still generating the char* vFilters = (char*)env->GetStringUTFChars(obj_vFilters, 0);, which causes an access violation when vFilters is null. I saw elsewhere in the library a check that uses obj_name == NULL, but this is not a safe way to check for a null as jni can have an object that is flagged as null, but not equal to null in the situation of a weak reference.

JNIEXPORT void JNICALL Java_imgui_extension_imguifiledialog_ImGuiFileDialog_openDialog(JNIEnv* env, jclass clazz, jstring obj_vKey, jstring obj_vTitle, jstring obj_vFilters, jstring obj_vPath, jstring obj_vFileName, jint vCountSelectionMax, jlong vUserDatas, jint vFlags) {
	char* vKey = (char*)env->GetStringUTFChars(obj_vKey, 0);
	char* vTitle = (char*)env->GetStringUTFChars(obj_vTitle, 0);
	char* vFilters = (char*)env->GetStringUTFChars(obj_vFilters, 0);
	char* vPath = (char*)env->GetStringUTFChars(obj_vPath, 0);
	char* vFileName = (char*)env->GetStringUTFChars(obj_vFileName, 0);


//@line:50

        if (env->IsSameObject(obj_vFilters, NULL)) {
            ImGuiFileDialog::Instance()->OpenDialog(vKey, vTitle, nullptr, vPath, vFileName, vCountSelectionMax, reinterpret_cast<void*>(vUserDatas), vFlags);
        } else {
            char* filters = (char*)env->GetStringUTFChars(obj_vFilters, JNI_FALSE);
            ImGuiFileDialog::Instance()->OpenDialog(vKey, vTitle, filters, vPath, vFileName, vCountSelectionMax, reinterpret_cast<void*>(vUserDatas), vFlags);
            env->ReleaseStringUTFChars(obj_vFilters, filters);
        }
    
	env->ReleaseStringUTFChars(obj_vKey, vKey);
	env->ReleaseStringUTFChars(obj_vTitle, vTitle);
	env->ReleaseStringUTFChars(obj_vFilters, vFilters);
	env->ReleaseStringUTFChars(obj_vPath, vPath);
	env->ReleaseStringUTFChars(obj_vFileName, vFileName);

}

Is there a way to prevent the build system from generating the char* vFilters = (char*)env->GetStringUTFChars(obj_vFilters, 0); and env->ReleaseStringUTFChars(obj_vFilters, vFilters);?

Thanks,
Trevor

@SpaiR
Copy link
Owner

SpaiR commented Dec 9, 2021

Yes. Add a "MANUAL" marker to the method definition. Thus no auto-generation will be applied to the method args.
https://github.com/SpaiR/imgui-java/blob/master/imgui-binding/src/main/java/imgui/ImGui.java#L837

@tlf30
Copy link
Contributor Author

tlf30 commented Dec 9, 2021

Got it, thanks! See #98

@tlf30 tlf30 closed this as completed Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants