Skip to content

Commit 6be47a6

Browse files
committed
cmp: remove unsafe & don't open unnecessary /dev/null
1 parent dc9ca17 commit 6be47a6

4 files changed

Lines changed: 42 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ chrono = "0.4.38"
1919
diff = "0.1.13"
2020
itoa = "1.0.11"
2121
regex = "1.10.4"
22+
rustix = { version = "1.1.4", features = ["fs"] }
2223
same-file = "1.0.6"
2324
unicode-width = "0.2.0"
2425

fuzz/Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmp.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ use std::iter::Peekable;
1111
use std::process::ExitCode;
1212
use std::{cmp, fs, io};
1313

14-
#[cfg(unix)]
15-
use std::os::fd::{AsRawFd, FromRawFd};
16-
17-
#[cfg(unix)]
18-
use std::os::unix::fs::MetadataExt;
19-
2014
#[cfg(target_os = "windows")]
2115
use std::os::windows::fs::MetadataExt;
2216

@@ -45,24 +39,12 @@ fn usage_string(executable: &str) -> String {
4539

4640
#[cfg(unix)]
4741
fn is_stdout_dev_null() -> bool {
48-
let Ok(dev_null) = fs::metadata("/dev/null") else {
42+
let stdout = io::stdout();
43+
let Ok(stat) = rustix::fs::fstat(stdout) else {
4944
return false;
5045
};
51-
52-
let stdout_fd = io::stdout().lock().as_raw_fd();
53-
54-
// SAFETY: we have exclusive access to stdout right now.
55-
let stdout_file = unsafe { fs::File::from_raw_fd(stdout_fd) };
56-
let Ok(stdout) = stdout_file.metadata() else {
57-
return false;
58-
};
59-
60-
let is_dev_null = stdout.dev() == dev_null.dev() && stdout.ino() == dev_null.ino();
61-
62-
// Don't let File close the fd. It's unfortunate that File doesn't have a leak_fd().
63-
std::mem::forget(stdout_file);
64-
65-
is_dev_null
46+
let dev = stat.st_rdev;
47+
rustix::fs::major(dev) == 1 && rustix::fs::minor(dev) == 3
6648
}
6749

6850
#[cfg(not(any(unix, target_os = "windows")))]

0 commit comments

Comments
 (0)