Skip to content

Commit 9752f15

Browse files
committed
Used write_c_str
1 parent 30608ff commit 9752f15

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/shims/unix/android/thread.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::iter;
2-
31
use rustc_span::Symbol;
42
use rustc_target::spec::abi::Abi;
53

@@ -49,17 +47,16 @@ pub fn prctl<'tcx>(
4947
let thread = ThreadId::try_from(thread).unwrap();
5048

5149
// FIXME: we should use the program name if the thread name is not set
52-
let name = this.get_thread_name(thread).unwrap_or(DEFAULT_THREAD_NAME).to_owned();
53-
let name_len = name.len().max(TASK_COMM_LEN - 1);
50+
let mut name = this.get_thread_name(thread).unwrap_or(DEFAULT_THREAD_NAME).to_owned();
51+
let name_len = TASK_COMM_LEN - 1;
5452

55-
this.eval_context_mut().write_bytes_ptr(
56-
name_out,
57-
name.iter()
58-
.take(name_len)
59-
.copied()
60-
.chain(iter::repeat_n(0u8, TASK_COMM_LEN.strict_sub(name_len))),
61-
)?;
53+
if name.len() >= name_len {
54+
name.drain(name_len..);
55+
} else {
56+
name.resize(name_len, 0);
57+
}
6258

59+
this.write_c_str(&name, name_out, TASK_COMM_LEN as u64)?;
6360
Scalar::from_u32(0)
6461
}
6562
op => {

tests/pass-dep/libc/threadname.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ fn main() {
5959
let result = thread::Builder::new().name(name.clone()).spawn(move || {
6060
assert_eq!(thread::current().name(), Some(name.as_str()));
6161

62-
let mut buf = vec![0u8; name.len() + 1];
62+
// POSIX seems to promise at least 15 chars excluding a null terminator.
63+
let mut buf = vec![0u8; 16];
6364
assert_eq!(get_thread_name(&mut buf), 0);
6465
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
6566
if name.len() >= 15 {
6667
assert!(
6768
cstr.to_bytes().len() >= 15,
6869
"name is too short: len={}",
6970
cstr.to_bytes().len()
70-
); // POSIX seems to promise at least 15 chars
71+
);
7172
assert!(name.as_bytes().starts_with(cstr.to_bytes()));
7273
} else {
7374
assert_eq!(name.as_bytes(), cstr.to_bytes());
@@ -80,7 +81,7 @@ fn main() {
8081
// But with a too long name it should fail except:
8182
// * on FreeBSD where the function has no return, hence cannot indicate failure,
8283
// * on Android where prctl silently truncates the string.
83-
#[cfg(not(all(target_os = "freebsd", target_os = "android")))]
84+
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
8485
assert_ne!(set_thread_name(&std::ffi::CString::new(name).unwrap()), 0);
8586
}
8687
});

0 commit comments

Comments
 (0)