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

[Unity]字符串中间含\0传递到js会被截断 #1732

Open
chexiongsheng opened this issue May 23, 2024 · 3 comments
Open

[Unity]字符串中间含\0传递到js会被截断 #1732

chexiongsheng opened this issue May 23, 2024 · 3 comments

Comments

@chexiongsheng
Copy link
Collaborator

detail | 详细描述

现在plugin api传递字符串是没传长度的,比如返回值:

public static extern void __ReturnString(IntPtr isolate, IntPtr info, string str);

但utf8,utf16编码都支持中间含\0,c#编码类型api调用可能会被截断:#20

@chexiongsheng
Copy link
Collaborator Author

chexiongsheng commented May 24, 2024

il2cpp pinvoke本身也有同样的bug,如果要修正,原生接口将不能直接声明为string,而是传byte[]和长度,这样会多一个Encoding.UTF8.GetBytes调用,增加一个byte[]数组的分配和gc
其次xil2cpp版本用到的il2cpp::utils::StringUtils::Utf16ToUtf8也有该bug,改为调用utf8::unchecked::utf16to8需要增加utils/utf8-cpp/source/utf8/unchecked.h的引用,这个修改不知道各unity版本的兼容性如何。

考虑到unity本身也有类似的bug,之前也甚少项目反馈这问题,所以本issue的修改先不合入到master,先放到这个分支:https://github.com/Tencent/puerts/commits/fix_null_terminator_inside_string_being_cut

@mingxxming
Copy link
Contributor

如果 \0 不截断是不是也会有问题。。有遇到base64 的string末尾多几个字符以及在linux下读取js文件多几个字符的情况,都是加\0后得以解决的。

@chexiongsheng
Copy link
Collaborator Author

如果 \0 不截断是不是也会有问题。。有遇到base64 的string末尾多几个字符以及在linux下读取js文件多几个字符的情况,都是加\0后得以解决的。

通过长度识别结尾

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