From 59cd5ab011e8f2cc04d86ca8c48ce94d14d6af14 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Thu, 18 Dec 2025 01:29:39 +0000 Subject: [PATCH] date: remove unsafe --- Cargo.lock | 2 +- fuzz/Cargo.lock | 2 +- src/uu/date/Cargo.toml | 2 +- src/uu/date/src/date.rs | 50 ++++++++++++++--------------------------- 4 files changed, 20 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe0ee52a1..c809b3af1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3191,7 +3191,7 @@ dependencies = [ "clap", "fluent", "jiff", - "libc", + "nix", "parse_datetime", "uucore", "windows-sys 0.61.2", diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 8d7b16196..90934a271 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -1597,7 +1597,7 @@ dependencies = [ "clap", "fluent", "jiff", - "libc", + "nix", "parse_datetime", "uucore", "windows-sys 0.61.2", diff --git a/src/uu/date/Cargo.toml b/src/uu/date/Cargo.toml index 2d5f53d4b..431868b91 100644 --- a/src/uu/date/Cargo.toml +++ b/src/uu/date/Cargo.toml @@ -30,7 +30,7 @@ parse_datetime = { workspace = true } uucore = { workspace = true, features = ["parser"] } [target.'cfg(unix)'.dependencies] -libc = { workspace = true } +nix = { workspace = true, features = ["time"] } [target.'cfg(windows)'.dependencies] windows-sys = { workspace = true, features = [ diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index 532125600..d2100fc80 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -9,10 +9,6 @@ use clap::{Arg, ArgAction, Command}; use jiff::fmt::strtime; use jiff::tz::{TimeZone, TimeZoneDatabase}; use jiff::{Timestamp, Zoned}; -#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] -use libc::clock_settime; -#[cfg(all(unix, not(target_os = "redox")))] -use libc::{CLOCK_REALTIME, clock_getres, timespec}; use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; @@ -700,25 +696,20 @@ fn get_clock_resolution() -> Timestamp { } #[cfg(all(unix, not(target_os = "redox")))] +/// Returns the resolution of the system’s realtime clock. +/// +/// # Panics +/// +/// Panics if `clock_getres` fails. On a POSIX-compliant system this should not occur, +/// as `CLOCK_REALTIME` is required to be supported. +/// Failure would indicate a non-conforming or otherwise broken implementation. fn get_clock_resolution() -> Timestamp { - let mut timespec = timespec { - tv_sec: 0, - tv_nsec: 0, - }; - unsafe { - // SAFETY: the timespec struct lives for the full duration of this function call. - // - // The clock_getres function can only fail if the passed clock_id is not - // a known clock. All compliant posix implementors must support - // CLOCK_REALTIME, therefore this function call cannot fail on any - // compliant posix implementation. - // - // See more here: - // https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html - clock_getres(CLOCK_REALTIME, &raw mut timespec); - } + use nix::time::{ClockId, clock_getres}; + + let timespec = clock_getres(ClockId::CLOCK_REALTIME).unwrap(); + #[allow(clippy::unnecessary_cast)] // Cast required on 32-bit platforms - Timestamp::constant(timespec.tv_sec as i64, timespec.tv_nsec as i32) + Timestamp::constant(timespec.tv_sec() as _, timespec.tv_nsec() as _) } #[cfg(all(unix, target_os = "redox"))] @@ -766,20 +757,13 @@ fn set_system_datetime(_date: Zoned) -> UResult<()> { /// `` /// `` fn set_system_datetime(date: Zoned) -> UResult<()> { + use nix::{sys::time::TimeSpec, time::ClockId}; + let ts = date.timestamp(); - let timespec = timespec { - tv_sec: ts.as_second() as _, - tv_nsec: ts.subsec_nanosecond() as _, - }; + let timespec = TimeSpec::new(ts.as_second() as _, ts.subsec_nanosecond() as _); - let result = unsafe { clock_settime(CLOCK_REALTIME, &raw const timespec) }; - - if result == 0 { - Ok(()) - } else { - Err(std::io::Error::last_os_error() - .map_err_context(|| translate!("date-error-cannot-set-date"))) - } + nix::time::clock_settime(ClockId::CLOCK_REALTIME, timespec) + .map_err_context(|| translate!("date-error-cannot-set-date")) } #[cfg(windows)]