Skip to content

Commit 4aabacb

Browse files
authored
Merge pull request #9688 from xtqqczze/get_clock_resolution
date: remove unsafe
2 parents 2000af8 + 59cd5ab commit 4aabacb

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

Cargo.lock

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

fuzz/Cargo.lock

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

src/uu/date/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ parse_datetime = { workspace = true }
3030
uucore = { workspace = true, features = ["parser"] }
3131

3232
[target.'cfg(unix)'.dependencies]
33-
libc = { workspace = true }
33+
nix = { workspace = true, features = ["time"] }
3434

3535
[target.'cfg(windows)'.dependencies]
3636
windows-sys = { workspace = true, features = [

src/uu/date/src/date.rs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ use clap::{Arg, ArgAction, Command};
99
use jiff::fmt::strtime;
1010
use jiff::tz::{TimeZone, TimeZoneDatabase};
1111
use jiff::{Timestamp, Zoned};
12-
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
13-
use libc::clock_settime;
14-
#[cfg(all(unix, not(target_os = "redox")))]
15-
use libc::{CLOCK_REALTIME, clock_getres, timespec};
1612
use std::collections::HashMap;
1713
use std::fs::File;
1814
use std::io::{BufRead, BufReader};
@@ -700,25 +696,20 @@ fn get_clock_resolution() -> Timestamp {
700696
}
701697

702698
#[cfg(all(unix, not(target_os = "redox")))]
699+
/// Returns the resolution of the system’s realtime clock.
700+
///
701+
/// # Panics
702+
///
703+
/// Panics if `clock_getres` fails. On a POSIX-compliant system this should not occur,
704+
/// as `CLOCK_REALTIME` is required to be supported.
705+
/// Failure would indicate a non-conforming or otherwise broken implementation.
703706
fn get_clock_resolution() -> Timestamp {
704-
let mut timespec = timespec {
705-
tv_sec: 0,
706-
tv_nsec: 0,
707-
};
708-
unsafe {
709-
// SAFETY: the timespec struct lives for the full duration of this function call.
710-
//
711-
// The clock_getres function can only fail if the passed clock_id is not
712-
// a known clock. All compliant posix implementors must support
713-
// CLOCK_REALTIME, therefore this function call cannot fail on any
714-
// compliant posix implementation.
715-
//
716-
// See more here:
717-
// https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html
718-
clock_getres(CLOCK_REALTIME, &raw mut timespec);
719-
}
707+
use nix::time::{ClockId, clock_getres};
708+
709+
let timespec = clock_getres(ClockId::CLOCK_REALTIME).unwrap();
710+
720711
#[allow(clippy::unnecessary_cast)] // Cast required on 32-bit platforms
721-
Timestamp::constant(timespec.tv_sec as i64, timespec.tv_nsec as i32)
712+
Timestamp::constant(timespec.tv_sec() as _, timespec.tv_nsec() as _)
722713
}
723714

724715
#[cfg(all(unix, target_os = "redox"))]
@@ -766,20 +757,13 @@ fn set_system_datetime(_date: Zoned) -> UResult<()> {
766757
/// `<https://linux.die.net/man/3/clock_settime>`
767758
/// `<https://www.gnu.org/software/libc/manual/html_node/Time-Types.html>`
768759
fn set_system_datetime(date: Zoned) -> UResult<()> {
769-
let ts = date.timestamp();
770-
let timespec = timespec {
771-
tv_sec: ts.as_second() as _,
772-
tv_nsec: ts.subsec_nanosecond() as _,
773-
};
760+
use nix::{sys::time::TimeSpec, time::ClockId};
774761

775-
let result = unsafe { clock_settime(CLOCK_REALTIME, &raw const timespec) };
762+
let ts = date.timestamp();
763+
let timespec = TimeSpec::new(ts.as_second() as _, ts.subsec_nanosecond() as _);
776764

777-
if result == 0 {
778-
Ok(())
779-
} else {
780-
Err(std::io::Error::last_os_error()
781-
.map_err_context(|| translate!("date-error-cannot-set-date")))
782-
}
765+
nix::time::clock_settime(ClockId::CLOCK_REALTIME, timespec)
766+
.map_err_context(|| translate!("date-error-cannot-set-date"))
783767
}
784768

785769
#[cfg(windows)]

0 commit comments

Comments
 (0)