1
+ use crate :: convert:: TryInto ;
1
2
use crate :: fmt;
2
3
use crate :: io:: { self , Error , ErrorKind } ;
3
4
use crate :: ptr;
@@ -17,7 +18,7 @@ impl Command {
17
18
default : Stdio ,
18
19
needs_stdin : bool ,
19
20
) -> io:: Result < ( Process , StdioPipes ) > {
20
- const CLOEXEC_MSG_FOOTER : & [ u8 ] = b"NOEX" ;
21
+ const CLOEXEC_MSG_FOOTER : [ u8 ; 4 ] = * b"NOEX" ;
21
22
22
23
let envp = self . capture_env ( ) ;
23
24
@@ -52,11 +53,12 @@ impl Command {
52
53
drop ( input) ;
53
54
let Err ( err) = self . do_exec ( theirs, envp. as_ref ( ) ) ;
54
55
let errno = err. raw_os_error ( ) . unwrap_or ( libc:: EINVAL ) as u32 ;
56
+ let errno = errno. to_be_bytes ( ) ;
55
57
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 ] ,
60
62
CLOEXEC_MSG_FOOTER [ 0 ] ,
61
63
CLOEXEC_MSG_FOOTER [ 1 ] ,
62
64
CLOEXEC_MSG_FOOTER [ 2 ] ,
@@ -81,12 +83,13 @@ impl Command {
81
83
match input. read ( & mut bytes) {
82
84
Ok ( 0 ) => return Ok ( ( p, ours) ) ,
83
85
Ok ( 8 ) => {
86
+ let ( errno, footer) = bytes. split_at ( 4 ) ;
84
87
assert ! (
85
- combine( CLOEXEC_MSG_FOOTER ) == combine( & bytes [ 4 .. 8 ] ) ,
88
+ combine( CLOEXEC_MSG_FOOTER ) == combine( footer . try_into ( ) . unwrap ( ) ) ,
86
89
"Validation on the CLOEXEC pipe failed: {:?}" ,
87
90
bytes
88
91
) ;
89
- let errno = combine ( & bytes [ 0 .. 4 ] ) ;
92
+ let errno = combine ( errno . try_into ( ) . unwrap ( ) ) ;
90
93
assert ! ( p. wait( ) . is_ok( ) , "wait() should either return Ok or panic" ) ;
91
94
return Err ( Error :: from_raw_os_error ( errno) ) ;
92
95
}
@@ -103,13 +106,8 @@ impl Command {
103
106
}
104
107
}
105
108
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)
113
111
}
114
112
}
115
113
0 commit comments