File tree 3 files changed +6
-43
lines changed
3 files changed +6
-43
lines changed Original file line number Diff line number Diff line change @@ -252,11 +252,7 @@ impl Drop for FanotifyEvent {
252
252
if self . 0 . fd == libc:: FAN_NOFD {
253
253
return ;
254
254
}
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 ) ;
260
256
if !std:: thread:: panicking ( ) && e == Err ( Errno :: EBADF ) {
261
257
panic ! ( "Closing an invalid file descriptor!" ) ;
262
258
} ;
Original file line number Diff line number Diff line change @@ -1340,38 +1340,9 @@ pub fn gethostname() -> Result<OsString> {
1340
1340
1341
1341
/// Close a file descriptor.
1342
1342
///
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 < ( ) > {
1375
1346
let res = unsafe { libc:: close ( fd. into_raw_fd ( ) ) } ;
1376
1347
Errno :: result ( res) . map ( drop)
1377
1348
}
Original file line number Diff line number Diff line change @@ -914,9 +914,7 @@ pub fn test_scm_rights() {
914
914
unsafe { std:: os:: fd:: BorrowedFd :: borrow_raw ( received_r) } ;
915
915
read ( borrowed_received_r, & mut buf) . unwrap ( ) ;
916
916
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 ( ) ;
920
918
}
921
919
922
920
// 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(
1645
1643
unsafe { std:: os:: fd:: BorrowedFd :: borrow_raw ( received_r) } ;
1646
1644
read ( received_r_borrowed, & mut buf) . unwrap ( ) ;
1647
1645
assert_eq ! ( & buf[ ..] , b"world" ) ;
1648
- // SAFETY:
1649
- // double-close won't happen
1650
- unsafe { close ( received_r) . unwrap ( ) } ;
1646
+ close ( received_r) . unwrap ( ) ;
1651
1647
1652
1648
Ok ( ( ) )
1653
1649
}
You can’t perform that action at this time.
0 commit comments