Skip to content

Commit c128e9b

Browse files
authored
Auto merge of rust-lang#34441 - tbu-:pr_dont_ignore_errors, r=alexcrichton
Don't ignore errors of syscalls in std::sys::unix::fd If any of these syscalls fail, it indicates a programmer error that should not be silently ignored.
2 parents d011290 + 9347ffc commit c128e9b

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

src/libstd/sys/unix/fd.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,31 @@ impl FileDesc {
6262
}
6363

6464
#[cfg(not(any(target_env = "newlib", target_os = "solaris", target_os = "emscripten")))]
65-
pub fn set_cloexec(&self) {
65+
pub fn set_cloexec(&self) -> io::Result<()> {
6666
unsafe {
67-
let ret = libc::ioctl(self.fd, libc::FIOCLEX);
68-
debug_assert_eq!(ret, 0);
67+
cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
68+
Ok(())
6969
}
7070
}
7171
#[cfg(any(target_env = "newlib", target_os = "solaris", target_os = "emscripten"))]
72-
pub fn set_cloexec(&self) {
72+
pub fn set_cloexec(&self) -> io::Result<()> {
7373
unsafe {
74-
let previous = libc::fcntl(self.fd, libc::F_GETFD);
75-
let ret = libc::fcntl(self.fd, libc::F_SETFD, previous | libc::FD_CLOEXEC);
76-
debug_assert_eq!(ret, 0);
74+
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
75+
cvt(libc::fcntl(self.fd, libc::F_SETFD, previous | libc::FD_CLOEXEC))?;
76+
Ok(())
7777
}
7878
}
7979

80-
pub fn set_nonblocking(&self, nonblocking: bool) {
80+
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
8181
unsafe {
82-
let previous = libc::fcntl(self.fd, libc::F_GETFL);
83-
debug_assert!(previous != -1);
82+
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
8483
let new = if nonblocking {
8584
previous | libc::O_NONBLOCK
8685
} else {
8786
previous & !libc::O_NONBLOCK
8887
};
89-
let ret = libc::fcntl(self.fd, libc::F_SETFL, new);
90-
debug_assert!(ret != -1);
88+
cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
89+
Ok(())
9190
}
9291
}
9392

@@ -114,8 +113,8 @@ impl FileDesc {
114113

115114
let make_filedesc = |fd| {
116115
let fd = FileDesc::new(fd);
117-
fd.set_cloexec();
118-
fd
116+
fd.set_cloexec()?;
117+
Ok(fd)
119118
};
120119
static TRY_CLOEXEC: AtomicBool =
121120
AtomicBool::new(!cfg!(target_os = "android"));
@@ -127,7 +126,7 @@ impl FileDesc {
127126
// though it reported doing so on F_DUPFD_CLOEXEC.
128127
Ok(fd) => {
129128
return Ok(if cfg!(target_os = "linux") {
130-
make_filedesc(fd)
129+
make_filedesc(fd)?
131130
} else {
132131
FileDesc::new(fd)
133132
})
@@ -138,7 +137,7 @@ impl FileDesc {
138137
Err(e) => return Err(e),
139138
}
140139
}
141-
cvt(unsafe { libc::fcntl(fd, libc::F_DUPFD, 0) }).map(make_filedesc)
140+
cvt(unsafe { libc::fcntl(fd, libc::F_DUPFD, 0) }).and_then(make_filedesc)
142141
}
143142
}
144143

src/libstd/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl File {
418418
// The CLOEXEC flag, however, is supported on versions of OSX/BSD/etc
419419
// that we support, so we only do this on Linux currently.
420420
if cfg!(target_os = "linux") {
421-
fd.set_cloexec();
421+
fd.set_cloexec()?;
422422
}
423423

424424
Ok(File(fd))

src/libstd/sys/unix/net.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Socket {
7777

7878
let fd = cvt(libc::socket(fam, ty, 0))?;
7979
let fd = FileDesc::new(fd);
80-
fd.set_cloexec();
80+
fd.set_cloexec()?;
8181
Ok(Socket(fd))
8282
}
8383
}
@@ -99,9 +99,9 @@ impl Socket {
9999

100100
cvt(libc::socketpair(fam, ty, 0, fds.as_mut_ptr()))?;
101101
let a = FileDesc::new(fds[0]);
102-
a.set_cloexec();
103102
let b = FileDesc::new(fds[1]);
104-
b.set_cloexec();
103+
a.set_cloexec()?;
104+
b.set_cloexec()?;
105105
Ok((Socket(a), Socket(b)))
106106
}
107107
}
@@ -132,7 +132,7 @@ impl Socket {
132132
libc::accept(self.0.raw(), storage, len)
133133
})?;
134134
let fd = FileDesc::new(fd);
135-
fd.set_cloexec();
135+
fd.set_cloexec()?;
136136
Ok(Socket(fd))
137137
}
138138

src/libstd/sys/unix/pipe.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
4444
}
4545
}
4646
if unsafe { libc::pipe(fds.as_mut_ptr()) == 0 } {
47-
Ok((AnonPipe::from_fd(fds[0]), AnonPipe::from_fd(fds[1])))
47+
let fd0 = FileDesc::new(fds[0]);
48+
let fd1 = FileDesc::new(fds[1]);
49+
Ok((AnonPipe::from_fd(fd0)?, AnonPipe::from_fd(fd1)?))
4850
} else {
4951
Err(io::Error::last_os_error())
5052
}
5153
}
5254

5355
impl AnonPipe {
54-
pub fn from_fd(fd: libc::c_int) -> AnonPipe {
55-
let fd = FileDesc::new(fd);
56-
fd.set_cloexec();
57-
AnonPipe(fd)
56+
pub fn from_fd(fd: FileDesc) -> io::Result<AnonPipe> {
57+
fd.set_cloexec()?;
58+
Ok(AnonPipe(fd))
5859
}
5960

6061
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
@@ -81,8 +82,8 @@ pub fn read2(p1: AnonPipe,
8182
// in the `select` loop below, and we wouldn't want one to block the other!
8283
let p1 = p1.into_fd();
8384
let p2 = p2.into_fd();
84-
p1.set_nonblocking(true);
85-
p2.set_nonblocking(true);
85+
p1.set_nonblocking(true)?;
86+
p2.set_nonblocking(true)?;
8687

8788
let max = cmp::max(p1.raw(), p2.raw());
8889
loop {
@@ -114,11 +115,11 @@ pub fn read2(p1: AnonPipe,
114115
}
115116
};
116117
if read(&p1, v1)? {
117-
p2.set_nonblocking(false);
118+
p2.set_nonblocking(false)?;
118119
return p2.read_to_end(v2).map(|_| ());
119120
}
120121
if read(&p2, v2)? {
121-
p1.set_nonblocking(false);
122+
p1.set_nonblocking(false)?;
122123
return p1.read_to_end(v1).map(|_| ());
123124
}
124125
}

0 commit comments

Comments
 (0)