Skip to content

Commit 05cda35

Browse files
committed
Don't use field initializers for libc::timespec
This create conflict if the timespec of a target has additional fields. Use libc::timespec::default() instead
1 parent 85805c3 commit 05cda35

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

library/std/src/sys/fs/unix.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,12 @@ fn file_time_to_timespec(time: Option<SystemTime>) -> io::Result<libc::timespec>
18721872
io::ErrorKind::InvalidInput,
18731873
"timestamp is too small to set as a file time",
18741874
)),
1875-
None => Ok(libc::timespec { tv_sec: 0, tv_nsec: libc::UTIME_OMIT as _ }),
1875+
None => Ok({
1876+
let mut ts = libc::timespec::default();
1877+
ts.tv_sec = 0;
1878+
ts.tv_nsec = libc::UTIME_OMIT as _;
1879+
ts
1880+
}),
18761881
}
18771882
}
18781883

library/std/src/sys/pal/unix/time.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::mem;
12
use core::num::niche_types::Nanoseconds;
23

34
use crate::io;
@@ -6,8 +7,12 @@ use crate::time::Duration;
67
const NSEC_PER_SEC: u64 = 1_000_000_000;
78

89
#[allow(dead_code)] // Used for pthread condvar timeouts
9-
pub const TIMESPEC_MAX: libc::timespec =
10-
libc::timespec { tv_sec: <libc::time_t>::MAX, tv_nsec: 1_000_000_000 - 1 };
10+
pub const TIMESPEC_MAX: libc::timespec = {
11+
let mut ts = unsafe { mem::zeroed::<libc::timespec>() };
12+
ts.tv_sec = <libc::time_t>::MAX;
13+
ts.tv_nsec = 1_000_000_000 - 1;
14+
ts
15+
};
1116

1217
// This additional constant is only used when calling
1318
// `libc::pthread_cond_timedwait`.
@@ -164,9 +169,11 @@ impl Timespec {
164169

165170
#[allow(dead_code)]
166171
pub fn to_timespec(&self) -> Option<libc::timespec> {
167-
Some(libc::timespec {
168-
tv_sec: self.tv_sec.try_into().ok()?,
169-
tv_nsec: self.tv_nsec.as_inner().try_into().ok()?,
172+
Some({
173+
let mut ts = libc::timespec::default();
174+
ts.tv_sec = self.tv_sec.try_into().ok()?;
175+
ts.tv_nsec = self.tv_nsec.as_inner().try_into().ok()?;
176+
ts
170177
})
171178
}
172179

library/std/src/sys/thread/unix.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,10 @@ pub fn sleep(dur: Duration) {
570570
// nanosleep will fill in `ts` with the remaining time.
571571
unsafe {
572572
while secs > 0 || nsecs > 0 {
573-
let mut ts = libc::timespec {
574-
tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
575-
tv_nsec: nsecs,
576-
};
573+
let mut ts = libc::timespec::default();
574+
ts.tv_sec = cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t;
575+
ts.tv_nsec = nsecs;
576+
577577
secs -= ts.tv_sec as u64;
578578
let ts_ptr = &raw mut ts;
579579
let r = nanosleep(ts_ptr, ts_ptr);

0 commit comments

Comments
 (0)