Merge pull request #9688 from xtqqczze/get_clock_resolution

date: remove unsafe
This commit is contained in:
Daniel Hofstetter 2025-12-18 08:36:15 +01:00 committed by GitHub
commit 4aabacb7f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 36 deletions

2
Cargo.lock generated
View file

@ -3191,7 +3191,7 @@ dependencies = [
"clap",
"fluent",
"jiff",
"libc",
"nix",
"parse_datetime",
"uucore",
"windows-sys 0.61.2",

2
fuzz/Cargo.lock generated
View file

@ -1597,7 +1597,7 @@ dependencies = [
"clap",
"fluent",
"jiff",
"libc",
"nix",
"parse_datetime",
"uucore",
"windows-sys 0.61.2",

View file

@ -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 = [

View file

@ -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 systems 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<()> {
/// `<https://linux.die.net/man/3/clock_settime>`
/// `<https://www.gnu.org/software/libc/manual/html_node/Time-Types.html>`
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)]