Hi there,
I've got a CString problem which occurs on Android ABI. Codes locate here, same as below:
use std::ffi::CString;
const PR_GET_NAME: libc::c_int = 16;
fn main() {
unsafe {
let cs = CString::from_vec_unchecked(Vec::with_capacity(32) as Vec<u8>);
let ptr = cs.into_raw();
let ret: libc::c_int = libc::prctl(PR_GET_NAME, ptr);
if ret != 0 {
println!("prctl failed: {}", ret);
return;
}
let c_string = CString::from_raw(ptr);
match c_string.into_string() {
Ok(s) => {
println!("Return: {}, debug print: {:#?}, len: {}.", s, s, s.len());
},
Err(_) => println!("into_string failed.!"),
};
}
}
On my Linux x86_64, the output is ALWAYS correct, let's say the thread name is demoarm64
:
Return: demoarm64, debug print: "demoarm64", len: 9.
On the aarch64-linux-android
(NDK-r17c, API-Level 26), the output looks confuse:
Return: demoarm6?, debug print: "demoarm6\u{0}", len: 9.
I use question mark for the unprintable character. This difference doesn't happen when the thread name(CString) contains letters only.
I used CString in a wrong way or something else? Any information will be helpful, thanks!