JNI:使用Integer.parseInt通过jvmti将充满数字的jstring转换为jint导致崩溃。

huangapple go评论127阅读模式
英文:

JNI:converting jstring(full of number) into jint using Integer.pasteInt by jvmti caused Crash

问题

jclass in = jniEnv->FindClass("java/lang/Integer");
jmethodID ipi = jniEnv->GetStaticMethodID(in, "parseInt", "(Ljava/lang/String;)I");

jint test = jniEnv->CallStaticIntMethod(in, ipi, (jstring)jniEnv->CallStaticObjectMethod(System, getProperty, jniEnv->NewStringUTF("input")));

Here is my code
I try to conver a jstring("10029909473242") into a jint
But if i try to print it it crash the vm.
is there a better solution?
Thanks.

jclass JOptionPane = jniEnv->FindClass("javax/swing/JOptionPane");
jmethodID showInputDialog = jniEnv->GetStaticMethodID(JOptionPane, "showInputDialog", "(Ljava/lang/Object;)Ljava/lang/String;");
jniEnv->CallStaticObjectMethod(NULL, showInputDialog, test);

Please note that I've only provided the translated code portions, as you requested.

英文:
    jclass in = jniEnv->FindClass("java/lang/Integer");
    jmethodID ipi = jniEnv->GetStaticMethodID(in, "parseInt", "(Ljava/lang/String;)I");

    jint test = jniEnv->CallStaticIntMethod(in, ipi,  (jstring)jniEnv->CallStaticObjectMethod(System, getProperty, jniEnv->NewStringUTF("input")));

Here is my code
I try to conver a jstring("10029909473242") into a jint
But if i try to print it it crash the vm.
is there a better solution?
Thanks.

	jclass JOptionPane = jniEnv->FindClass("javax/swing/JOptionPane");
    jmethodID showInputDialog = jniEnv->GetStaticMethodID(JOptionPane, "showInputDialog", "(Ljava/lang/Object;)Ljava/lang/String;");
    jniEnv->CallStaticObjectMethod(NULL, showInputDialog, test);

答案1

得分: 0

你没有显示实际错误,但我可以猜测一个原因:10029909473242太大了,无法适应Java的int类型,所以我猜Integer.parseInt抛出了一个NumberFormatException。在有未决异常的情况下调用任何JNI方法都会导致JVM崩溃。

这里的根本原因是你应该使用Long.parseLong,但你还应该在每次Call*Method调用后通过调用ExceptionOccurredExceptionCheck来添加错误检查。然后要么返回到JVM,要么调用ExceptionClear并在本机代码中继续。

英文:

You did not show an actual error, but I can guess at one reason:
10029909473242 is too large to fit into a Java int so I bet Integer.parseInt threw a NumberFormatException. Calling any JNI method with a pending exception will crash the JVM.

The root cause here is that you should use Long.parseLong instead, but you should additionally add error checking by calling ExceptionOccurred or ExceptionCheck after each Call*Method call. Then either return to the JVM or call ExceptionClear and continue in native code.

答案2

得分: 0

	char buf1[64];
	
	sprintf(buf1, "%d", p);
英文:
char buf1[64];

sprintf(buf1, "%d", p);

solved

huangapple
  • 本文由 发表于 2020年8月27日 16:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63612456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定