Skip to content

Commit 65d90b9

Browse files
authored
refactor: unistd::close() does not need to be unsafe (#2495)
1 parent 81acce2 commit 65d90b9

File tree

3 files changed

+6
-43
lines changed

3 files changed

+6
-43
lines changed

src/sys/fanotify.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,7 @@ impl Drop for FanotifyEvent {
252252
if self.0.fd == libc::FAN_NOFD {
253253
return;
254254
}
255-
// SAFETY:
256-
//
257-
// If this fd is not `FAN_NOFD`, then it should be a valid, owned file
258-
// descriptor, which means we can safely close it.
259-
let e = unsafe { close(self.0.fd) };
255+
let e = close(self.0.fd);
260256
if !std::thread::panicking() && e == Err(Errno::EBADF) {
261257
panic!("Closing an invalid file descriptor!");
262258
};

src/unistd.rs

+3-32
Original file line numberDiff line numberDiff line change
@@ -1340,38 +1340,9 @@ pub fn gethostname() -> Result<OsString> {
13401340

13411341
/// Close a file descriptor.
13421342
///
1343-
/// # Safety
1344-
///
1345-
/// If you pass a `RawFd` to this function, ensure that this `close()` won't
1346-
/// trigger a double close.
1347-
///
1348-
/// ```no_run
1349-
/// use std::os::unix::io::AsRawFd;
1350-
/// use nix::unistd::close;
1351-
///
1352-
/// let f = tempfile::tempfile().unwrap();
1353-
/// // SAFETY:
1354-
/// //
1355-
/// // NOT safe! f will also close on drop!
1356-
/// unsafe { close(f.as_raw_fd()).unwrap() };
1357-
/// ```
1358-
///
1359-
/// We should pass `f` by value:
1360-
///
1361-
/// In the following case, it is generally preferred to call `drop(f)` rather
1362-
/// than `close()`.
1363-
///
1364-
/// ```rust
1365-
/// use std::os::unix::io::IntoRawFd;
1366-
/// use nix::unistd::close;
1367-
///
1368-
/// let f = tempfile::tempfile().unwrap();
1369-
/// // SAFETY:
1370-
/// //
1371-
/// // We are safe! `into_raw_fd()` consumes f
1372-
/// unsafe { close(f).unwrap() };
1373-
/// ```
1374-
pub unsafe fn close<Fd: std::os::fd::IntoRawFd>(fd: Fd) -> Result<()> {
1343+
/// If `fd` is an owned file descriptor, it is generally preferred to call
1344+
/// `drop(fd)` rather than `close(fd)`.
1345+
pub fn close<Fd: std::os::fd::IntoRawFd>(fd: Fd) -> Result<()> {
13751346
let res = unsafe { libc::close(fd.into_raw_fd()) };
13761347
Errno::result(res).map(drop)
13771348
}

test/sys/test_socket.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,7 @@ pub fn test_scm_rights() {
914914
unsafe { std::os::fd::BorrowedFd::borrow_raw(received_r) };
915915
read(borrowed_received_r, &mut buf).unwrap();
916916
assert_eq!(&buf[..], b"world");
917-
// SAFETY:
918-
// there shouldn't be double close
919-
unsafe { close(received_r).unwrap() };
917+
close(received_r).unwrap();
920918
}
921919

922920
// Disable the test on emulated platforms due to not enabled support of AF_ALG in QEMU from rust cross
@@ -1645,9 +1643,7 @@ fn test_impl_scm_credentials_and_rights(
16451643
unsafe { std::os::fd::BorrowedFd::borrow_raw(received_r) };
16461644
read(received_r_borrowed, &mut buf).unwrap();
16471645
assert_eq!(&buf[..], b"world");
1648-
// SAFETY:
1649-
// double-close won't happen
1650-
unsafe { close(received_r).unwrap() };
1646+
close(received_r).unwrap();
16511647

16521648
Ok(())
16531649
}

0 commit comments

Comments
 (0)