diff --git a/src/header/time/mod.rs b/src/header/time/mod.rs index 7c17155a5e0b48c8d9aec1af8a3279196fc860a5..b5c7aa2d0c9e2b79e5d4a9fd715e3273732e72fb 100644 --- a/src/header/time/mod.rs +++ b/src/header/time/mod.rs @@ -141,9 +141,22 @@ pub unsafe extern "C" fn ctime(clock: *const time_t) -> *mut c_char { #[no_mangle] pub unsafe extern "C" fn ctime_r(clock: *const time_t, buf: *mut c_char) -> *mut c_char { - let mut tm1: tm = core::mem::uninitialized(); + // Using MaybeUninit<tm> seems to cause a panic during the build process + let mut tm1 = tm { + tm_sec: 0, + tm_min: 0, + tm_hour: 0, + tm_mday: 0, + tm_mon: 0, + tm_year: 0, + tm_wday: 0, + tm_yday: 0, + tm_isdst: 0, + tm_gmtoff: 0, + tm_zone: core::ptr::null_mut(), + }; localtime_r(clock, &mut tm1); - asctime_r(&mut tm1, buf) + asctime_r(&tm1, buf) } #[no_mangle] diff --git a/tests/expected/time/localtime.stdout b/tests/expected/time/localtime.stdout index e79e0fb873aa1f6d53f72a3c9b0227940d1617a8..a3807007d24d9ca041ab14906202ddd5900c6aa0 100644 --- a/tests/expected/time/localtime.stdout +++ b/tests/expected/time/localtime.stdout @@ -6,3 +6,4 @@ Year 70, Day of year: 0, Month 0, Day of month: 1, Day of week: 4, 0:0:0 Year 70, Day of year: 0, Month 0, Day of month: 1, Day of week: 4, 0:0:1 Year 118, Day of year: 193, Month 6, Day of month: 13, Day of week: 5, 4:9:10 Fri Jul 13 06:03:43 2018 +Fri Jul 13 06:03:43 2018 diff --git a/tests/time/localtime.c b/tests/time/localtime.c index df8d2d550f7b4f659f036484679b964e61425583..fd7af735ce1365c07969fc9e585cf0360ec34cdd 100644 --- a/tests/time/localtime.c +++ b/tests/time/localtime.c @@ -17,4 +17,15 @@ int main(void) { time_t input = 1531461823; fputs(ctime(&input), stdout); // Omit newline + + char ctime_r_buffer[26]; + /* ctime_r() generally returns the address of the provided buffer, + * but may return NULL upon error according to the spec. */ + char *ctime_r_result = ctime_r(&input, ctime_r_buffer); + if (ctime_r_result == ctime_r_buffer) { + fputs(ctime_r_result, stdout); + } + else { + printf("Unexpected pointer from ctime_r: %p\n", ctime_r_result); + } }