java - 将 JNI 有符号指针转换为无符号 native 指针 (C)

标签 java android c java-native-interface

从 jint*(32 位有符号)到 uint32_t* 的转换有效吗?它有效,但它似乎是一个黑客。编程语言是C。

JNIEXPORT jint JNICALL Java_foo_bar(JNIENV *env, jobject thisObj, jintArray inputDataJava){

/* Input data is signed */
jint * inputDataSigned = (*env)->GetIntArrayElements(env,inputDataJava,0);

/* Input data has been casted to unsigned. Is that valid? */
uint32_t * inputDataUnsigned = (uint32_t *) (*env)->GetIntArrayElements(env,inputDataJava,0);

}

提前致谢

最佳答案

Is this conversion from jint* (32 bit signed) to uint32_t* valid?

简而言之,没有。存在这样的强制转换可能会导致一些不良行为的实现。

<小时/>

It works, but it seems to be a hack.

许多未定义的行为似乎至少在最初对于一种配置有效......直到有人注意到heartbleed或未定义的行为可能导致的其他漏洞,例如。事实上,人们经常会溢出一个字节的数组,而他们的程序将继续运行......

<小时/>

很少有类型的指针具有足够宽松的对齐要求以指向任何类型。我从 C 标准中了解到的唯一类型是字符指针类型(char *signed char *unsigned char *)和 无效*

让事情变得复杂的是,该标准对这个jint *一无所知,也不知道任何对齐要求。我不会相信这样的代码。请参阅 C 标准的相关转换部分以了解可接受的内容: 6.3.2.3 Pointers为方便起见,引用如下。作为一名程序员,您通常会希望避开任何“未定义”的内容。

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

2 For any qualifier q, a pointer to a non-q-qualified type may be converted to a pointer to the q-qualified version of the type; the values stored in the original and converted pointers shall compare equal.

3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

4 Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.

5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.67)

6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

7 A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned68) for the referenced type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.

8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

关于java - 将 JNI 有符号指针转换为无符号 native 指针 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44714066/

相关文章:

c - 文件的二进制兼容性*

java - 在 Kotlin 中读取 ZipInputStream

java - 从不同的 JFrame 更新 JTable

java - 如何在Android中检查当前时间是否在未来的时间间隔内?

Java 打开文件夹确实返回所选文件夹

android - 在相对布局中将 TextView 对齐到 ImageButton 下方

java - ListView 项目 state_selected 在真实设备上丢失

c - 有没有更好的方法来确定多个字符范围?

c++ - Android 服务器最佳实践

c - 直接以二进制执行计算机指令