diff options
| author | Lzu Tao <taolzu@gmail.com> | 2020-07-12 16:47:15 +0000 |
|---|---|---|
| committer | Lzu Tao <taolzu@gmail.com> | 2020-07-12 16:47:15 +0000 |
| commit | 879afd5116a77baea6cdb76029e878bb24eba203 (patch) | |
| tree | a324c9277a831c7adc5066e73e75bd6f32357e9a /src/libstd/sys | |
| parent | 9d09331e00b02f81c714b0c41ce3a38380dd36a2 (diff) | |
| download | rust-879afd5116a77baea6cdb76029e878bb24eba203.tar.gz rust-879afd5116a77baea6cdb76029e878bb24eba203.zip | |
process_unix: prefer i32::*_be_bytes over manually shifting bytes
Diffstat (limited to 'src/libstd/sys')
| -rw-r--r-- | src/libstd/sys/unix/process/process_unix.rs | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/libstd/sys/unix/process/process_unix.rs b/src/libstd/sys/unix/process/process_unix.rs index f389c60615f..de35fe0521d 100644 --- a/src/libstd/sys/unix/process/process_unix.rs +++ b/src/libstd/sys/unix/process/process_unix.rs @@ -1,3 +1,4 @@ +use crate::convert::TryInto; use crate::fmt; use crate::io::{self, Error, ErrorKind}; use crate::ptr; @@ -17,7 +18,7 @@ impl Command { default: Stdio, needs_stdin: bool, ) -> io::Result<(Process, StdioPipes)> { - const CLOEXEC_MSG_FOOTER: &[u8] = b"NOEX"; + const CLOEXEC_MSG_FOOTER: [u8; 4] = *b"NOEX"; let envp = self.capture_env(); @@ -52,11 +53,12 @@ impl Command { drop(input); let Err(err) = self.do_exec(theirs, envp.as_ref()); let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32; + let errno = errno.to_be_bytes(); let bytes = [ - (errno >> 24) as u8, - (errno >> 16) as u8, - (errno >> 8) as u8, - (errno >> 0) as u8, + errno[0], + errno[1], + errno[2], + errno[3], CLOEXEC_MSG_FOOTER[0], CLOEXEC_MSG_FOOTER[1], CLOEXEC_MSG_FOOTER[2], @@ -81,12 +83,13 @@ impl Command { match input.read(&mut bytes) { Ok(0) => return Ok((p, ours)), Ok(8) => { + let (errno, footer) = bytes.split_at(4); assert!( - combine(CLOEXEC_MSG_FOOTER) == combine(&bytes[4..8]), + combine(CLOEXEC_MSG_FOOTER) == combine(footer.try_into().unwrap()), "Validation on the CLOEXEC pipe failed: {:?}", bytes ); - let errno = combine(&bytes[0..4]); + let errno = combine(errno.try_into().unwrap()); assert!(p.wait().is_ok(), "wait() should either return Ok or panic"); return Err(Error::from_raw_os_error(errno)); } @@ -103,13 +106,8 @@ impl Command { } } - fn combine(arr: &[u8]) -> i32 { - let a = arr[0] as u32; - let b = arr[1] as u32; - let c = arr[2] as u32; - let d = arr[3] as u32; - - ((a << 24) | (b << 16) | (c << 8) | (d << 0)) as i32 + fn combine(arr: [u8; 4]) -> i32 { + i32::from_be_bytes(arr) } } |
