Skip to content

Commit ea01af4

Browse files
authored
Rollup merge of rust-lang#60581 - hellow554:fix_60580, r=alexcrichton
convert custom try macro to `?` resolves rust-lang#60580 r? @frewsxcv
2 parents 50a0def + 5458b65 commit ea01af4

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed

src/libstd/sys/redox/process.rs

+30-35
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl Command {
150150
match cvt(syscall::clone(0))? {
151151
0 => {
152152
drop(input);
153-
let err = self.do_exec(theirs);
153+
let Err(err) = self.do_exec(theirs);
154154
let errno = err.raw_os_error().unwrap_or(syscall::EINVAL) as u32;
155155
let bytes = [
156156
(errno >> 24) as u8,
@@ -218,7 +218,10 @@ impl Command {
218218
}
219219

220220
match self.setup_io(default, true) {
221-
Ok((_, theirs)) => unsafe { self.do_exec(theirs) },
221+
Ok((_, theirs)) => unsafe {
222+
let Err(e) = self.do_exec(theirs);
223+
e
224+
},
222225
Err(e) => e,
223226
}
224227
}
@@ -253,45 +256,38 @@ impl Command {
253256
// allocation). Instead we just close it manually. This will never
254257
// have the drop glue anyway because this code never returns (the
255258
// child will either exec() or invoke syscall::exit)
256-
unsafe fn do_exec(&mut self, stdio: ChildPipes) -> io::Error {
257-
macro_rules! t {
258-
($e:expr) => (match $e {
259-
Ok(e) => e,
260-
Err(e) => return e,
261-
})
262-
}
263-
259+
unsafe fn do_exec(&mut self, stdio: ChildPipes) -> Result<!, io::Error> {
264260
if let Some(fd) = stdio.stderr.fd() {
265-
t!(cvt(syscall::dup2(fd, 2, &[])));
266-
let mut flags = t!(cvt(syscall::fcntl(2, syscall::F_GETFD, 0)));
261+
cvt(syscall::dup2(fd, 2, &[]))?;
262+
let mut flags = cvt(syscall::fcntl(2, syscall::F_GETFD, 0))?;
267263
flags &= ! syscall::O_CLOEXEC;
268-
t!(cvt(syscall::fcntl(2, syscall::F_SETFD, flags)));
264+
cvt(syscall::fcntl(2, syscall::F_SETFD, flags))?;
269265
}
270266
if let Some(fd) = stdio.stdout.fd() {
271-
t!(cvt(syscall::dup2(fd, 1, &[])));
272-
let mut flags = t!(cvt(syscall::fcntl(1, syscall::F_GETFD, 0)));
267+
cvt(syscall::dup2(fd, 1, &[]))?;
268+
let mut flags = cvt(syscall::fcntl(1, syscall::F_GETFD, 0))?;
273269
flags &= ! syscall::O_CLOEXEC;
274-
t!(cvt(syscall::fcntl(1, syscall::F_SETFD, flags)));
270+
cvt(syscall::fcntl(1, syscall::F_SETFD, flags))?;
275271
}
276272
if let Some(fd) = stdio.stdin.fd() {
277-
t!(cvt(syscall::dup2(fd, 0, &[])));
278-
let mut flags = t!(cvt(syscall::fcntl(0, syscall::F_GETFD, 0)));
273+
cvt(syscall::dup2(fd, 0, &[]))?;
274+
let mut flags = cvt(syscall::fcntl(0, syscall::F_GETFD, 0))?;
279275
flags &= ! syscall::O_CLOEXEC;
280-
t!(cvt(syscall::fcntl(0, syscall::F_SETFD, flags)));
276+
cvt(syscall::fcntl(0, syscall::F_SETFD, flags))?;
281277
}
282278

283279
if let Some(g) = self.gid {
284-
t!(cvt(syscall::setregid(g as usize, g as usize)));
280+
cvt(syscall::setregid(g as usize, g as usize))?;
285281
}
286282
if let Some(u) = self.uid {
287-
t!(cvt(syscall::setreuid(u as usize, u as usize)));
283+
cvt(syscall::setreuid(u as usize, u as usize))?;
288284
}
289285
if let Some(ref cwd) = self.cwd {
290-
t!(cvt(syscall::chdir(cwd)));
286+
cvt(syscall::chdir(cwd))?;
291287
}
292288

293289
for callback in self.closures.iter_mut() {
294-
t!(callback());
290+
callback()?;
295291
}
296292

297293
self.env.apply();
@@ -313,9 +309,9 @@ impl Command {
313309
};
314310

315311
let mut file = if let Some(program) = program {
316-
t!(File::open(program.as_os_str()))
312+
File::open(program.as_os_str())?
317313
} else {
318-
return io::Error::from_raw_os_error(syscall::ENOENT);
314+
return Err(io::Error::from_raw_os_error(syscall::ENOENT));
319315
};
320316

321317
// Push all the arguments
@@ -327,7 +323,7 @@ impl Command {
327323
let mut shebang = [0; 2];
328324
let mut read = 0;
329325
loop {
330-
match t!(reader.read(&mut shebang[read..])) {
326+
match reader.read(&mut shebang[read..])? {
331327
0 => break,
332328
n => read += n,
333329
}
@@ -338,9 +334,9 @@ impl Command {
338334
// First of all, since we'll be passing another file to
339335
// fexec(), we need to manually check that we have permission
340336
// to execute this file:
341-
let uid = t!(cvt(syscall::getuid()));
342-
let gid = t!(cvt(syscall::getgid()));
343-
let meta = t!(file.metadata());
337+
let uid = cvt(syscall::getuid())?;
338+
let gid = cvt(syscall::getgid())?;
339+
let meta = file.metadata()?;
344340

345341
let mode = if uid == meta.uid() as usize {
346342
meta.mode() >> 3*2 & 0o7
@@ -350,12 +346,12 @@ impl Command {
350346
meta.mode() & 0o7
351347
};
352348
if mode & 1 == 0 {
353-
return io::Error::from_raw_os_error(syscall::EPERM);
349+
return Err(io::Error::from_raw_os_error(syscall::EPERM));
354350
}
355351

356352
// Second of all, we need to actually read which interpreter it wants
357353
let mut interpreter = Vec::new();
358-
t!(reader.read_until(b'\n', &mut interpreter));
354+
reader.read_until(b'\n', &mut interpreter)?;
359355
// Pop one trailing newline, if any
360356
if interpreter.ends_with(&[b'\n']) {
361357
interpreter.pop().unwrap();
@@ -373,11 +369,11 @@ impl Command {
373369
};
374370
if let Some(ref interpreter) = interpreter {
375371
let path: &OsStr = OsStr::from_bytes(&interpreter);
376-
file = t!(File::open(path));
372+
file = File::open(path)?;
377373

378374
args.push([interpreter.as_ptr() as usize, interpreter.len()]);
379375
} else {
380-
t!(file.seek(SeekFrom::Start(0)));
376+
file.seek(SeekFrom::Start(0))?;
381377
}
382378

383379
args.push([self.program.as_ptr() as usize, self.program.len()]);
@@ -396,13 +392,12 @@ impl Command {
396392
}
397393

398394
if let Err(err) = syscall::fexec(file.as_raw_fd(), &args, &vars) {
399-
io::Error::from_raw_os_error(err.errno as i32)
395+
Err(io::Error::from_raw_os_error(err.errno as i32))
400396
} else {
401397
panic!("return from exec without err");
402398
}
403399
}
404400

405-
406401
fn setup_io(&self, default: Stdio, needs_stdin: bool)
407402
-> io::Result<(StdioPipes, ChildPipes)> {
408403
let null = Stdio::Null;

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

+16-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Command {
4747
match result {
4848
0 => {
4949
drop(input);
50-
let err = self.do_exec(theirs, envp.as_ref());
50+
let Err(err) = self.do_exec(theirs, envp.as_ref());
5151
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
5252
let bytes = [
5353
(errno >> 24) as u8,
@@ -123,7 +123,8 @@ impl Command {
123123
// environment lock before we try to exec.
124124
let _lock = sys::os::env_lock();
125125

126-
self.do_exec(theirs, envp.as_ref())
126+
let Err(e) = self.do_exec(theirs, envp.as_ref());
127+
e
127128
}
128129
}
129130
Err(e) => e,
@@ -164,29 +165,22 @@ impl Command {
164165
&mut self,
165166
stdio: ChildPipes,
166167
maybe_envp: Option<&CStringArray>
167-
) -> io::Error {
168+
) -> Result<!, io::Error> {
168169
use crate::sys::{self, cvt_r};
169170

170-
macro_rules! t {
171-
($e:expr) => (match $e {
172-
Ok(e) => e,
173-
Err(e) => return e,
174-
})
175-
}
176-
177171
if let Some(fd) = stdio.stdin.fd() {
178-
t!(cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO)));
172+
cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
179173
}
180174
if let Some(fd) = stdio.stdout.fd() {
181-
t!(cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO)));
175+
cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO))?;
182176
}
183177
if let Some(fd) = stdio.stderr.fd() {
184-
t!(cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO)));
178+
cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
185179
}
186180

187181
if cfg!(not(any(target_os = "l4re"))) {
188182
if let Some(u) = self.get_gid() {
189-
t!(cvt(libc::setgid(u as gid_t)));
183+
cvt(libc::setgid(u as gid_t))?;
190184
}
191185
if let Some(u) = self.get_uid() {
192186
// When dropping privileges from root, the `setgroups` call
@@ -198,11 +192,11 @@ impl Command {
198192
// privilege dropping function.
199193
let _ = libc::setgroups(0, ptr::null());
200194

201-
t!(cvt(libc::setuid(u as uid_t)));
195+
cvt(libc::setuid(u as uid_t))?;
202196
}
203197
}
204198
if let Some(ref cwd) = *self.get_cwd() {
205-
t!(cvt(libc::chdir(cwd.as_ptr())));
199+
cvt(libc::chdir(cwd.as_ptr()))?;
206200
}
207201

208202
// emscripten has no signal support.
@@ -225,18 +219,18 @@ impl Command {
225219
0,
226220
mem::size_of::<libc::sigset_t>());
227221
} else {
228-
t!(cvt(libc::sigemptyset(&mut set)));
222+
cvt(libc::sigemptyset(&mut set))?;
229223
}
230-
t!(cvt(libc::pthread_sigmask(libc::SIG_SETMASK, &set,
231-
ptr::null_mut())));
224+
cvt(libc::pthread_sigmask(libc::SIG_SETMASK, &set,
225+
ptr::null_mut()))?;
232226
let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
233227
if ret == libc::SIG_ERR {
234-
return io::Error::last_os_error()
228+
return Err(io::Error::last_os_error())
235229
}
236230
}
237231

238232
for callback in self.get_closures().iter_mut() {
239-
t!(callback());
233+
callback()?;
240234
}
241235

242236
// Although we're performing an exec here we may also return with an
@@ -261,7 +255,7 @@ impl Command {
261255
}
262256

263257
libc::execvp(self.get_argv()[0], self.get_argv().as_ptr());
264-
io::Error::last_os_error()
258+
Err(io::Error::last_os_error())
265259
}
266260

267261
#[cfg(not(any(target_os = "macos", target_os = "freebsd",

0 commit comments

Comments
 (0)