You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following sample compiles in 64bit but does not compile on 32bit:
fnmain(){let tv_sec:i64 = 1741340955;letmut tm = unsafe{ std::mem::zeroed()};ifunsafe{ libc::localtime_r(&tv_sec,&mut tm)}.is_null(){panic!("Failed to determine local time via localtime_r");}println!("Today is {}-{:02}-{:02}",
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday
);}
The equivalent C code, compiles and produces the correct result on both 64bit and 32bit:
intmain() {
int64_ttv_sec=1741340955;
structtmtm;
if (localtime_r(&tv_sec, &tm) ==NULL) {
perror("Failed to determine local time via localtime_r");
exit(EXIT_FAILURE);
}
printf("Today is %d-%02d-%02d\n",
tm.tm_year+1900,
tm.tm_mon+1,
tm.tm_mday);
return0;
}
Context
This becomes a problem when using other crates which interact with the same underlying type from libc (the implementation itself, not the libc crate).
The following works fine on 64bit:
letTimespec{ tv_sec, tv_nsec } = rustix::clock_gettime(ClockId::Realtime);// SAFETY: This is safe as long as tm is fully overwritten before use, which localtime_r does.letmut tm = unsafe{ std::mem::zeroed()};// SAFETY: localtime_r is called with a valid pointer to secs and a mutable reference to tm.ifunsafe{localtime_r(&tv_sec,&mut tm)}.is_null(){bail!("Failed to determine local time via localtime_r");}
But on 32bit platforms it fails with:
error[E0308]: mismatched types
--> src/paths.rs:44:29
|
44 | if unsafe { localtime_r(&tv_sec, &mut tm) }.is_null() {
| ----------- ^^^^^^^ expected `*const i32`, found `&i64`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const i32`
found reference `&i64`
note: function defined here
--> /home/buildozer/.cargo/registry/src/index.crates.io-1cd66030c949c28d/libc-0.2.170/src/unix/mod.rs:1328:12
|
1328 | pub fn localtime_r(time_p: *const time_t, result: *mut tm) -> *mut tm;
| ^^^^^^^^^^^
The text was updated successfully, but these errors were encountered:
Yeah, the issues Chris linked cover this thoroughly so I'm going to close this. See also relevant PRs #3175, #3791 and #3068, these need some help moving forward if that is something you are interested in.
Thanks, I'm following up on these. Note that #3175 is about a different libc implementation, where I believe that the size of time_t is configurable at build time, whereas musl has a fixed size.
On almost all 32bit platforms, this crate defines
time_t = i32
: https://docs.rs/libc/0.2.170/src/libc/unix/linux_like/linux/gnu/b64/mod.rs.html#19At least on Linux/musl, libc defines
time_t = i64
: https://git.musl-libc.org/cgit/musl/tree/include/alltypes.h.in#n12See also: https://musl.libc.org/time64.html
Examples
The following sample compiles in 64bit but does not compile on 32bit:
The equivalent C code, compiles and produces the correct result on both 64bit and 32bit:
Context
This becomes a problem when using other crates which interact with the same underlying type from libc (the implementation itself, not the libc crate).
The following works fine on 64bit:
But on 32bit platforms it fails with:
The text was updated successfully, but these errors were encountered: