Skip to content

Commit 7b1247c

Browse files
authored
Rollup merge of #74271 - lzutao:cmdbytes, r=LukasKalbertodt
process_unix: prefer i32::*_be_bytes over manually shifting bytes This PR makes it more clear about the intend of the code.
2 parents fadd91c + 879afd5 commit 7b1247c

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/libstd/sys/unix/process/process_unix.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::convert::TryInto;
12
use crate::fmt;
23
use crate::io::{self, Error, ErrorKind};
34
use crate::ptr;
@@ -17,7 +18,7 @@ impl Command {
1718
default: Stdio,
1819
needs_stdin: bool,
1920
) -> io::Result<(Process, StdioPipes)> {
20-
const CLOEXEC_MSG_FOOTER: &[u8] = b"NOEX";
21+
const CLOEXEC_MSG_FOOTER: [u8; 4] = *b"NOEX";
2122

2223
let envp = self.capture_env();
2324

@@ -52,11 +53,12 @@ impl Command {
5253
drop(input);
5354
let Err(err) = self.do_exec(theirs, envp.as_ref());
5455
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
56+
let errno = errno.to_be_bytes();
5557
let bytes = [
56-
(errno >> 24) as u8,
57-
(errno >> 16) as u8,
58-
(errno >> 8) as u8,
59-
(errno >> 0) as u8,
58+
errno[0],
59+
errno[1],
60+
errno[2],
61+
errno[3],
6062
CLOEXEC_MSG_FOOTER[0],
6163
CLOEXEC_MSG_FOOTER[1],
6264
CLOEXEC_MSG_FOOTER[2],
@@ -81,12 +83,13 @@ impl Command {
8183
match input.read(&mut bytes) {
8284
Ok(0) => return Ok((p, ours)),
8385
Ok(8) => {
86+
let (errno, footer) = bytes.split_at(4);
8487
assert!(
85-
combine(CLOEXEC_MSG_FOOTER) == combine(&bytes[4..8]),
88+
combine(CLOEXEC_MSG_FOOTER) == combine(footer.try_into().unwrap()),
8689
"Validation on the CLOEXEC pipe failed: {:?}",
8790
bytes
8891
);
89-
let errno = combine(&bytes[0..4]);
92+
let errno = combine(errno.try_into().unwrap());
9093
assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
9194
return Err(Error::from_raw_os_error(errno));
9295
}
@@ -103,13 +106,8 @@ impl Command {
103106
}
104107
}
105108

106-
fn combine(arr: &[u8]) -> i32 {
107-
let a = arr[0] as u32;
108-
let b = arr[1] as u32;
109-
let c = arr[2] as u32;
110-
let d = arr[3] as u32;
111-
112-
((a << 24) | (b << 16) | (c << 8) | (d << 0)) as i32
109+
fn combine(arr: [u8; 4]) -> i32 {
110+
i32::from_be_bytes(arr)
113111
}
114112
}
115113

0 commit comments

Comments
 (0)