Skip to content

Commit 51cecbf

Browse files
committed
library: use c string literals
1 parent 4fa3342 commit 51cecbf

File tree

8 files changed

+25
-26
lines changed

8 files changed

+25
-26
lines changed

library/std/src/sys/unix/args.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,15 @@ mod imp {
244244
let mut res = Vec::new();
245245

246246
unsafe {
247-
let process_info_sel = sel_registerName("processInfo\0".as_ptr());
248-
let arguments_sel = sel_registerName("arguments\0".as_ptr());
249-
let utf8_sel = sel_registerName("UTF8String\0".as_ptr());
250-
let count_sel = sel_registerName("count\0".as_ptr());
251-
let object_at_sel = sel_registerName("objectAtIndex:\0".as_ptr());
252-
253-
let klass = objc_getClass("NSProcessInfo\0".as_ptr());
247+
let process_info_sel =
248+
sel_registerName(c"processInfo".as_ptr() as *const libc::c_uchar);
249+
let arguments_sel = sel_registerName(c"arguments".as_ptr() as *const libc::c_uchar);
250+
let utf8_sel = sel_registerName(c"UTF8String".as_ptr() as *const libc::c_uchar);
251+
let count_sel = sel_registerName(c"count".as_ptr() as *const libc::c_uchar);
252+
let object_at_sel =
253+
sel_registerName(c"objectAtIndex:".as_ptr() as *const libc::c_uchar);
254+
255+
let klass = objc_getClass(c"NSProcessInfo".as_ptr() as *const libc::c_uchar);
254256
let info = objc_msgSend(klass, process_info_sel);
255257
let args = objc_msgSend(info, arguments_sel);
256258

library/std/src/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ impl File {
11401140
cfg_has_statx! {
11411141
if let Some(ret) = unsafe { try_statx(
11421142
fd,
1143-
b"\0" as *const _ as *const c_char,
1143+
c"".as_ptr() as *const c_char,
11441144
libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT,
11451145
libc::STATX_ALL,
11461146
) } {

library/std/src/sys/unix/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::CStr;
43
use crate::io::ErrorKind;
54

65
pub use self::rand::hashmap_random_keys;
@@ -75,7 +74,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
7574
// thread-id for the main thread and so renaming the main thread will rename the
7675
// process and we only want to enable this on platforms we've tested.
7776
if cfg!(target_os = "macos") {
78-
thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
77+
thread::Thread::set_name(&c"main");
7978
}
8079

8180
unsafe fn sanitize_standard_fds() {
@@ -127,7 +126,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
127126
if pfd.revents & libc::POLLNVAL == 0 {
128127
continue;
129128
}
130-
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
129+
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
131130
// If the stream is closed but we failed to reopen it, abort the
132131
// process. Otherwise we wouldn't preserve the safety of
133132
// operations on the corresponding Rust object Stdin, Stdout, or
@@ -157,7 +156,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
157156
use libc::open64;
158157
for fd in 0..3 {
159158
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
160-
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
159+
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
161160
// If the stream is closed but we failed to reopen it, abort the
162161
// process. Otherwise we wouldn't preserve the safety of
163162
// operations on the corresponding Rust object Stdin, Stdout, or

library/std/src/sys/unix/process/process_common.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ cfg_if::cfg_if! {
2424
if #[cfg(target_os = "fuchsia")] {
2525
// fuchsia doesn't have /dev/null
2626
} else if #[cfg(target_os = "redox")] {
27-
const DEV_NULL: &str = "null:\0";
27+
const DEV_NULL: &CStr = c"null:";
2828
} else if #[cfg(target_os = "vxworks")] {
29-
const DEV_NULL: &str = "/null\0";
29+
const DEV_NULL: &CStr = c"/null";
3030
} else {
31-
const DEV_NULL: &str = "/dev/null\0";
31+
const DEV_NULL: &CStr = c"/dev/null";
3232
}
3333
}
3434

@@ -481,8 +481,7 @@ impl Stdio {
481481
let mut opts = OpenOptions::new();
482482
opts.read(readable);
483483
opts.write(!readable);
484-
let path = unsafe { CStr::from_ptr(DEV_NULL.as_ptr() as *const _) };
485-
let fd = File::open_c(&path, &opts)?;
484+
let fd = File::open_c(DEV_NULL, &opts)?;
486485
Ok((ChildStdio::Owned(fd.into_inner()), None))
487486
}
488487

library/std/src/sys/unix/thread.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ impl Thread {
163163
#[cfg(target_os = "netbsd")]
164164
pub fn set_name(name: &CStr) {
165165
unsafe {
166-
let cname = CStr::from_bytes_with_nul_unchecked(b"%s\0".as_slice());
167166
let res = libc::pthread_setname_np(
168167
libc::pthread_self(),
169-
cname.as_ptr(),
168+
c"%s".as_ptr(),
170169
name.as_ptr() as *mut libc::c_void,
171170
);
172171
debug_assert_eq!(res, 0);

library/std/src/sys/windows/c.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub unsafe fn NtWriteFile(
324324
// Functions that aren't available on every version of Windows that we support,
325325
// but we still use them and just provide some form of a fallback implementation.
326326
compat_fn_with_fallback! {
327-
pub static KERNEL32: &CStr = ansi_str!("kernel32");
327+
pub static KERNEL32: &CStr = c"kernel32";
328328

329329
// >= Win10 1607
330330
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
@@ -357,7 +357,7 @@ compat_fn_optional! {
357357
}
358358

359359
compat_fn_with_fallback! {
360-
pub static NTDLL: &CStr = ansi_str!("ntdll");
360+
pub static NTDLL: &CStr = c"ntdll";
361361

362362
pub fn NtCreateKeyedEvent(
363363
KeyedEventHandle: LPHANDLE,

library/std/src/sys/windows/compat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ macro_rules! compat_fn_optional {
225225
/// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
226226
pub(super) fn load_synch_functions() {
227227
fn try_load() -> Option<()> {
228-
const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0");
229-
const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress");
230-
const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle");
228+
const MODULE_NAME: &CStr = c"api-ms-win-core-synch-l1-2-0";
229+
const WAIT_ON_ADDRESS: &CStr = c"WaitOnAddress";
230+
const WAKE_BY_ADDRESS_SINGLE: &CStr = c"WakeByAddressSingle";
231231

232232
// Try loading the library and all the required functions.
233233
// If any step fails, then they all fail.

library/std/src/sys/windows/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::{CStr, OsStr, OsString};
3+
use crate::ffi::{OsStr, OsString};
44
use crate::io::ErrorKind;
55
use crate::mem::MaybeUninit;
66
use crate::os::windows::ffi::{OsStrExt, OsStringExt};
@@ -63,7 +63,7 @@ pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
6363

6464
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
6565
// exists, we have to call it ourselves.
66-
thread::Thread::set_name(CStr::from_bytes_with_nul_unchecked(b"main\0"));
66+
thread::Thread::set_name(&c"main");
6767
}
6868

6969
// SAFETY: must be called only once during runtime cleanup.

0 commit comments

Comments
 (0)